util.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /*
  2. Various utilities
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2007, 2009, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Miguel de Icaza, 1994, 1995, 1996
  8. Janne Kukonlehto, 1994, 1995, 1996
  9. Dugan Porter, 1994, 1995, 1996
  10. Jakub Jelinek, 1994, 1995, 1996
  11. Mauricio Plaza, 1994, 1995, 1996
  12. The file_date routine is mostly from GNU's fileutils package,
  13. written by Richard Stallman and David MacKenzie.
  14. This file is part of the Midnight Commander.
  15. The Midnight Commander is free software: you can redistribute it
  16. and/or modify it under the terms of the GNU General Public License as
  17. published by the Free Software Foundation, either version 3 of the License,
  18. or (at your option) any later version.
  19. The Midnight Commander is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License
  24. along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. /** \file lib/util.c
  27. * \brief Source: various utilities
  28. */
  29. #include <config.h>
  30. #include <ctype.h>
  31. #include <limits.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <fcntl.h>
  37. #include <sys/time.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <unistd.h>
  41. #include "lib/global.h"
  42. #include "lib/mcconfig.h"
  43. #include "lib/fileloc.h"
  44. #include "lib/vfs/vfs.h"
  45. #include "lib/strutil.h"
  46. #include "lib/util.h"
  47. /*** global variables ****************************************************************************/
  48. /*** file scope macro definitions ****************************************************************/
  49. #define ismode(n,m) ((n & m) == m)
  50. /* Number of attempts to create a temporary file */
  51. #ifndef TMP_MAX
  52. #define TMP_MAX 16384
  53. #endif /* !TMP_MAX */
  54. #define TMP_SUFFIX ".tmp"
  55. #define ASCII_A (0x40 + 1)
  56. #define ASCII_Z (0x40 + 26)
  57. #define ASCII_a (0x60 + 1)
  58. #define ASCII_z (0x60 + 26)
  59. /*** file scope type declarations ****************************************************************/
  60. /*** file scope variables ************************************************************************/
  61. /*** file scope functions ************************************************************************/
  62. /* --------------------------------------------------------------------------------------------- */
  63. static inline int
  64. is_7bit_printable (unsigned char c)
  65. {
  66. return (c > 31 && c < 127);
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. static inline int
  70. is_iso_printable (unsigned char c)
  71. {
  72. return ((c > 31 && c < 127) || c >= 160);
  73. }
  74. /* --------------------------------------------------------------------------------------------- */
  75. static inline int
  76. is_8bit_printable (unsigned char c)
  77. {
  78. /* "Full 8 bits output" doesn't work on xterm */
  79. if (mc_global.tty.xterm_flag)
  80. return is_iso_printable (c);
  81. return (c > 31 && c != 127 && c != 155);
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. static char *
  85. resolve_symlinks (const vfs_path_t * vpath)
  86. {
  87. char *p, *p2;
  88. char *buf, *buf2, *q, *r, c;
  89. struct stat mybuf;
  90. if (vpath->relative)
  91. return NULL;
  92. p = p2 = vfs_path_to_str (vpath);
  93. r = buf = g_malloc (MC_MAXPATHLEN);
  94. buf2 = g_malloc (MC_MAXPATHLEN);
  95. *r++ = PATH_SEP;
  96. *r = 0;
  97. do
  98. {
  99. q = strchr (p + 1, PATH_SEP);
  100. if (!q)
  101. {
  102. q = strchr (p + 1, 0);
  103. if (q == p + 1)
  104. break;
  105. }
  106. c = *q;
  107. *q = 0;
  108. if (mc_lstat (vpath, &mybuf) < 0)
  109. {
  110. g_free (buf);
  111. buf = NULL;
  112. goto ret;
  113. }
  114. if (!S_ISLNK (mybuf.st_mode))
  115. strcpy (r, p + 1);
  116. else
  117. {
  118. int len;
  119. len = mc_readlink (vpath, buf2, MC_MAXPATHLEN - 1);
  120. if (len < 0)
  121. {
  122. g_free (buf);
  123. buf = NULL;
  124. goto ret;
  125. }
  126. buf2[len] = 0;
  127. if (*buf2 == PATH_SEP)
  128. strcpy (buf, buf2);
  129. else
  130. strcpy (r, buf2);
  131. }
  132. canonicalize_pathname (buf);
  133. r = strchr (buf, 0);
  134. if (!*r || *(r - 1) != PATH_SEP)
  135. /* FIXME: this condition is always true because r points to the EOL */
  136. {
  137. *r++ = PATH_SEP;
  138. *r = 0;
  139. }
  140. *q = c;
  141. p = q;
  142. }
  143. while (c != '\0');
  144. if (!*buf)
  145. strcpy (buf, PATH_SEP_STR);
  146. else if (*(r - 1) == PATH_SEP && r != buf + 1)
  147. *(r - 1) = 0;
  148. ret:
  149. g_free (buf2);
  150. g_free (p2);
  151. return buf;
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. static gboolean
  155. mc_util_write_backup_content (const char *from_file_name, const char *to_file_name)
  156. {
  157. FILE *backup_fd;
  158. char *contents;
  159. gsize length;
  160. gboolean ret1 = TRUE;
  161. if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
  162. return FALSE;
  163. backup_fd = fopen (to_file_name, "w");
  164. if (backup_fd == NULL)
  165. {
  166. g_free (contents);
  167. return FALSE;
  168. }
  169. if (fwrite ((const void *) contents, 1, length, backup_fd) != length)
  170. ret1 = FALSE;
  171. {
  172. int ret2;
  173. ret2 = fflush (backup_fd);
  174. ret2 = fclose (backup_fd);
  175. }
  176. g_free (contents);
  177. return ret1;
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. /*** public functions ****************************************************************************/
  181. /* --------------------------------------------------------------------------------------------- */
  182. int
  183. is_printable (int c)
  184. {
  185. c &= 0xff;
  186. #ifdef HAVE_CHARSET
  187. /* "Display bits" is ignored, since the user controls the output
  188. by setting the output codepage */
  189. return is_8bit_printable (c);
  190. #else
  191. if (!mc_global.eight_bit_clean)
  192. return is_7bit_printable (c);
  193. if (mc_global.full_eight_bits)
  194. {
  195. return is_8bit_printable (c);
  196. }
  197. else
  198. return is_iso_printable (c);
  199. #endif /* !HAVE_CHARSET */
  200. }
  201. /* --------------------------------------------------------------------------------------------- */
  202. /**
  203. * Quote the filename for the purpose of inserting it into the command
  204. * line. If quote_percent is 1, replace "%" with "%%" - the percent is
  205. * processed by the mc command line.
  206. */
  207. char *
  208. name_quote (const char *s, int quote_percent)
  209. {
  210. char *ret, *d;
  211. d = ret = g_malloc (strlen (s) * 2 + 2 + 1);
  212. if (*s == '-')
  213. {
  214. *d++ = '.';
  215. *d++ = '/';
  216. }
  217. for (; *s; s++, d++)
  218. {
  219. switch (*s)
  220. {
  221. case '%':
  222. if (quote_percent)
  223. *d++ = '%';
  224. break;
  225. case '\'':
  226. case '\\':
  227. case '\r':
  228. case '\n':
  229. case '\t':
  230. case '"':
  231. case ';':
  232. case ' ':
  233. case '?':
  234. case '|':
  235. case '[':
  236. case ']':
  237. case '{':
  238. case '}':
  239. case '<':
  240. case '>':
  241. case '`':
  242. case '!':
  243. case '$':
  244. case '&':
  245. case '*':
  246. case '(':
  247. case ')':
  248. *d++ = '\\';
  249. break;
  250. case '~':
  251. case '#':
  252. if (d == ret)
  253. *d++ = '\\';
  254. break;
  255. }
  256. *d = *s;
  257. }
  258. *d = '\0';
  259. return ret;
  260. }
  261. /* --------------------------------------------------------------------------------------------- */
  262. char *
  263. fake_name_quote (const char *s, int quote_percent)
  264. {
  265. (void) quote_percent;
  266. return g_strdup (s);
  267. }
  268. /* --------------------------------------------------------------------------------------------- */
  269. /**
  270. * path_trunc() is the same as str_trunc() but
  271. * it deletes possible password from path for security
  272. * reasons.
  273. */
  274. const char *
  275. path_trunc (const char *path, size_t trunc_len)
  276. {
  277. vfs_path_t *vpath;
  278. char *secure_path;
  279. const char *ret;
  280. vpath = vfs_path_from_str (path);
  281. secure_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
  282. vfs_path_free (vpath);
  283. ret = str_trunc (secure_path, trunc_len);
  284. g_free (secure_path);
  285. return ret;
  286. }
  287. /* --------------------------------------------------------------------------------------------- */
  288. const char *
  289. size_trunc (uintmax_t size, gboolean use_si)
  290. {
  291. static char x[BUF_TINY];
  292. uintmax_t divisor = 1;
  293. const char *xtra = "";
  294. if (size > 999999999UL)
  295. {
  296. divisor = use_si ? 1000 : 1024;
  297. xtra = use_si ? "k" : "K";
  298. if (size / divisor > 999999999UL)
  299. {
  300. divisor = use_si ? (1000 * 1000) : (1024 * 1024);
  301. xtra = use_si ? "m" : "M";
  302. if (size / divisor > 999999999UL)
  303. {
  304. divisor = use_si ? (1000 * 1000 * 1000) : (1024 * 1024 * 1024);
  305. xtra = use_si ? "g" : "G";
  306. }
  307. }
  308. }
  309. g_snprintf (x, sizeof (x), "%.0f%s", 1.0 * size / divisor, xtra);
  310. return x;
  311. }
  312. /* --------------------------------------------------------------------------------------------- */
  313. const char *
  314. size_trunc_sep (uintmax_t size, gboolean use_si)
  315. {
  316. static char x[60];
  317. int count;
  318. const char *p, *y;
  319. char *d;
  320. p = y = size_trunc (size, use_si);
  321. p += strlen (p) - 1;
  322. d = x + sizeof (x) - 1;
  323. *d-- = '\0';
  324. while (p >= y && isalpha ((unsigned char) *p))
  325. *d-- = *p--;
  326. for (count = 0; p >= y; count++)
  327. {
  328. if (count == 3)
  329. {
  330. *d-- = ',';
  331. count = 0;
  332. }
  333. *d-- = *p--;
  334. }
  335. d++;
  336. if (*d == ',')
  337. d++;
  338. return d;
  339. }
  340. /* --------------------------------------------------------------------------------------------- */
  341. /**
  342. * Print file SIZE to BUFFER, but don't exceed LEN characters,
  343. * not including trailing 0. BUFFER should be at least LEN+1 long.
  344. * This function is called for every file on panels, so avoid
  345. * floating point by any means.
  346. *
  347. * Units: size units (filesystem sizes are 1K blocks)
  348. * 0=bytes, 1=Kbytes, 2=Mbytes, etc.
  349. */
  350. void
  351. size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si)
  352. {
  353. /* Avoid taking power for every file. */
  354. /* *INDENT-OFF* */
  355. static const uintmax_t power10[] = {
  356. /* we hope that size of uintmax_t is 4 bytes at least */
  357. 1ULL,
  358. 10ULL,
  359. 100ULL,
  360. 1000ULL,
  361. 10000ULL,
  362. 100000ULL,
  363. 1000000ULL,
  364. 10000000ULL,
  365. 100000000ULL,
  366. 1000000000ULL
  367. /* maximmum value of uintmax_t (in case of 4 bytes) is
  368. 4294967295
  369. */
  370. #if SIZEOF_UINTMAX_T == 8
  371. ,
  372. 10000000000ULL,
  373. 100000000000ULL,
  374. 1000000000000ULL,
  375. 10000000000000ULL,
  376. 100000000000000ULL,
  377. 1000000000000000ULL,
  378. 10000000000000000ULL,
  379. 100000000000000000ULL,
  380. 1000000000000000000ULL,
  381. 10000000000000000000ULL
  382. /* maximmum value of uintmax_t (in case of 8 bytes) is
  383. 18447644073710439615
  384. */
  385. #endif
  386. };
  387. /* *INDENT-ON* */
  388. static const char *const suffix[] = { "", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL };
  389. static const char *const suffix_lc[] = { "", "k", "m", "g", "t", "p", "e", "z", "y", NULL };
  390. int j = 0;
  391. if (len == 0)
  392. len = 9;
  393. #if SIZEOF_UINTMAX_T == 8
  394. /* 20 decimal digits are required to represent 8 bytes */
  395. else if (len > 19)
  396. len = 19;
  397. #else
  398. /* 10 decimal digits are required to represent 4 bytes */
  399. else if (len > 9)
  400. len = 9;
  401. #endif
  402. /*
  403. * recalculate from 1024 base to 1000 base if units>0
  404. * We can't just multiply by 1024 - that might cause overflow
  405. * if off_t type is too small
  406. */
  407. if (use_si)
  408. for (j = 0; j < units; j++)
  409. {
  410. uintmax_t size_remain;
  411. size_remain = ((size % 125) * 1024) / 1000; /* size mod 125, recalculated */
  412. size = size / 125; /* 128/125 = 1024/1000 */
  413. size = size * 128; /* This will convert size from multiple of 1024 to multiple of 1000 */
  414. size += size_remain; /* Re-add remainder lost by division/multiplication */
  415. }
  416. for (j = units; suffix[j] != NULL; j++)
  417. {
  418. if (size == 0)
  419. {
  420. if (j == units)
  421. {
  422. /* Empty files will print "0" even with minimal width. */
  423. g_snprintf (buffer, len + 1, "0");
  424. break;
  425. }
  426. /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
  427. g_snprintf (buffer, len + 1, (len > 1) ? "~%s" : "%s",
  428. (j > 1) ? (use_si ? suffix_lc[j - 1] : suffix[j - 1]) : "B");
  429. break;
  430. }
  431. if (size < power10[len - (j > 0 ? 1 : 0)])
  432. {
  433. g_snprintf (buffer, len + 1, "%" PRIuMAX "%s", size, use_si ? suffix_lc[j] : suffix[j]);
  434. break;
  435. }
  436. /* Powers of 1000 or 1024, with rounding. */
  437. if (use_si)
  438. size = (size + 500) / 1000;
  439. else
  440. size = (size + 512) >> 10;
  441. }
  442. }
  443. /* --------------------------------------------------------------------------------------------- */
  444. const char *
  445. string_perm (mode_t mode_bits)
  446. {
  447. static char mode[11];
  448. strcpy (mode, "----------");
  449. if (S_ISDIR (mode_bits))
  450. mode[0] = 'd';
  451. if (S_ISCHR (mode_bits))
  452. mode[0] = 'c';
  453. if (S_ISBLK (mode_bits))
  454. mode[0] = 'b';
  455. if (S_ISLNK (mode_bits))
  456. mode[0] = 'l';
  457. if (S_ISFIFO (mode_bits))
  458. mode[0] = 'p';
  459. if (S_ISNAM (mode_bits))
  460. mode[0] = 'n';
  461. if (S_ISSOCK (mode_bits))
  462. mode[0] = 's';
  463. if (S_ISDOOR (mode_bits))
  464. mode[0] = 'D';
  465. if (ismode (mode_bits, S_IXOTH))
  466. mode[9] = 'x';
  467. if (ismode (mode_bits, S_IWOTH))
  468. mode[8] = 'w';
  469. if (ismode (mode_bits, S_IROTH))
  470. mode[7] = 'r';
  471. if (ismode (mode_bits, S_IXGRP))
  472. mode[6] = 'x';
  473. if (ismode (mode_bits, S_IWGRP))
  474. mode[5] = 'w';
  475. if (ismode (mode_bits, S_IRGRP))
  476. mode[4] = 'r';
  477. if (ismode (mode_bits, S_IXUSR))
  478. mode[3] = 'x';
  479. if (ismode (mode_bits, S_IWUSR))
  480. mode[2] = 'w';
  481. if (ismode (mode_bits, S_IRUSR))
  482. mode[1] = 'r';
  483. #ifdef S_ISUID
  484. if (ismode (mode_bits, S_ISUID))
  485. mode[3] = (mode[3] == 'x') ? 's' : 'S';
  486. #endif /* S_ISUID */
  487. #ifdef S_ISGID
  488. if (ismode (mode_bits, S_ISGID))
  489. mode[6] = (mode[6] == 'x') ? 's' : 'S';
  490. #endif /* S_ISGID */
  491. #ifdef S_ISVTX
  492. if (ismode (mode_bits, S_ISVTX))
  493. mode[9] = (mode[9] == 'x') ? 't' : 'T';
  494. #endif /* S_ISVTX */
  495. return mode;
  496. }
  497. /* --------------------------------------------------------------------------------------------- */
  498. const char *
  499. extension (const char *filename)
  500. {
  501. const char *d = strrchr (filename, '.');
  502. return (d != NULL) ? d + 1 : "";
  503. }
  504. /* --------------------------------------------------------------------------------------------- */
  505. char *
  506. load_mc_home_file (const char *from, const char *filename, char **allocated_filename)
  507. {
  508. char *hintfile_base, *hintfile;
  509. char *lang;
  510. char *data;
  511. hintfile_base = g_build_filename (from, filename, (char *) NULL);
  512. lang = guess_message_value ();
  513. hintfile = g_strconcat (hintfile_base, ".", lang, (char *) NULL);
  514. if (!g_file_get_contents (hintfile, &data, NULL, NULL))
  515. {
  516. /* Fall back to the two-letter language code */
  517. if (lang[0] != '\0' && lang[1] != '\0')
  518. lang[2] = '\0';
  519. g_free (hintfile);
  520. hintfile = g_strconcat (hintfile_base, ".", lang, (char *) NULL);
  521. if (!g_file_get_contents (hintfile, &data, NULL, NULL))
  522. {
  523. g_free (hintfile);
  524. hintfile = hintfile_base;
  525. g_file_get_contents (hintfile_base, &data, NULL, NULL);
  526. }
  527. }
  528. g_free (lang);
  529. if (hintfile != hintfile_base)
  530. g_free (hintfile_base);
  531. if (allocated_filename != NULL)
  532. *allocated_filename = hintfile;
  533. else
  534. g_free (hintfile);
  535. return data;
  536. }
  537. /* --------------------------------------------------------------------------------------------- */
  538. const char *
  539. extract_line (const char *s, const char *top)
  540. {
  541. static char tmp_line[BUF_MEDIUM];
  542. char *t = tmp_line;
  543. while (*s && *s != '\n' && (size_t) (t - tmp_line) < sizeof (tmp_line) - 1 && s < top)
  544. *t++ = *s++;
  545. *t = 0;
  546. return tmp_line;
  547. }
  548. /* --------------------------------------------------------------------------------------------- */
  549. /**
  550. * The basename routine
  551. */
  552. const char *
  553. x_basename (const char *s)
  554. {
  555. const char *url_delim, *path_sep;
  556. url_delim = g_strrstr (s, VFS_PATH_URL_DELIMITER);
  557. path_sep = strrchr (s, PATH_SEP);
  558. if (url_delim == NULL
  559. || url_delim < path_sep - strlen (VFS_PATH_URL_DELIMITER)
  560. || url_delim - s + strlen (VFS_PATH_URL_DELIMITER) < strlen (s))
  561. {
  562. /* avoid trailing PATH_SEP, if present */
  563. if (s[strlen (s) - 1] == PATH_SEP)
  564. {
  565. while (--path_sep > s && *path_sep != PATH_SEP);
  566. return (path_sep != s) ? path_sep + 1 : s;
  567. }
  568. else
  569. return (path_sep != NULL) ? path_sep + 1 : s;
  570. }
  571. while (--url_delim > s && *url_delim != PATH_SEP);
  572. while (--url_delim > s && *url_delim != PATH_SEP);
  573. return (url_delim == s) ? s : url_delim + 1;
  574. }
  575. /* --------------------------------------------------------------------------------------------- */
  576. const char *
  577. unix_error_string (int error_num)
  578. {
  579. static char buffer[BUF_LARGE];
  580. gchar *strerror_currentlocale;
  581. strerror_currentlocale = g_locale_from_utf8 (g_strerror (error_num), -1, NULL, NULL, NULL);
  582. g_snprintf (buffer, sizeof (buffer), "%s (%d)", strerror_currentlocale, error_num);
  583. g_free (strerror_currentlocale);
  584. return buffer;
  585. }
  586. /* --------------------------------------------------------------------------------------------- */
  587. const char *
  588. skip_separators (const char *s)
  589. {
  590. const char *su = s;
  591. for (; *su; str_cnext_char (&su))
  592. if (*su != ' ' && *su != '\t' && *su != ',')
  593. break;
  594. return su;
  595. }
  596. /* --------------------------------------------------------------------------------------------- */
  597. const char *
  598. skip_numbers (const char *s)
  599. {
  600. const char *su = s;
  601. for (; *su; str_cnext_char (&su))
  602. if (!str_isdigit (su))
  603. break;
  604. return su;
  605. }
  606. /* --------------------------------------------------------------------------------------------- */
  607. /**
  608. * Remove all control sequences from the argument string. We define
  609. * "control sequence", in a sort of pidgin BNF, as follows:
  610. *
  611. * control-seq = Esc non-'['
  612. * | Esc '[' (0 or more digits or ';' or '?') (any other char)
  613. *
  614. * This scheme works for all the terminals described in my termcap /
  615. * terminfo databases, except the Hewlett-Packard 70092 and some Wyse
  616. * terminals. If I hear from a single person who uses such a terminal
  617. * with MC, I'll be glad to add support for it. (Dugan)
  618. * Non-printable characters are also removed.
  619. */
  620. char *
  621. strip_ctrl_codes (char *s)
  622. {
  623. char *w; /* Current position where the stripped data is written */
  624. char *r; /* Current position where the original data is read */
  625. char *n;
  626. if (!s)
  627. return 0;
  628. for (w = s, r = s; *r;)
  629. {
  630. if (*r == ESC_CHAR)
  631. {
  632. /* Skip the control sequence's arguments */ ;
  633. /* '(' need to avoid strange 'B' letter in *Suse (if mc runs under root user) */
  634. if (*(++r) == '[' || *r == '(')
  635. {
  636. /* strchr() matches trailing binary 0 */
  637. while (*(++r) && strchr ("0123456789;?", *r));
  638. }
  639. else if (*r == ']')
  640. {
  641. /*
  642. * Skip xterm's OSC (Operating System Command)
  643. * http://www.xfree86.org/current/ctlseqs.html
  644. * OSC P s ; P t ST
  645. * OSC P s ; P t BEL
  646. */
  647. char *new_r = r;
  648. for (; *new_r; ++new_r)
  649. {
  650. switch (*new_r)
  651. {
  652. /* BEL */
  653. case '\a':
  654. r = new_r;
  655. goto osc_out;
  656. case ESC_CHAR:
  657. /* ST */
  658. if (*(new_r + 1) == '\\')
  659. {
  660. r = new_r + 1;
  661. goto osc_out;
  662. }
  663. }
  664. }
  665. osc_out:;
  666. }
  667. /*
  668. * Now we are at the last character of the sequence.
  669. * Skip it unless it's binary 0.
  670. */
  671. if (*r)
  672. r++;
  673. continue;
  674. }
  675. n = str_get_next_char (r);
  676. if (str_isprint (r))
  677. {
  678. memmove (w, r, n - r);
  679. w += n - r;
  680. }
  681. r = n;
  682. }
  683. *w = 0;
  684. return s;
  685. }
  686. /* --------------------------------------------------------------------------------------------- */
  687. enum compression_type
  688. get_compression_type (int fd, const char *name)
  689. {
  690. unsigned char magic[16];
  691. size_t str_len;
  692. /* Read the magic signature */
  693. if (mc_read (fd, (char *) magic, 4) != 4)
  694. return COMPRESSION_NONE;
  695. /* GZIP_MAGIC and OLD_GZIP_MAGIC */
  696. if (magic[0] == 037 && (magic[1] == 0213 || magic[1] == 0236))
  697. {
  698. return COMPRESSION_GZIP;
  699. }
  700. /* PKZIP_MAGIC */
  701. if (magic[0] == 0120 && magic[1] == 0113 && magic[2] == 003 && magic[3] == 004)
  702. {
  703. /* Read compression type */
  704. mc_lseek (fd, 8, SEEK_SET);
  705. if (mc_read (fd, (char *) magic, 2) != 2)
  706. return COMPRESSION_NONE;
  707. /* Gzip can handle only deflated (8) or stored (0) files */
  708. if ((magic[0] != 8 && magic[0] != 0) || magic[1] != 0)
  709. return COMPRESSION_NONE;
  710. /* Compatible with gzip */
  711. return COMPRESSION_GZIP;
  712. }
  713. /* PACK_MAGIC and LZH_MAGIC and compress magic */
  714. if (magic[0] == 037 && (magic[1] == 036 || magic[1] == 0240 || magic[1] == 0235))
  715. {
  716. /* Compatible with gzip */
  717. return COMPRESSION_GZIP;
  718. }
  719. /* BZIP and BZIP2 files */
  720. if ((magic[0] == 'B') && (magic[1] == 'Z') && (magic[3] >= '1') && (magic[3] <= '9'))
  721. {
  722. switch (magic[2])
  723. {
  724. case '0':
  725. return COMPRESSION_BZIP;
  726. case 'h':
  727. return COMPRESSION_BZIP2;
  728. }
  729. }
  730. /* Support for LZMA (only utils format with magic in header).
  731. * This is the default format of LZMA utils 4.32.1 and later. */
  732. if (mc_read (fd, (char *) magic + 4, 2) != 2)
  733. return COMPRESSION_NONE;
  734. /* LZMA utils format */
  735. if (magic[0] == 0xFF
  736. && magic[1] == 'L'
  737. && magic[2] == 'Z' && magic[3] == 'M' && magic[4] == 'A' && magic[5] == 0x00)
  738. return COMPRESSION_LZMA;
  739. /* XZ compression magic */
  740. if (magic[0] == 0xFD
  741. && magic[1] == 0x37
  742. && magic[2] == 0x7A && magic[3] == 0x58 && magic[4] == 0x5A && magic[5] == 0x00)
  743. return COMPRESSION_XZ;
  744. str_len = strlen (name);
  745. /* HACK: we must belive to extention of LZMA file :) ... */
  746. if ((str_len > 5 && strcmp (&name[str_len - 5], ".lzma") == 0) ||
  747. (str_len > 4 && strcmp (&name[str_len - 4], ".tlz") == 0))
  748. return COMPRESSION_LZMA;
  749. return COMPRESSION_NONE;
  750. }
  751. /* --------------------------------------------------------------------------------------------- */
  752. const char *
  753. decompress_extension (int type)
  754. {
  755. switch (type)
  756. {
  757. case COMPRESSION_GZIP:
  758. return "/ugz" VFS_PATH_URL_DELIMITER;
  759. case COMPRESSION_BZIP:
  760. return "/ubz" VFS_PATH_URL_DELIMITER;
  761. case COMPRESSION_BZIP2:
  762. return "/ubz2" VFS_PATH_URL_DELIMITER;
  763. case COMPRESSION_LZMA:
  764. return "/ulzma" VFS_PATH_URL_DELIMITER;
  765. case COMPRESSION_XZ:
  766. return "/uxz" VFS_PATH_URL_DELIMITER;
  767. }
  768. /* Should never reach this place */
  769. fprintf (stderr, "Fatal: decompress_extension called with an unknown argument\n");
  770. return 0;
  771. }
  772. /* --------------------------------------------------------------------------------------------- */
  773. void
  774. wipe_password (char *passwd)
  775. {
  776. char *p = passwd;
  777. if (!p)
  778. return;
  779. for (; *p; p++)
  780. *p = 0;
  781. g_free (passwd);
  782. }
  783. /* --------------------------------------------------------------------------------------------- */
  784. /**
  785. * Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key
  786. *
  787. * @param p pointer to string
  788. *
  789. * @return newly allocated string
  790. */
  791. char *
  792. convert_controls (const char *p)
  793. {
  794. char *valcopy = g_strdup (p);
  795. char *q;
  796. /* Parse the escape special character */
  797. for (q = valcopy; *p;)
  798. {
  799. if (*p == '\\')
  800. {
  801. p++;
  802. if ((*p == 'e') || (*p == 'E'))
  803. {
  804. p++;
  805. *q++ = ESC_CHAR;
  806. }
  807. }
  808. else
  809. {
  810. if (*p == '^')
  811. {
  812. p++;
  813. if (*p == '^')
  814. *q++ = *p++;
  815. else
  816. {
  817. char c = (*p | 0x20);
  818. if (c >= 'a' && c <= 'z')
  819. {
  820. *q++ = c - 'a' + 1;
  821. p++;
  822. }
  823. else if (*p)
  824. p++;
  825. }
  826. }
  827. else
  828. *q++ = *p++;
  829. }
  830. }
  831. *q = 0;
  832. return valcopy;
  833. }
  834. /* --------------------------------------------------------------------------------------------- */
  835. /**
  836. * Finds out a relative path from first to second, i.e. goes as many ..
  837. * as needed up in first and then goes down using second
  838. */
  839. char *
  840. diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
  841. {
  842. char *p, *q, *r, *s, *buf = NULL;
  843. int i, j, prevlen = -1, currlen;
  844. char *my_first = NULL, *my_second = NULL;
  845. my_first = resolve_symlinks (vpath1);
  846. if (my_first == NULL)
  847. goto ret;
  848. my_second = resolve_symlinks (vpath2);
  849. if (my_second == NULL)
  850. goto ret;
  851. for (j = 0; j < 2; j++)
  852. {
  853. p = my_first;
  854. q = my_second;
  855. while (TRUE)
  856. {
  857. r = strchr (p, PATH_SEP);
  858. s = strchr (q, PATH_SEP);
  859. if (r == NULL || s == NULL)
  860. break;
  861. *r = '\0';
  862. *s = '\0';
  863. if (strcmp (p, q) != 0)
  864. {
  865. *r = PATH_SEP;
  866. *s = PATH_SEP;
  867. break;
  868. }
  869. *r = PATH_SEP;
  870. *s = PATH_SEP;
  871. p = r + 1;
  872. q = s + 1;
  873. }
  874. p--;
  875. for (i = 0; (p = strchr (p + 1, PATH_SEP)) != NULL; i++)
  876. ;
  877. currlen = (i + 1) * 3 + strlen (q) + 1;
  878. if (j != 0)
  879. {
  880. if (currlen < prevlen)
  881. g_free (buf);
  882. else
  883. goto ret;
  884. }
  885. p = buf = g_malloc (currlen);
  886. prevlen = currlen;
  887. for (; i >= 0; i--, p += 3)
  888. strcpy (p, "../");
  889. strcpy (p, q);
  890. }
  891. ret:
  892. g_free (my_first);
  893. g_free (my_second);
  894. return buf;
  895. }
  896. /* --------------------------------------------------------------------------------------------- */
  897. /**
  898. * Append text to GList, remove all entries with the same text
  899. */
  900. GList *
  901. list_append_unique (GList * list, char *text)
  902. {
  903. GList *lc_link;
  904. /*
  905. * Go to the last position and traverse the list backwards
  906. * starting from the second last entry to make sure that we
  907. * are not removing the current link.
  908. */
  909. list = g_list_append (list, text);
  910. list = g_list_last (list);
  911. lc_link = g_list_previous (list);
  912. while (lc_link != NULL)
  913. {
  914. GList *newlink;
  915. newlink = g_list_previous (lc_link);
  916. if (strcmp ((char *) lc_link->data, text) == 0)
  917. {
  918. GList *tmp;
  919. g_free (lc_link->data);
  920. tmp = g_list_remove_link (list, lc_link);
  921. g_list_free_1 (lc_link);
  922. }
  923. lc_link = newlink;
  924. }
  925. return list;
  926. }
  927. /* --------------------------------------------------------------------------------------------- */
  928. /**
  929. * Read and restore position for the given filename.
  930. * If there is no stored data, return line 1 and col 0.
  931. */
  932. void
  933. load_file_position (const vfs_path_t * filename_vpath, long *line, long *column, off_t * offset,
  934. GArray ** bookmarks)
  935. {
  936. char *fn;
  937. FILE *f;
  938. char buf[MC_MAXPATHLEN + 100];
  939. const size_t len = vfs_path_len (filename_vpath);
  940. char *filename;
  941. /* defaults */
  942. *line = 1;
  943. *column = 0;
  944. *offset = 0;
  945. /* open file with positions */
  946. fn = mc_config_get_full_path (MC_FILEPOS_FILE);
  947. f = fopen (fn, "r");
  948. g_free (fn);
  949. if (f == NULL)
  950. return;
  951. /* prepare array for serialized bookmarks */
  952. *bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS);
  953. filename = vfs_path_to_str (filename_vpath);
  954. while (fgets (buf, sizeof (buf), f) != NULL)
  955. {
  956. const char *p;
  957. gchar **pos_tokens;
  958. /* check if the filename matches the beginning of string */
  959. if (strncmp (buf, filename, len) != 0)
  960. continue;
  961. /* followed by single space */
  962. if (buf[len] != ' ')
  963. continue;
  964. /* and string without spaces */
  965. p = &buf[len + 1];
  966. if (strchr (p, ' ') != NULL)
  967. continue;
  968. pos_tokens = g_strsplit (p, ";", 3 + MAX_SAVED_BOOKMARKS);
  969. if (pos_tokens[0] == NULL)
  970. {
  971. *line = 1;
  972. *column = 0;
  973. *offset = 0;
  974. }
  975. else
  976. {
  977. *line = strtol (pos_tokens[0], NULL, 10);
  978. if (pos_tokens[1] == NULL)
  979. {
  980. *column = 0;
  981. *offset = 0;
  982. }
  983. else
  984. {
  985. *column = strtol (pos_tokens[1], NULL, 10);
  986. if (pos_tokens[2] == NULL)
  987. *offset = 0;
  988. else
  989. {
  990. size_t i;
  991. *offset = strtoll (pos_tokens[2], NULL, 10);
  992. for (i = 0; i < MAX_SAVED_BOOKMARKS && pos_tokens[3 + i] != NULL; i++)
  993. {
  994. size_t val;
  995. val = strtoul (pos_tokens[3 + i], NULL, 10);
  996. g_array_append_val (*bookmarks, val);
  997. }
  998. }
  999. }
  1000. }
  1001. g_strfreev (pos_tokens);
  1002. }
  1003. g_free (filename);
  1004. fclose (f);
  1005. }
  1006. /* --------------------------------------------------------------------------------------------- */
  1007. /**
  1008. * Save position for the given file
  1009. */
  1010. void
  1011. save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
  1012. GArray * bookmarks)
  1013. {
  1014. static size_t filepos_max_saved_entries = 0;
  1015. char *fn, *tmp_fn;
  1016. FILE *f, *tmp_f;
  1017. char buf[MC_MAXPATHLEN + 100];
  1018. size_t i;
  1019. const size_t len = vfs_path_len (filename_vpath);
  1020. gboolean src_error = FALSE;
  1021. char *filename;
  1022. if (filepos_max_saved_entries == 0)
  1023. filepos_max_saved_entries = mc_config_get_int (mc_main_config, CONFIG_APP_SECTION,
  1024. "filepos_max_saved_entries", 1024);
  1025. fn = mc_config_get_full_path (MC_FILEPOS_FILE);
  1026. if (fn == NULL)
  1027. goto early_error;
  1028. mc_util_make_backup_if_possible (fn, TMP_SUFFIX);
  1029. /* open file */
  1030. f = fopen (fn, "w");
  1031. if (f == NULL)
  1032. goto open_target_error;
  1033. tmp_fn = g_strdup_printf ("%s" TMP_SUFFIX, fn);
  1034. tmp_f = fopen (tmp_fn, "r");
  1035. if (tmp_f == NULL)
  1036. {
  1037. src_error = TRUE;
  1038. goto open_source_error;
  1039. }
  1040. filename = vfs_path_to_str (filename_vpath);
  1041. /* put the new record */
  1042. if (line != 1 || column != 0 || bookmarks != NULL)
  1043. {
  1044. if (fprintf (f, "%s %ld;%ld;%" PRIuMAX, filename, line, column, (uintmax_t) offset) < 0)
  1045. goto write_position_error;
  1046. if (bookmarks != NULL)
  1047. for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++)
  1048. if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0)
  1049. goto write_position_error;
  1050. if (fprintf (f, "\n") < 0)
  1051. goto write_position_error;
  1052. }
  1053. i = 1;
  1054. while (fgets (buf, sizeof (buf), tmp_f) != NULL)
  1055. {
  1056. if (buf[len] == ' ' && strncmp (buf, filename, len) == 0
  1057. && strchr (&buf[len + 1], ' ') == NULL)
  1058. continue;
  1059. fprintf (f, "%s", buf);
  1060. if (++i > filepos_max_saved_entries)
  1061. break;
  1062. }
  1063. write_position_error:
  1064. g_free (filename);
  1065. fclose (tmp_f);
  1066. open_source_error:
  1067. g_free (tmp_fn);
  1068. fclose (f);
  1069. if (src_error)
  1070. mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
  1071. else
  1072. mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
  1073. open_target_error:
  1074. g_free (fn);
  1075. early_error:
  1076. if (bookmarks != NULL)
  1077. g_array_free (bookmarks, TRUE);
  1078. }
  1079. /* --------------------------------------------------------------------------------------------- */
  1080. extern int
  1081. ascii_alpha_to_cntrl (int ch)
  1082. {
  1083. if ((ch >= ASCII_A && ch <= ASCII_Z) || (ch >= ASCII_a && ch <= ASCII_z))
  1084. {
  1085. ch &= 0x1f;
  1086. }
  1087. return ch;
  1088. }
  1089. /* --------------------------------------------------------------------------------------------- */
  1090. const char *
  1091. Q_ (const char *s)
  1092. {
  1093. const char *result, *sep;
  1094. result = _(s);
  1095. sep = strchr (result, '|');
  1096. return (sep != NULL) ? sep + 1 : result;
  1097. }
  1098. /* --------------------------------------------------------------------------------------------- */
  1099. gboolean
  1100. mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffix)
  1101. {
  1102. struct stat stat_buf;
  1103. char *backup_path;
  1104. gboolean ret;
  1105. if (!exist_file (file_name))
  1106. return FALSE;
  1107. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1108. if (backup_path == NULL)
  1109. return FALSE;
  1110. ret = mc_util_write_backup_content (file_name, backup_path);
  1111. if (ret)
  1112. {
  1113. /* Backup file will have same ownership with main file. */
  1114. if (stat (file_name, &stat_buf) == 0)
  1115. chmod (backup_path, stat_buf.st_mode);
  1116. else
  1117. chmod (backup_path, S_IRUSR | S_IWUSR);
  1118. }
  1119. g_free (backup_path);
  1120. return ret;
  1121. }
  1122. /* --------------------------------------------------------------------------------------------- */
  1123. gboolean
  1124. mc_util_restore_from_backup_if_possible (const char *file_name, const char *backup_suffix)
  1125. {
  1126. gboolean ret;
  1127. char *backup_path;
  1128. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1129. if (backup_path == NULL)
  1130. return FALSE;
  1131. ret = mc_util_write_backup_content (backup_path, file_name);
  1132. g_free (backup_path);
  1133. return ret;
  1134. }
  1135. /* --------------------------------------------------------------------------------------------- */
  1136. gboolean
  1137. mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix)
  1138. {
  1139. char *backup_path;
  1140. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1141. if (backup_path == NULL)
  1142. return FALSE;
  1143. if (exist_file (backup_path))
  1144. {
  1145. vfs_path_t *vpath;
  1146. vpath = vfs_path_from_str (backup_path);
  1147. mc_unlink (vpath);
  1148. vfs_path_free (vpath);
  1149. }
  1150. g_free (backup_path);
  1151. return TRUE;
  1152. }
  1153. /* --------------------------------------------------------------------------------------------- */
  1154. /**
  1155. * partly taken from dcigettext.c, returns "" for default locale
  1156. * value should be freed by calling function g_free()
  1157. */
  1158. char *
  1159. guess_message_value (void)
  1160. {
  1161. static const char *const var[] = {
  1162. /* Setting of LC_ALL overwrites all other. */
  1163. /* Do not use LANGUAGE for check user locale and drowing hints */
  1164. "LC_ALL",
  1165. /* Next comes the name of the desired category. */
  1166. "LC_MESSAGES",
  1167. /* Last possibility is the LANG environment variable. */
  1168. "LANG",
  1169. /* NULL exit loops */
  1170. NULL
  1171. };
  1172. unsigned i = 0;
  1173. const char *locale = NULL;
  1174. while (var[i] != NULL)
  1175. {
  1176. locale = getenv (var[i]);
  1177. if (locale != NULL && locale[0] != '\0')
  1178. break;
  1179. i++;
  1180. }
  1181. if (locale == NULL)
  1182. locale = "";
  1183. return g_strdup (locale);
  1184. }
  1185. /* --------------------------------------------------------------------------------------------- */