util.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. Various utilities
  3. Copyright (C) 1994-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Miguel de Icaza, 1994, 1995, 1996
  7. Janne Kukonlehto, 1994, 1995, 1996
  8. Dugan Porter, 1994, 1995, 1996
  9. Jakub Jelinek, 1994, 1995, 1996
  10. Mauricio Plaza, 1994, 1995, 1996
  11. Slava Zanko <slavazanko@gmail.com>, 2013
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file lib/util.c
  25. * \brief Source: various utilities
  26. */
  27. #include <config.h>
  28. #include <ctype.h>
  29. #include <stddef.h> /* ptrdiff_t */
  30. #include <limits.h>
  31. #include <stdarg.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <unistd.h>
  38. #include "lib/global.h"
  39. #include "lib/mcconfig.h"
  40. #include "lib/fileloc.h"
  41. #include "lib/vfs/vfs.h"
  42. #include "lib/strutil.h"
  43. #include "lib/util.h"
  44. /*** global variables ****************************************************************************/
  45. /*** file scope macro definitions ****************************************************************/
  46. #define ismode(n,m) ((n & m) == m)
  47. /* Number of attempts to create a temporary file */
  48. #ifndef TMP_MAX
  49. #define TMP_MAX 16384
  50. #endif /* !TMP_MAX */
  51. #define TMP_SUFFIX ".tmp"
  52. #define ASCII_A (0x40 + 1)
  53. #define ASCII_Z (0x40 + 26)
  54. #define ASCII_a (0x60 + 1)
  55. #define ASCII_z (0x60 + 26)
  56. /*** file scope type declarations ****************************************************************/
  57. /*** forward declarations (file scope functions) *************************************************/
  58. /*** file scope variables ************************************************************************/
  59. /* --------------------------------------------------------------------------------------------- */
  60. /*** file scope functions ************************************************************************/
  61. /* --------------------------------------------------------------------------------------------- */
  62. #ifndef HAVE_CHARSET
  63. static inline int
  64. is_7bit_printable (unsigned char c)
  65. {
  66. return (c > 31 && c < 127);
  67. }
  68. #endif
  69. /* --------------------------------------------------------------------------------------------- */
  70. static inline int
  71. is_iso_printable (unsigned char c)
  72. {
  73. return ((c > 31 && c < 127) || c >= 160);
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. static inline int
  77. is_8bit_printable (unsigned char c)
  78. {
  79. /* "Full 8 bits output" doesn't work on xterm */
  80. if (mc_global.tty.xterm_flag)
  81. return is_iso_printable (c);
  82. return (c > 31 && c != 127 && c != 155);
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. static char *
  86. resolve_symlinks (const vfs_path_t *vpath)
  87. {
  88. char *p, *p2;
  89. char *buf, *buf2, *q, *r, c;
  90. struct stat mybuf;
  91. if (vpath->relative)
  92. return NULL;
  93. p = p2 = g_strdup (vfs_path_as_str (vpath));
  94. r = buf = g_malloc (MC_MAXPATHLEN);
  95. buf2 = g_malloc (MC_MAXPATHLEN);
  96. *r++ = PATH_SEP;
  97. *r = '\0';
  98. do
  99. {
  100. q = strchr (p + 1, PATH_SEP);
  101. if (q == NULL)
  102. {
  103. q = strchr (p + 1, '\0');
  104. if (q == p + 1)
  105. break;
  106. }
  107. c = *q;
  108. *q = '\0';
  109. if (mc_lstat (vpath, &mybuf) < 0)
  110. {
  111. MC_PTR_FREE (buf);
  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. MC_PTR_FREE (buf);
  123. goto ret;
  124. }
  125. buf2[len] = '\0';
  126. if (IS_PATH_SEP (*buf2))
  127. strcpy (buf, buf2);
  128. else
  129. strcpy (r, buf2);
  130. }
  131. canonicalize_pathname (buf);
  132. r = strchr (buf, '\0');
  133. if (*r == '\0' || !IS_PATH_SEP (r[-1]))
  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 == '\0')
  144. strcpy (buf, PATH_SEP_STR);
  145. else if (IS_PATH_SEP (r[-1]) && 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. return is_8bit_printable (c);
  197. return is_iso_printable (c);
  198. #endif /* !HAVE_CHARSET */
  199. }
  200. /* --------------------------------------------------------------------------------------------- */
  201. /**
  202. * Quote the filename for the purpose of inserting it into the command
  203. * line. If quote_percent is TRUE, replace "%" with "%%" - the percent is
  204. * processed by the mc command line.
  205. */
  206. char *
  207. name_quote (const char *s, gboolean quote_percent)
  208. {
  209. GString *ret;
  210. if (s == NULL || *s == '\0')
  211. return NULL;
  212. ret = g_string_sized_new (64);
  213. if (*s == '-')
  214. g_string_append (ret, "." PATH_SEP_STR);
  215. for (; *s != '\0'; s++)
  216. {
  217. switch (*s)
  218. {
  219. case '%':
  220. if (quote_percent)
  221. g_string_append_c (ret, '%');
  222. break;
  223. case '\'':
  224. case '\\':
  225. case '\r':
  226. case '\n':
  227. case '\t':
  228. case '"':
  229. case ';':
  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. g_string_append_c (ret, '\\');
  247. break;
  248. case '~':
  249. case '#':
  250. if (ret->len == 0)
  251. g_string_append_c (ret, '\\');
  252. break;
  253. default:
  254. break;
  255. }
  256. g_string_append_c (ret, *s);
  257. }
  258. return g_string_free (ret, ret->len == 0);
  259. }
  260. /* --------------------------------------------------------------------------------------------- */
  261. char *
  262. fake_name_quote (const char *s, gboolean quote_percent)
  263. {
  264. (void) quote_percent;
  265. return (s == NULL || *s == '\0' ? NULL : g_strdup (s));
  266. }
  267. /* --------------------------------------------------------------------------------------------- */
  268. /**
  269. * path_trunc() is the same as str_trunc() but
  270. * it deletes possible password from path for security
  271. * reasons.
  272. */
  273. const char *
  274. path_trunc (const char *path, size_t trunc_len)
  275. {
  276. vfs_path_t *vpath;
  277. const char *ret;
  278. vpath = vfs_path_from_str_flags (path, VPF_STRIP_PASSWORD);
  279. ret = str_trunc (vfs_path_as_str (vpath), trunc_len);
  280. vfs_path_free (vpath, TRUE);
  281. return ret;
  282. }
  283. /* --------------------------------------------------------------------------------------------- */
  284. const char *
  285. size_trunc (uintmax_t size, gboolean use_si)
  286. {
  287. static char x[BUF_TINY];
  288. uintmax_t divisor = 1;
  289. const char *xtra = _("B");
  290. if (size > 999999999UL)
  291. {
  292. divisor = use_si ? 1000 : 1024;
  293. xtra = use_si ? _("kB") : _("KiB");
  294. if (size / divisor > 999999999UL)
  295. {
  296. divisor = use_si ? (1000 * 1000) : (1024 * 1024);
  297. xtra = use_si ? _("MB") : _("MiB");
  298. if (size / divisor > 999999999UL)
  299. {
  300. divisor = use_si ? (1000 * 1000 * 1000) : (1024 * 1024 * 1024);
  301. xtra = use_si ? _("GB") : _("GiB");
  302. }
  303. }
  304. }
  305. g_snprintf (x, sizeof (x), "%.0f %s", 1.0 * size / divisor, xtra);
  306. return x;
  307. }
  308. /* --------------------------------------------------------------------------------------------- */
  309. const char *
  310. size_trunc_sep (uintmax_t size, gboolean use_si)
  311. {
  312. static char x[60];
  313. int count;
  314. const char *p, *y;
  315. char *d;
  316. p = y = size_trunc (size, use_si);
  317. p += strlen (p) - 1;
  318. d = x + sizeof (x) - 1;
  319. *d-- = '\0';
  320. /* @size format is "size unit", i.e. "[digits][space][letters]".
  321. Copy all characters after digits. */
  322. while (p >= y && !g_ascii_isdigit (*p))
  323. *d-- = *p--;
  324. for (count = 0; p >= y; count++)
  325. {
  326. if (count == 3)
  327. {
  328. *d-- = ',';
  329. count = 0;
  330. }
  331. *d-- = *p--;
  332. }
  333. d++;
  334. if (*d == ',')
  335. d++;
  336. return d;
  337. }
  338. /* --------------------------------------------------------------------------------------------- */
  339. /**
  340. * Print file SIZE to BUFFER, but don't exceed LEN characters,
  341. * not including trailing 0. BUFFER should be at least LEN+1 long.
  342. * This function is called for every file on panels, so avoid
  343. * floating point by any means.
  344. *
  345. * Units: size units (filesystem sizes are 1K blocks)
  346. * 0=bytes, 1=Kbytes, 2=Mbytes, etc.
  347. */
  348. void
  349. size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si)
  350. {
  351. /* Avoid taking power for every file. */
  352. /* *INDENT-OFF* */
  353. static const uintmax_t power10[] = {
  354. /* we hope that size of uintmax_t is 4 bytes at least */
  355. 1ULL,
  356. 10ULL,
  357. 100ULL,
  358. 1000ULL,
  359. 10000ULL,
  360. 100000ULL,
  361. 1000000ULL,
  362. 10000000ULL,
  363. 100000000ULL,
  364. 1000000000ULL
  365. /* maximum value of uintmax_t (in case of 4 bytes) is
  366. 4294967295
  367. */
  368. #if SIZEOF_UINTMAX_T == 8
  369. ,
  370. 10000000000ULL,
  371. 100000000000ULL,
  372. 1000000000000ULL,
  373. 10000000000000ULL,
  374. 100000000000000ULL,
  375. 1000000000000000ULL,
  376. 10000000000000000ULL,
  377. 100000000000000000ULL,
  378. 1000000000000000000ULL,
  379. 10000000000000000000ULL
  380. /* maximum value of uintmax_t (in case of 8 bytes) is
  381. 18447644073710439615
  382. */
  383. #endif
  384. };
  385. static const char *const suffix[] =
  386. { "", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q", NULL };
  387. static const char *const suffix_lc[] =
  388. { "", "k", "m", "g", "t", "p", "e", "z", "y", "r", "q", NULL };
  389. /* *INDENT-ON* */
  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;
  504. d = strrchr (filename, '.');
  505. return d != NULL ? d + 1 : "";
  506. }
  507. /* --------------------------------------------------------------------------------------------- */
  508. char *
  509. load_mc_home_file (const char *from, const char *filename, char **allocated_filename,
  510. size_t *length)
  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, length, 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, length, NULL))
  526. {
  527. g_free (hintfile);
  528. hintfile = hintfile_base;
  529. g_file_get_contents (hintfile_base, &data, length, 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 != '\0' && *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 (path_sep == NULL)
  563. return s;
  564. if (url_delim == NULL
  565. || url_delim < path_sep - strlen (VFS_PATH_URL_DELIMITER)
  566. || url_delim - s + strlen (VFS_PATH_URL_DELIMITER) < strlen (s))
  567. {
  568. /* avoid trailing PATH_SEP, if present */
  569. if (!IS_PATH_SEP (s[strlen (s) - 1]))
  570. return path_sep + 1;
  571. while (--path_sep > s && !IS_PATH_SEP (*path_sep))
  572. ;
  573. return (path_sep != s) ? path_sep + 1 : s;
  574. }
  575. while (--url_delim > s && !IS_PATH_SEP (*url_delim))
  576. ;
  577. while (--url_delim > s && !IS_PATH_SEP (*url_delim))
  578. ;
  579. return url_delim == s ? s : url_delim + 1;
  580. }
  581. /* --------------------------------------------------------------------------------------------- */
  582. const char *
  583. unix_error_string (int error_num)
  584. {
  585. static char buffer[BUF_LARGE];
  586. gchar *strerror_currentlocale;
  587. strerror_currentlocale = g_locale_from_utf8 (g_strerror (error_num), -1, NULL, NULL, NULL);
  588. g_snprintf (buffer, sizeof (buffer), "%s (%d)", strerror_currentlocale, error_num);
  589. g_free (strerror_currentlocale);
  590. return buffer;
  591. }
  592. /* --------------------------------------------------------------------------------------------- */
  593. const char *
  594. skip_separators (const char *s)
  595. {
  596. const char *su = s;
  597. for (; *su != '\0'; str_cnext_char (&su))
  598. if (!whitespace (*su) && *su != ',')
  599. break;
  600. return su;
  601. }
  602. /* --------------------------------------------------------------------------------------------- */
  603. const char *
  604. skip_numbers (const char *s)
  605. {
  606. const char *su = s;
  607. for (; *su != '\0'; str_cnext_char (&su))
  608. if (!str_isdigit (su))
  609. break;
  610. return su;
  611. }
  612. /* --------------------------------------------------------------------------------------------- */
  613. /**
  614. * Remove all control sequences from the argument string. We define
  615. * "control sequence", in a sort of pidgin BNF, as follows:
  616. *
  617. * control-seq = Esc non-'['
  618. * | Esc '[' (0 or more digits or ';' or ':' or '?') (any other char)
  619. *
  620. * The 256-color and true-color escape sequences should allow either ';' or ':' inside as separator,
  621. * actually, ':' is the more correct according to ECMA-48.
  622. * Some terminal emulators (e.g. xterm, gnome-terminal) support this.
  623. *
  624. * Non-printable characters are also removed.
  625. */
  626. char *
  627. strip_ctrl_codes (char *s)
  628. {
  629. char *w; /* Current position where the stripped data is written */
  630. char *r; /* Current position where the original data is read */
  631. if (s == NULL)
  632. return NULL;
  633. for (w = s, r = s; *r != '\0';)
  634. {
  635. if (*r == ESC_CHAR)
  636. {
  637. /* Skip the control sequence's arguments */ ;
  638. /* '(' need to avoid strange 'B' letter in *Suse (if mc runs under root user) */
  639. if (*(++r) == '[' || *r == '(')
  640. {
  641. /* strchr() matches trailing binary 0 */
  642. while (*(++r) != '\0' && strchr ("0123456789;:?", *r) != NULL)
  643. ;
  644. }
  645. else if (*r == ']')
  646. {
  647. /*
  648. * Skip xterm's OSC (Operating System Command)
  649. * http://www.xfree86.org/current/ctlseqs.html
  650. * OSC P s ; P t ST
  651. * OSC P s ; P t BEL
  652. */
  653. char *new_r;
  654. for (new_r = r; *new_r != '\0'; new_r++)
  655. {
  656. switch (*new_r)
  657. {
  658. /* BEL */
  659. case '\a':
  660. r = new_r;
  661. goto osc_out;
  662. case ESC_CHAR:
  663. /* ST */
  664. if (new_r[1] == '\\')
  665. {
  666. r = new_r + 1;
  667. goto osc_out;
  668. }
  669. break;
  670. default:
  671. break;
  672. }
  673. }
  674. osc_out:
  675. ;
  676. }
  677. /*
  678. * Now we are at the last character of the sequence.
  679. * Skip it unless it's binary 0.
  680. */
  681. if (*r != '\0')
  682. r++;
  683. }
  684. else
  685. {
  686. char *n;
  687. n = str_get_next_char (r);
  688. if (str_isprint (r))
  689. {
  690. memmove (w, r, n - r);
  691. w += n - r;
  692. }
  693. r = n;
  694. }
  695. }
  696. *w = '\0';
  697. return s;
  698. }
  699. /* --------------------------------------------------------------------------------------------- */
  700. enum compression_type
  701. get_compression_type (int fd, const char *name)
  702. {
  703. unsigned char magic[16];
  704. size_t str_len;
  705. /* Read the magic signature */
  706. if (mc_read (fd, (char *) magic, 4) != 4)
  707. return COMPRESSION_NONE;
  708. /* GZIP_MAGIC and OLD_GZIP_MAGIC */
  709. if (magic[0] == 0x1F && (magic[1] == 0x8B || magic[1] == 0x9E))
  710. return COMPRESSION_GZIP;
  711. /* PKZIP_MAGIC */
  712. if (magic[0] == 'P' && magic[1] == 'K' && magic[2] == 0x03 && magic[3] == 0x04)
  713. {
  714. /* Read compression type */
  715. mc_lseek (fd, 8, SEEK_SET);
  716. if (mc_read (fd, (char *) magic, 2) != 2)
  717. return COMPRESSION_NONE;
  718. if ((magic[0] != 8 && magic[0] != 0) || magic[1] != 0)
  719. return COMPRESSION_NONE;
  720. return COMPRESSION_ZIP;
  721. }
  722. /* PACK_MAGIC and LZH_MAGIC and compress magic */
  723. if (magic[0] == 0x1F && (magic[1] == 0x1E || magic[1] == 0xA0 || magic[1] == 0x9D))
  724. /* Compatible with gzip */
  725. return COMPRESSION_GZIP;
  726. /* BZIP and BZIP2 files */
  727. if ((magic[0] == 'B') && (magic[1] == 'Z') && (magic[3] >= '1') && (magic[3] <= '9'))
  728. switch (magic[2])
  729. {
  730. case '0':
  731. return COMPRESSION_BZIP;
  732. case 'h':
  733. return COMPRESSION_BZIP2;
  734. default:
  735. break;
  736. }
  737. /* LZ4 format - v1.5.0 - 0x184D2204 (little endian) */
  738. if (magic[0] == 0x04 && magic[1] == 0x22 && magic[2] == 0x4d && magic[3] == 0x18)
  739. return COMPRESSION_LZ4;
  740. if (mc_read (fd, (char *) magic + 4, 2) != 2)
  741. return COMPRESSION_NONE;
  742. /* LZIP files */
  743. if (magic[0] == 'L'
  744. && magic[1] == 'Z'
  745. && magic[2] == 'I' && magic[3] == 'P' && (magic[4] == 0x00 || magic[4] == 0x01))
  746. return COMPRESSION_LZIP;
  747. /* Support for LZMA (only utils format with magic in header).
  748. * This is the default format of LZMA utils 4.32.1 and later. */
  749. if (magic[0] == 0xFF
  750. && magic[1] == 'L'
  751. && magic[2] == 'Z' && magic[3] == 'M' && magic[4] == 'A' && magic[5] == 0x00)
  752. return COMPRESSION_LZMA;
  753. /* LZO format - \x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a lzop compressed data */
  754. if (magic[0] == 0x89 && magic[1] == 0x4c &&
  755. magic[2] == 0x5a && magic[3] == 0x4f && magic[4] == 0x00 && magic[5] == 0x0d)
  756. return COMPRESSION_LZO;
  757. /* XZ compression magic */
  758. if (magic[0] == 0xFD
  759. && magic[1] == 0x37
  760. && magic[2] == 0x7A && magic[3] == 0x58 && magic[4] == 0x5A && magic[5] == 0x00)
  761. return COMPRESSION_XZ;
  762. if (magic[0] == 0x28 && magic[1] == 0xB5 && magic[2] == 0x2F && magic[3] == 0xFD)
  763. return COMPRESSION_ZSTD;
  764. str_len = strlen (name);
  765. /* HACK: we must believe to extension of LZMA file :) ... */
  766. if ((str_len > 5 && strcmp (&name[str_len - 5], ".lzma") == 0) ||
  767. (str_len > 4 && strcmp (&name[str_len - 4], ".tlz") == 0))
  768. return COMPRESSION_LZMA;
  769. return COMPRESSION_NONE;
  770. }
  771. /* --------------------------------------------------------------------------------------------- */
  772. const char *
  773. decompress_extension (int type)
  774. {
  775. switch (type)
  776. {
  777. case COMPRESSION_ZIP:
  778. return "/uz" VFS_PATH_URL_DELIMITER;
  779. case COMPRESSION_GZIP:
  780. return "/ugz" VFS_PATH_URL_DELIMITER;
  781. case COMPRESSION_BZIP:
  782. return "/ubz" VFS_PATH_URL_DELIMITER;
  783. case COMPRESSION_BZIP2:
  784. return "/ubz2" VFS_PATH_URL_DELIMITER;
  785. case COMPRESSION_LZIP:
  786. return "/ulz" VFS_PATH_URL_DELIMITER;
  787. case COMPRESSION_LZ4:
  788. return "/ulz4" VFS_PATH_URL_DELIMITER;
  789. case COMPRESSION_LZMA:
  790. return "/ulzma" VFS_PATH_URL_DELIMITER;
  791. case COMPRESSION_LZO:
  792. return "/ulzo" VFS_PATH_URL_DELIMITER;
  793. case COMPRESSION_XZ:
  794. return "/uxz" VFS_PATH_URL_DELIMITER;
  795. case COMPRESSION_ZSTD:
  796. return "/uzst" VFS_PATH_URL_DELIMITER;
  797. default:
  798. break;
  799. }
  800. /* Should never reach this place */
  801. fprintf (stderr, "Fatal: decompress_extension called with an unknown argument\n");
  802. return 0;
  803. }
  804. /* --------------------------------------------------------------------------------------------- */
  805. void
  806. wipe_password (char *passwd)
  807. {
  808. if (passwd != NULL)
  809. {
  810. char *p;
  811. for (p = passwd; *p != '\0'; p++)
  812. *p = '\0';
  813. g_free (passwd);
  814. }
  815. }
  816. /* --------------------------------------------------------------------------------------------- */
  817. /**
  818. * Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key
  819. *
  820. * @param p pointer to string
  821. *
  822. * @return newly allocated string
  823. */
  824. char *
  825. convert_controls (const char *p)
  826. {
  827. char *valcopy;
  828. char *q;
  829. valcopy = g_strdup (p);
  830. /* Parse the escape special character */
  831. for (q = valcopy; *p != '\0';)
  832. switch (*p)
  833. {
  834. case '\\':
  835. p++;
  836. if (*p == 'e' || *p == 'E')
  837. {
  838. p++;
  839. *q++ = ESC_CHAR;
  840. }
  841. break;
  842. case '^':
  843. p++;
  844. if (*p == '^')
  845. *q++ = *p++;
  846. else
  847. {
  848. char c;
  849. c = *p | 0x20;
  850. if (c >= 'a' && c <= 'z')
  851. {
  852. *q++ = c - 'a' + 1;
  853. p++;
  854. }
  855. else if (*p != '\0')
  856. p++;
  857. }
  858. break;
  859. default:
  860. *q++ = *p++;
  861. }
  862. *q = '\0';
  863. return valcopy;
  864. }
  865. /* --------------------------------------------------------------------------------------------- */
  866. /**
  867. * Finds out a relative path from first to second, i.e. goes as many ..
  868. * as needed up in first and then goes down using second
  869. */
  870. char *
  871. diff_two_paths (const vfs_path_t *vpath1, const vfs_path_t *vpath2)
  872. {
  873. int j, prevlen = -1, currlen;
  874. char *my_first = NULL, *my_second = NULL;
  875. char *buf = NULL;
  876. my_first = resolve_symlinks (vpath1);
  877. if (my_first == NULL)
  878. goto ret;
  879. my_second = resolve_symlinks (vpath2);
  880. if (my_second == NULL)
  881. goto ret;
  882. for (j = 0; j < 2; j++)
  883. {
  884. char *p, *q;
  885. int i;
  886. p = my_first;
  887. q = my_second;
  888. while (TRUE)
  889. {
  890. char *r, *s;
  891. ptrdiff_t len;
  892. r = strchr (p, PATH_SEP);
  893. if (r == NULL)
  894. break;
  895. s = strchr (q, PATH_SEP);
  896. if (s == NULL)
  897. break;
  898. len = r - p;
  899. if (len != (s - q) || strncmp (p, q, (size_t) len) != 0)
  900. break;
  901. p = r + 1;
  902. q = s + 1;
  903. }
  904. p--;
  905. for (i = 0; (p = strchr (p + 1, PATH_SEP)) != NULL; i++)
  906. ;
  907. currlen = (i + 1) * 3 + strlen (q) + 1;
  908. if (j != 0)
  909. {
  910. if (currlen < prevlen)
  911. g_free (buf);
  912. else
  913. goto ret;
  914. }
  915. p = buf = g_malloc (currlen);
  916. prevlen = currlen;
  917. for (; i >= 0; i--, p += 3)
  918. strcpy (p, "../");
  919. strcpy (p, q);
  920. }
  921. ret:
  922. g_free (my_first);
  923. g_free (my_second);
  924. return buf;
  925. }
  926. /* --------------------------------------------------------------------------------------------- */
  927. /**
  928. * Append text to GList, remove all entries with the same text
  929. */
  930. GList *
  931. list_append_unique (GList *list, char *text)
  932. {
  933. GList *lc_link;
  934. /*
  935. * Go to the last position and traverse the list backwards
  936. * starting from the second last entry to make sure that we
  937. * are not removing the current link.
  938. */
  939. list = g_list_append (list, text);
  940. list = g_list_last (list);
  941. lc_link = g_list_previous (list);
  942. while (lc_link != NULL)
  943. {
  944. GList *newlink;
  945. newlink = g_list_previous (lc_link);
  946. if (strcmp ((char *) lc_link->data, text) == 0)
  947. {
  948. GList *tmp;
  949. g_free (lc_link->data);
  950. tmp = g_list_remove_link (list, lc_link);
  951. (void) tmp;
  952. g_list_free_1 (lc_link);
  953. }
  954. lc_link = newlink;
  955. }
  956. return list;
  957. }
  958. /* --------------------------------------------------------------------------------------------- */
  959. /**
  960. * Read and restore position for the given filename.
  961. * If there is no stored data, return line 1 and col 0.
  962. */
  963. void
  964. load_file_position (const vfs_path_t *filename_vpath, long *line, long *column, off_t *offset,
  965. GArray **bookmarks)
  966. {
  967. char *fn;
  968. FILE *f;
  969. char buf[MC_MAXPATHLEN + 100];
  970. const size_t len = vfs_path_len (filename_vpath);
  971. /* defaults */
  972. *line = 1;
  973. *column = 0;
  974. *offset = 0;
  975. /* open file with positions */
  976. fn = mc_config_get_full_path (MC_FILEPOS_FILE);
  977. f = fopen (fn, "r");
  978. g_free (fn);
  979. if (f == NULL)
  980. return;
  981. /* prepare array for serialized bookmarks */
  982. if (bookmarks != NULL)
  983. *bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS);
  984. while (fgets (buf, sizeof (buf), f) != NULL)
  985. {
  986. const char *p;
  987. gchar **pos_tokens;
  988. /* check if the filename matches the beginning of string */
  989. if (strncmp (buf, vfs_path_as_str (filename_vpath), len) != 0)
  990. continue;
  991. /* followed by single space */
  992. if (buf[len] != ' ')
  993. continue;
  994. /* and string without spaces */
  995. p = &buf[len + 1];
  996. if (strchr (p, ' ') != NULL)
  997. continue;
  998. pos_tokens = g_strsplit (p, ";", 3 + MAX_SAVED_BOOKMARKS);
  999. if (pos_tokens[0] == NULL)
  1000. {
  1001. *line = 1;
  1002. *column = 0;
  1003. *offset = 0;
  1004. }
  1005. else
  1006. {
  1007. *line = strtol (pos_tokens[0], NULL, 10);
  1008. if (pos_tokens[1] == NULL)
  1009. {
  1010. *column = 0;
  1011. *offset = 0;
  1012. }
  1013. else
  1014. {
  1015. *column = strtol (pos_tokens[1], NULL, 10);
  1016. if (pos_tokens[2] == NULL)
  1017. *offset = 0;
  1018. else if (bookmarks != NULL)
  1019. {
  1020. size_t i;
  1021. *offset = (off_t) g_ascii_strtoll (pos_tokens[2], NULL, 10);
  1022. for (i = 0; i < MAX_SAVED_BOOKMARKS && pos_tokens[3 + i] != NULL; i++)
  1023. {
  1024. size_t val;
  1025. val = strtoul (pos_tokens[3 + i], NULL, 10);
  1026. g_array_append_val (*bookmarks, val);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. g_strfreev (pos_tokens);
  1032. }
  1033. fclose (f);
  1034. }
  1035. /* --------------------------------------------------------------------------------------------- */
  1036. /**
  1037. * Save position for the given file
  1038. */
  1039. void
  1040. save_file_position (const vfs_path_t *filename_vpath, long line, long column, off_t offset,
  1041. GArray *bookmarks)
  1042. {
  1043. static size_t filepos_max_saved_entries = 0;
  1044. char *fn, *tmp_fn;
  1045. FILE *f, *tmp_f;
  1046. char buf[MC_MAXPATHLEN + 100];
  1047. size_t i;
  1048. const size_t len = vfs_path_len (filename_vpath);
  1049. gboolean src_error = FALSE;
  1050. if (filepos_max_saved_entries == 0)
  1051. filepos_max_saved_entries = mc_config_get_int (mc_global.main_config, CONFIG_APP_SECTION,
  1052. "filepos_max_saved_entries", 1024);
  1053. fn = mc_config_get_full_path (MC_FILEPOS_FILE);
  1054. if (fn == NULL)
  1055. goto early_error;
  1056. mc_util_make_backup_if_possible (fn, TMP_SUFFIX);
  1057. /* open file */
  1058. f = fopen (fn, "w");
  1059. if (f == NULL)
  1060. goto open_target_error;
  1061. tmp_fn = g_strdup_printf ("%s" TMP_SUFFIX, fn);
  1062. tmp_f = fopen (tmp_fn, "r");
  1063. if (tmp_f == NULL)
  1064. {
  1065. src_error = TRUE;
  1066. goto open_source_error;
  1067. }
  1068. /* put the new record */
  1069. if (line != 1 || column != 0 || bookmarks != NULL)
  1070. {
  1071. if (fprintf
  1072. (f, "%s %ld;%ld;%" PRIuMAX, vfs_path_as_str (filename_vpath), line, column,
  1073. (uintmax_t) offset) < 0)
  1074. goto write_position_error;
  1075. if (bookmarks != NULL)
  1076. for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++)
  1077. if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0)
  1078. goto write_position_error;
  1079. if (fprintf (f, "\n") < 0)
  1080. goto write_position_error;
  1081. }
  1082. i = 1;
  1083. while (fgets (buf, sizeof (buf), tmp_f) != NULL)
  1084. {
  1085. if (buf[len] == ' ' && strncmp (buf, vfs_path_as_str (filename_vpath), len) == 0
  1086. && strchr (&buf[len + 1], ' ') == NULL)
  1087. continue;
  1088. fprintf (f, "%s", buf);
  1089. if (++i > filepos_max_saved_entries)
  1090. break;
  1091. }
  1092. write_position_error:
  1093. fclose (tmp_f);
  1094. open_source_error:
  1095. g_free (tmp_fn);
  1096. fclose (f);
  1097. if (src_error)
  1098. mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
  1099. else
  1100. mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
  1101. open_target_error:
  1102. g_free (fn);
  1103. early_error:
  1104. if (bookmarks != NULL)
  1105. g_array_free (bookmarks, TRUE);
  1106. }
  1107. /* --------------------------------------------------------------------------------------------- */
  1108. extern int
  1109. ascii_alpha_to_cntrl (int ch)
  1110. {
  1111. if ((ch >= ASCII_A && ch <= ASCII_Z) || (ch >= ASCII_a && ch <= ASCII_z))
  1112. ch &= 0x1f;
  1113. return ch;
  1114. }
  1115. /* --------------------------------------------------------------------------------------------- */
  1116. const char *
  1117. Q_ (const char *s)
  1118. {
  1119. const char *result, *sep;
  1120. result = _(s);
  1121. sep = strchr (result, '|');
  1122. return sep != NULL ? sep + 1 : result;
  1123. }
  1124. /* --------------------------------------------------------------------------------------------- */
  1125. gboolean
  1126. mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffix)
  1127. {
  1128. struct stat stat_buf;
  1129. char *backup_path;
  1130. gboolean ret;
  1131. if (!exist_file (file_name))
  1132. return FALSE;
  1133. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1134. if (backup_path == NULL)
  1135. return FALSE;
  1136. ret = mc_util_write_backup_content (file_name, backup_path);
  1137. if (ret)
  1138. {
  1139. /* Backup file will have same ownership with main file. */
  1140. if (stat (file_name, &stat_buf) == 0)
  1141. chmod (backup_path, stat_buf.st_mode);
  1142. else
  1143. chmod (backup_path, S_IRUSR | S_IWUSR);
  1144. }
  1145. g_free (backup_path);
  1146. return ret;
  1147. }
  1148. /* --------------------------------------------------------------------------------------------- */
  1149. gboolean
  1150. mc_util_restore_from_backup_if_possible (const char *file_name, const char *backup_suffix)
  1151. {
  1152. gboolean ret;
  1153. char *backup_path;
  1154. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1155. if (backup_path == NULL)
  1156. return FALSE;
  1157. ret = mc_util_write_backup_content (backup_path, file_name);
  1158. g_free (backup_path);
  1159. return ret;
  1160. }
  1161. /* --------------------------------------------------------------------------------------------- */
  1162. gboolean
  1163. mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix)
  1164. {
  1165. char *backup_path;
  1166. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1167. if (backup_path == NULL)
  1168. return FALSE;
  1169. if (exist_file (backup_path))
  1170. {
  1171. vfs_path_t *vpath;
  1172. vpath = vfs_path_from_str (backup_path);
  1173. mc_unlink (vpath);
  1174. vfs_path_free (vpath, TRUE);
  1175. }
  1176. g_free (backup_path);
  1177. return TRUE;
  1178. }
  1179. /* --------------------------------------------------------------------------------------------- */
  1180. /**
  1181. * partly taken from dcigettext.c, returns "" for default locale
  1182. * value should be freed by calling function g_free()
  1183. */
  1184. char *
  1185. guess_message_value (void)
  1186. {
  1187. static const char *const var[] = {
  1188. /* Setting of LC_ALL overwrites all other. */
  1189. /* Do not use LANGUAGE for check user locale and drowing hints */
  1190. "LC_ALL",
  1191. /* Next comes the name of the desired category. */
  1192. "LC_MESSAGES",
  1193. /* Last possibility is the LANG environment variable. */
  1194. "LANG",
  1195. /* NULL exit loops */
  1196. NULL
  1197. };
  1198. size_t i;
  1199. const char *locale = NULL;
  1200. for (i = 0; var[i] != NULL; i++)
  1201. {
  1202. locale = getenv (var[i]);
  1203. if (locale != NULL && locale[0] != '\0')
  1204. break;
  1205. }
  1206. if (locale == NULL)
  1207. locale = "";
  1208. return g_strdup (locale);
  1209. }
  1210. /* --------------------------------------------------------------------------------------------- */
  1211. /**
  1212. * The "profile root" is the tree under which all of MC's user data &
  1213. * settings are stored.
  1214. *
  1215. * It defaults to the user's home dir. The user may override this default
  1216. * with the environment variable $MC_PROFILE_ROOT.
  1217. */
  1218. const char *
  1219. mc_get_profile_root (void)
  1220. {
  1221. static const char *profile_root = NULL;
  1222. if (profile_root == NULL)
  1223. {
  1224. profile_root = g_getenv ("MC_PROFILE_ROOT");
  1225. if (profile_root == NULL || *profile_root == '\0')
  1226. profile_root = mc_config_get_home_dir ();
  1227. }
  1228. return profile_root;
  1229. }
  1230. /* --------------------------------------------------------------------------------------------- */
  1231. /**
  1232. * Propagate error in simple way.
  1233. *
  1234. * @param dest error return location
  1235. * @param code error code
  1236. * @param format printf()-style format for error message
  1237. * @param ... parameters for message format
  1238. */
  1239. void
  1240. mc_propagate_error (GError **dest, int code, const char *format, ...)
  1241. {
  1242. if (dest != NULL && *dest == NULL)
  1243. {
  1244. GError *tmp_error;
  1245. va_list args;
  1246. va_start (args, format);
  1247. tmp_error = g_error_new_valist (MC_ERROR, code, format, args);
  1248. va_end (args);
  1249. g_propagate_error (dest, tmp_error);
  1250. }
  1251. }
  1252. /* --------------------------------------------------------------------------------------------- */
  1253. /**
  1254. * Replace existing error in simple way.
  1255. *
  1256. * @param dest error return location
  1257. * @param code error code
  1258. * @param format printf()-style format for error message
  1259. * @param ... parameters for message format
  1260. */
  1261. void
  1262. mc_replace_error (GError **dest, int code, const char *format, ...)
  1263. {
  1264. if (dest != NULL)
  1265. {
  1266. GError *tmp_error;
  1267. va_list args;
  1268. va_start (args, format);
  1269. tmp_error = g_error_new_valist (MC_ERROR, code, format, args);
  1270. va_end (args);
  1271. g_error_free (*dest);
  1272. *dest = NULL;
  1273. g_propagate_error (dest, tmp_error);
  1274. }
  1275. }
  1276. /* --------------------------------------------------------------------------------------------- */
  1277. /**
  1278. * Returns if the given duration has elapsed since the given timestamp,
  1279. * and if it has then updates the timestamp.
  1280. *
  1281. * @param timestamp the last timestamp in microseconds, updated if the given time elapsed
  1282. * @param delay amount of time in microseconds
  1283. * @return TRUE if clock skew detected, FALSE otherwise
  1284. */
  1285. gboolean
  1286. mc_time_elapsed (gint64 *timestamp, gint64 delay)
  1287. {
  1288. gint64 now;
  1289. now = g_get_monotonic_time ();
  1290. if (now >= *timestamp && now < *timestamp + delay)
  1291. return FALSE;
  1292. *timestamp = now;
  1293. return TRUE;
  1294. }
  1295. /* --------------------------------------------------------------------------------------------- */