util.c 36 KB

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