util.c 36 KB

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