util.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /*
  2. Various utilities
  3. Copyright (C) 1994-2021
  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 <limits.h>
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <sys/time.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. /*** file scope variables ************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. /*** file scope functions ************************************************************************/
  60. /* --------------------------------------------------------------------------------------------- */
  61. #ifndef HAVE_CHARSET
  62. static inline int
  63. is_7bit_printable (unsigned char c)
  64. {
  65. return (c > 31 && c < 127);
  66. }
  67. #endif
  68. /* --------------------------------------------------------------------------------------------- */
  69. static inline int
  70. is_iso_printable (unsigned char c)
  71. {
  72. return ((c > 31 && c < 127) || c >= 160);
  73. }
  74. /* --------------------------------------------------------------------------------------------- */
  75. static inline int
  76. is_8bit_printable (unsigned char c)
  77. {
  78. /* "Full 8 bits output" doesn't work on xterm */
  79. if (mc_global.tty.xterm_flag)
  80. return is_iso_printable (c);
  81. return (c > 31 && c != 127 && c != 155);
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. static char *
  85. resolve_symlinks (const vfs_path_t * vpath)
  86. {
  87. char *p, *p2;
  88. char *buf, *buf2, *q, *r, c;
  89. struct stat mybuf;
  90. if (vpath->relative)
  91. return NULL;
  92. p = p2 = g_strdup (vfs_path_as_str (vpath));
  93. r = buf = g_malloc (MC_MAXPATHLEN);
  94. buf2 = g_malloc (MC_MAXPATHLEN);
  95. *r++ = PATH_SEP;
  96. *r = '\0';
  97. do
  98. {
  99. q = strchr (p + 1, PATH_SEP);
  100. if (q == NULL)
  101. {
  102. q = strchr (p + 1, '\0');
  103. if (q == p + 1)
  104. break;
  105. }
  106. c = *q;
  107. *q = '\0';
  108. if (mc_lstat (vpath, &mybuf) < 0)
  109. {
  110. MC_PTR_FREE (buf);
  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. MC_PTR_FREE (buf);
  122. goto ret;
  123. }
  124. buf2[len] = '\0';
  125. if (IS_PATH_SEP (*buf2))
  126. strcpy (buf, buf2);
  127. else
  128. strcpy (r, buf2);
  129. }
  130. canonicalize_pathname (buf);
  131. r = strchr (buf, '\0');
  132. if (*r == '\0' || !IS_PATH_SEP (r[-1]))
  133. /* FIXME: this condition is always true because r points to the EOL */
  134. {
  135. *r++ = PATH_SEP;
  136. *r = '\0';
  137. }
  138. *q = c;
  139. p = q;
  140. }
  141. while (c != '\0');
  142. if (*buf == '\0')
  143. strcpy (buf, PATH_SEP_STR);
  144. else if (IS_PATH_SEP (r[-1]) && r != buf + 1)
  145. r[-1] = '\0';
  146. ret:
  147. g_free (buf2);
  148. g_free (p2);
  149. return buf;
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. static gboolean
  153. mc_util_write_backup_content (const char *from_file_name, const char *to_file_name)
  154. {
  155. FILE *backup_fd;
  156. char *contents;
  157. gsize length;
  158. gboolean ret1 = TRUE;
  159. if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
  160. return FALSE;
  161. backup_fd = fopen (to_file_name, "w");
  162. if (backup_fd == NULL)
  163. {
  164. g_free (contents);
  165. return FALSE;
  166. }
  167. if (fwrite ((const void *) contents, 1, length, backup_fd) != length)
  168. ret1 = FALSE;
  169. {
  170. int ret2;
  171. /* cppcheck-suppress redundantAssignment */
  172. ret2 = fflush (backup_fd);
  173. /* cppcheck-suppress redundantAssignment */
  174. ret2 = fclose (backup_fd);
  175. (void) ret2;
  176. }
  177. g_free (contents);
  178. return ret1;
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. /*** public functions ****************************************************************************/
  182. /* --------------------------------------------------------------------------------------------- */
  183. int
  184. is_printable (int c)
  185. {
  186. c &= 0xff;
  187. #ifdef HAVE_CHARSET
  188. /* "Display bits" is ignored, since the user controls the output
  189. by setting the output codepage */
  190. return is_8bit_printable (c);
  191. #else
  192. if (!mc_global.eight_bit_clean)
  193. return is_7bit_printable (c);
  194. if (mc_global.full_eight_bits)
  195. return is_8bit_printable (c);
  196. return is_iso_printable (c);
  197. #endif /* !HAVE_CHARSET */
  198. }
  199. /* --------------------------------------------------------------------------------------------- */
  200. /**
  201. * Quote the filename for the purpose of inserting it into the command
  202. * line. If quote_percent is TRUE, replace "%" with "%%" - the percent is
  203. * processed by the mc command line.
  204. */
  205. char *
  206. name_quote (const char *s, gboolean quote_percent)
  207. {
  208. GString *ret;
  209. ret = g_string_sized_new (64);
  210. if (*s == '-')
  211. g_string_append (ret, "." PATH_SEP_STR);
  212. for (; *s != '\0'; s++)
  213. {
  214. switch (*s)
  215. {
  216. case '%':
  217. if (quote_percent)
  218. g_string_append_c (ret, '%');
  219. break;
  220. case '\'':
  221. case '\\':
  222. case '\r':
  223. case '\n':
  224. case '\t':
  225. case '"':
  226. case ';':
  227. case ' ':
  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. g_string_append_c (ret, '\\');
  244. break;
  245. case '~':
  246. case '#':
  247. if (ret->len == 0)
  248. g_string_append_c (ret, '\\');
  249. break;
  250. default:
  251. break;
  252. }
  253. g_string_append_c (ret, *s);
  254. }
  255. return g_string_free (ret, FALSE);
  256. }
  257. /* --------------------------------------------------------------------------------------------- */
  258. char *
  259. fake_name_quote (const char *s, gboolean quote_percent)
  260. {
  261. (void) quote_percent;
  262. return g_strdup (s);
  263. }
  264. /* --------------------------------------------------------------------------------------------- */
  265. /**
  266. * path_trunc() is the same as str_trunc() but
  267. * it deletes possible password from path for security
  268. * reasons.
  269. */
  270. const char *
  271. path_trunc (const char *path, size_t trunc_len)
  272. {
  273. vfs_path_t *vpath;
  274. char *secure_path;
  275. const char *ret;
  276. vpath = vfs_path_from_str (path);
  277. secure_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
  278. vfs_path_free (vpath, TRUE);
  279. ret = str_trunc (secure_path, trunc_len);
  280. g_free (secure_path);
  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 charactes 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. /* *INDENT-ON* */
  386. static const char *const suffix[] = { "", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL };
  387. static const char *const suffix_lc[] = { "", "k", "m", "g", "t", "p", "e", "z", "y", NULL };
  388. const char *const *sfx = use_si ? suffix_lc : suffix;
  389. int j = 0;
  390. if (len == 0)
  391. len = 9;
  392. #if SIZEOF_UINTMAX_T == 8
  393. /* 20 decimal digits are required to represent 8 bytes */
  394. else if (len > 19)
  395. len = 19;
  396. #else
  397. /* 10 decimal digits are required to represent 4 bytes */
  398. else if (len > 9)
  399. len = 9;
  400. #endif
  401. /*
  402. * recalculate from 1024 base to 1000 base if units>0
  403. * We can't just multiply by 1024 - that might cause overflow
  404. * if uintmax_t type is too small
  405. */
  406. if (use_si)
  407. for (j = 0; j < units; j++)
  408. {
  409. uintmax_t size_remain;
  410. size_remain = ((size % 125) * 1024) / 1000; /* size mod 125, recalculated */
  411. size /= 125; /* 128/125 = 1024/1000 */
  412. size *= 128; /* This will convert size from multiple of 1024 to multiple of 1000 */
  413. size += size_remain; /* Re-add remainder lost by division/multiplication */
  414. }
  415. for (j = units; sfx[j] != NULL; j++)
  416. {
  417. if (size == 0)
  418. {
  419. if (j == units)
  420. {
  421. /* Empty files will print "0" even with minimal width. */
  422. g_snprintf (buffer, len + 1, "%s", "0");
  423. }
  424. else
  425. {
  426. /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
  427. g_snprintf (buffer, len + 1, (len > 1) ? "~%s" : "%s", (j > 1) ? sfx[j - 1] : "B");
  428. }
  429. break;
  430. }
  431. if (size < power10[len - (j > 0 ? 1 : 0)])
  432. {
  433. g_snprintf (buffer, len + 1, "%" PRIuMAX "%s", size, sfx[j]);
  434. break;
  435. }
  436. /* Powers of 1000 or 1024, with rounding. */
  437. if (use_si)
  438. size = (size + 500) / 1000;
  439. else
  440. size = (size + 512) >> 10;
  441. }
  442. }
  443. /* --------------------------------------------------------------------------------------------- */
  444. const char *
  445. string_perm (mode_t mode_bits)
  446. {
  447. static char mode[11];
  448. strcpy (mode, "----------");
  449. if (S_ISDIR (mode_bits))
  450. mode[0] = 'd';
  451. if (S_ISCHR (mode_bits))
  452. mode[0] = 'c';
  453. if (S_ISBLK (mode_bits))
  454. mode[0] = 'b';
  455. if (S_ISLNK (mode_bits))
  456. mode[0] = 'l';
  457. if (S_ISFIFO (mode_bits))
  458. mode[0] = 'p';
  459. if (S_ISNAM (mode_bits))
  460. mode[0] = 'n';
  461. if (S_ISSOCK (mode_bits))
  462. mode[0] = 's';
  463. if (S_ISDOOR (mode_bits))
  464. mode[0] = 'D';
  465. if (ismode (mode_bits, S_IXOTH))
  466. mode[9] = 'x';
  467. if (ismode (mode_bits, S_IWOTH))
  468. mode[8] = 'w';
  469. if (ismode (mode_bits, S_IROTH))
  470. mode[7] = 'r';
  471. if (ismode (mode_bits, S_IXGRP))
  472. mode[6] = 'x';
  473. if (ismode (mode_bits, S_IWGRP))
  474. mode[5] = 'w';
  475. if (ismode (mode_bits, S_IRGRP))
  476. mode[4] = 'r';
  477. if (ismode (mode_bits, S_IXUSR))
  478. mode[3] = 'x';
  479. if (ismode (mode_bits, S_IWUSR))
  480. mode[2] = 'w';
  481. if (ismode (mode_bits, S_IRUSR))
  482. mode[1] = 'r';
  483. #ifdef S_ISUID
  484. if (ismode (mode_bits, S_ISUID))
  485. mode[3] = (mode[3] == 'x') ? 's' : 'S';
  486. #endif /* S_ISUID */
  487. #ifdef S_ISGID
  488. if (ismode (mode_bits, S_ISGID))
  489. mode[6] = (mode[6] == 'x') ? 's' : 'S';
  490. #endif /* S_ISGID */
  491. #ifdef S_ISVTX
  492. if (ismode (mode_bits, S_ISVTX))
  493. mode[9] = (mode[9] == 'x') ? 't' : 'T';
  494. #endif /* S_ISVTX */
  495. return mode;
  496. }
  497. /* --------------------------------------------------------------------------------------------- */
  498. const char *
  499. extension (const char *filename)
  500. {
  501. const char *d;
  502. d = strrchr (filename, '.');
  503. return d != NULL ? d + 1 : "";
  504. }
  505. /* --------------------------------------------------------------------------------------------- */
  506. char *
  507. load_mc_home_file (const char *from, const char *filename, char **allocated_filename,
  508. size_t * length)
  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, length, 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, length, NULL))
  524. {
  525. g_free (hintfile);
  526. hintfile = hintfile_base;
  527. g_file_get_contents (hintfile_base, &data, length, 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 != '\0' && *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 (path_sep == NULL)
  561. return s;
  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 (!IS_PATH_SEP (s[strlen (s) - 1]))
  568. return path_sep + 1;
  569. while (--path_sep > s && !IS_PATH_SEP (*path_sep))
  570. ;
  571. return (path_sep != s) ? path_sep + 1 : s;
  572. }
  573. while (--url_delim > s && !IS_PATH_SEP (*url_delim))
  574. ;
  575. while (--url_delim > s && !IS_PATH_SEP (*url_delim))
  576. ;
  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 != '\0'; str_cnext_char (&su))
  596. if (!whitespace (*su) && *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 != '\0'; 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 ':' or '?') (any other char)
  617. *
  618. * The 256-color and true-color escape sequences should allow either ';' or ':' inside as separator,
  619. * actually, ':' is the more correct according to ECMA-48.
  620. * Some terminal emulators (e.g. xterm, gnome-terminal) support this.
  621. *
  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;
  652. for (new_r = r; *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. break;
  668. default:
  669. break;
  670. }
  671. }
  672. osc_out:
  673. ;
  674. }
  675. /*
  676. * Now we are at the last character of the sequence.
  677. * Skip it unless it's binary 0.
  678. */
  679. if (*r != '\0')
  680. r++;
  681. }
  682. else
  683. {
  684. char *n;
  685. n = str_get_next_char (r);
  686. if (str_isprint (r))
  687. {
  688. memmove (w, r, n - r);
  689. w += n - r;
  690. }
  691. r = n;
  692. }
  693. }
  694. *w = '\0';
  695. return s;
  696. }
  697. /* --------------------------------------------------------------------------------------------- */
  698. enum compression_type
  699. get_compression_type (int fd, const char *name)
  700. {
  701. unsigned char magic[16];
  702. size_t str_len;
  703. /* Read the magic signature */
  704. if (mc_read (fd, (char *) magic, 4) != 4)
  705. return COMPRESSION_NONE;
  706. /* GZIP_MAGIC and OLD_GZIP_MAGIC */
  707. if (magic[0] == 037 && (magic[1] == 0213 || magic[1] == 0236))
  708. return COMPRESSION_GZIP;
  709. /* PKZIP_MAGIC */
  710. if (magic[0] == 0120 && magic[1] == 0113 && magic[2] == 003 && magic[3] == 004)
  711. {
  712. /* Read compression type */
  713. mc_lseek (fd, 8, SEEK_SET);
  714. if (mc_read (fd, (char *) magic, 2) != 2)
  715. return COMPRESSION_NONE;
  716. /* Gzip can handle only deflated (8) or stored (0) files */
  717. if ((magic[0] != 8 && magic[0] != 0) || magic[1] != 0)
  718. return COMPRESSION_NONE;
  719. /* Compatible with gzip */
  720. return COMPRESSION_GZIP;
  721. }
  722. /* PACK_MAGIC and LZH_MAGIC and compress magic */
  723. if (magic[0] == 037 && (magic[1] == 036 || magic[1] == 0240 || magic[1] == 0235))
  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. /* XZ compression magic */
  754. if (magic[0] == 0xFD
  755. && magic[1] == 0x37
  756. && magic[2] == 0x7A && magic[3] == 0x58 && magic[4] == 0x5A && magic[5] == 0x00)
  757. return COMPRESSION_XZ;
  758. if (magic[0] == 0x28 && magic[1] == 0xB5 && magic[2] == 0x2F && magic[3] == 0xFD)
  759. return COMPRESSION_ZSTD;
  760. str_len = strlen (name);
  761. /* HACK: we must belive to extension of LZMA file :) ... */
  762. if ((str_len > 5 && strcmp (&name[str_len - 5], ".lzma") == 0) ||
  763. (str_len > 4 && strcmp (&name[str_len - 4], ".tlz") == 0))
  764. return COMPRESSION_LZMA;
  765. return COMPRESSION_NONE;
  766. }
  767. /* --------------------------------------------------------------------------------------------- */
  768. const char *
  769. decompress_extension (int type)
  770. {
  771. switch (type)
  772. {
  773. case COMPRESSION_GZIP:
  774. return "/ugz" VFS_PATH_URL_DELIMITER;
  775. case COMPRESSION_BZIP:
  776. return "/ubz" VFS_PATH_URL_DELIMITER;
  777. case COMPRESSION_BZIP2:
  778. return "/ubz2" VFS_PATH_URL_DELIMITER;
  779. case COMPRESSION_LZIP:
  780. return "/ulz" VFS_PATH_URL_DELIMITER;
  781. case COMPRESSION_LZ4:
  782. return "/ulz4" VFS_PATH_URL_DELIMITER;
  783. case COMPRESSION_LZMA:
  784. return "/ulzma" VFS_PATH_URL_DELIMITER;
  785. case COMPRESSION_XZ:
  786. return "/uxz" VFS_PATH_URL_DELIMITER;
  787. case COMPRESSION_ZSTD:
  788. return "/uzst" VFS_PATH_URL_DELIMITER;
  789. default:
  790. break;
  791. }
  792. /* Should never reach this place */
  793. fprintf (stderr, "Fatal: decompress_extension called with an unknown argument\n");
  794. return 0;
  795. }
  796. /* --------------------------------------------------------------------------------------------- */
  797. void
  798. wipe_password (char *passwd)
  799. {
  800. if (passwd != NULL)
  801. {
  802. char *p;
  803. for (p = passwd; *p != '\0'; p++)
  804. *p = '\0';
  805. g_free (passwd);
  806. }
  807. }
  808. /* --------------------------------------------------------------------------------------------- */
  809. /**
  810. * Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key
  811. *
  812. * @param p pointer to string
  813. *
  814. * @return newly allocated string
  815. */
  816. char *
  817. convert_controls (const char *p)
  818. {
  819. char *valcopy;
  820. char *q;
  821. valcopy = g_strdup (p);
  822. /* Parse the escape special character */
  823. for (q = valcopy; *p != '\0';)
  824. switch (*p)
  825. {
  826. case '\\':
  827. p++;
  828. if (*p == 'e' || *p == 'E')
  829. {
  830. p++;
  831. *q++ = ESC_CHAR;
  832. }
  833. break;
  834. case '^':
  835. p++;
  836. if (*p == '^')
  837. *q++ = *p++;
  838. else
  839. {
  840. char c;
  841. c = *p | 0x20;
  842. if (c >= 'a' && c <= 'z')
  843. {
  844. *q++ = c - 'a' + 1;
  845. p++;
  846. }
  847. else if (*p != '\0')
  848. p++;
  849. }
  850. break;
  851. default:
  852. *q++ = *p++;
  853. }
  854. *q = '\0';
  855. return valcopy;
  856. }
  857. /* --------------------------------------------------------------------------------------------- */
  858. /**
  859. * Finds out a relative path from first to second, i.e. goes as many ..
  860. * as needed up in first and then goes down using second
  861. */
  862. char *
  863. diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
  864. {
  865. int j, prevlen = -1, currlen;
  866. char *my_first = NULL, *my_second = NULL;
  867. char *buf = NULL;
  868. my_first = resolve_symlinks (vpath1);
  869. if (my_first == NULL)
  870. goto ret;
  871. my_second = resolve_symlinks (vpath2);
  872. if (my_second == NULL)
  873. goto ret;
  874. for (j = 0; j < 2; j++)
  875. {
  876. char *p, *q;
  877. int i;
  878. p = my_first;
  879. q = my_second;
  880. while (TRUE)
  881. {
  882. char *r, *s;
  883. ptrdiff_t len;
  884. r = strchr (p, PATH_SEP);
  885. if (r == NULL)
  886. break;
  887. s = strchr (q, PATH_SEP);
  888. if (s == NULL)
  889. break;
  890. len = r - p;
  891. if (len != (s - q) || strncmp (p, q, (size_t) len) != 0)
  892. break;
  893. p = r + 1;
  894. q = s + 1;
  895. }
  896. p--;
  897. for (i = 0; (p = strchr (p + 1, PATH_SEP)) != NULL; i++)
  898. ;
  899. currlen = (i + 1) * 3 + strlen (q) + 1;
  900. if (j != 0)
  901. {
  902. if (currlen < prevlen)
  903. g_free (buf);
  904. else
  905. goto ret;
  906. }
  907. p = buf = g_malloc (currlen);
  908. prevlen = currlen;
  909. for (; i >= 0; i--, p += 3)
  910. strcpy (p, "../");
  911. strcpy (p, q);
  912. }
  913. ret:
  914. g_free (my_first);
  915. g_free (my_second);
  916. return buf;
  917. }
  918. /* --------------------------------------------------------------------------------------------- */
  919. /**
  920. * Append text to GList, remove all entries with the same text
  921. */
  922. GList *
  923. list_append_unique (GList * list, char *text)
  924. {
  925. GList *lc_link;
  926. /*
  927. * Go to the last position and traverse the list backwards
  928. * starting from the second last entry to make sure that we
  929. * are not removing the current link.
  930. */
  931. list = g_list_append (list, text);
  932. list = g_list_last (list);
  933. lc_link = g_list_previous (list);
  934. while (lc_link != NULL)
  935. {
  936. GList *newlink;
  937. newlink = g_list_previous (lc_link);
  938. if (strcmp ((char *) lc_link->data, text) == 0)
  939. {
  940. GList *tmp;
  941. g_free (lc_link->data);
  942. tmp = g_list_remove_link (list, lc_link);
  943. (void) tmp;
  944. g_list_free_1 (lc_link);
  945. }
  946. lc_link = newlink;
  947. }
  948. return list;
  949. }
  950. /* --------------------------------------------------------------------------------------------- */
  951. /**
  952. * Read and restore position for the given filename.
  953. * If there is no stored data, return line 1 and col 0.
  954. */
  955. void
  956. load_file_position (const vfs_path_t * filename_vpath, long *line, long *column, off_t * offset,
  957. GArray ** bookmarks)
  958. {
  959. char *fn;
  960. FILE *f;
  961. char buf[MC_MAXPATHLEN + 100];
  962. const size_t len = vfs_path_len (filename_vpath);
  963. /* defaults */
  964. *line = 1;
  965. *column = 0;
  966. *offset = 0;
  967. /* open file with positions */
  968. fn = mc_config_get_full_path (MC_FILEPOS_FILE);
  969. f = fopen (fn, "r");
  970. g_free (fn);
  971. if (f == NULL)
  972. return;
  973. /* prepare array for serialized bookmarks */
  974. if (bookmarks != NULL)
  975. *bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS);
  976. while (fgets (buf, sizeof (buf), f) != NULL)
  977. {
  978. const char *p;
  979. gchar **pos_tokens;
  980. /* check if the filename matches the beginning of string */
  981. if (strncmp (buf, vfs_path_as_str (filename_vpath), len) != 0)
  982. continue;
  983. /* followed by single space */
  984. if (buf[len] != ' ')
  985. continue;
  986. /* and string without spaces */
  987. p = &buf[len + 1];
  988. if (strchr (p, ' ') != NULL)
  989. continue;
  990. pos_tokens = g_strsplit (p, ";", 3 + MAX_SAVED_BOOKMARKS);
  991. if (pos_tokens[0] == NULL)
  992. {
  993. *line = 1;
  994. *column = 0;
  995. *offset = 0;
  996. }
  997. else
  998. {
  999. *line = strtol (pos_tokens[0], NULL, 10);
  1000. if (pos_tokens[1] == NULL)
  1001. {
  1002. *column = 0;
  1003. *offset = 0;
  1004. }
  1005. else
  1006. {
  1007. *column = strtol (pos_tokens[1], NULL, 10);
  1008. if (pos_tokens[2] == NULL)
  1009. *offset = 0;
  1010. else if (bookmarks != NULL)
  1011. {
  1012. size_t i;
  1013. *offset = (off_t) g_ascii_strtoll (pos_tokens[2], NULL, 10);
  1014. for (i = 0; i < MAX_SAVED_BOOKMARKS && pos_tokens[3 + i] != NULL; i++)
  1015. {
  1016. size_t val;
  1017. val = strtoul (pos_tokens[3 + i], NULL, 10);
  1018. g_array_append_val (*bookmarks, val);
  1019. }
  1020. }
  1021. }
  1022. }
  1023. g_strfreev (pos_tokens);
  1024. }
  1025. fclose (f);
  1026. }
  1027. /* --------------------------------------------------------------------------------------------- */
  1028. /**
  1029. * Save position for the given file
  1030. */
  1031. void
  1032. save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
  1033. GArray * bookmarks)
  1034. {
  1035. static size_t filepos_max_saved_entries = 0;
  1036. char *fn, *tmp_fn;
  1037. FILE *f, *tmp_f;
  1038. char buf[MC_MAXPATHLEN + 100];
  1039. size_t i;
  1040. const size_t len = vfs_path_len (filename_vpath);
  1041. gboolean src_error = FALSE;
  1042. if (filepos_max_saved_entries == 0)
  1043. filepos_max_saved_entries = mc_config_get_int (mc_global.main_config, CONFIG_APP_SECTION,
  1044. "filepos_max_saved_entries", 1024);
  1045. fn = mc_config_get_full_path (MC_FILEPOS_FILE);
  1046. if (fn == NULL)
  1047. goto early_error;
  1048. mc_util_make_backup_if_possible (fn, TMP_SUFFIX);
  1049. /* open file */
  1050. f = fopen (fn, "w");
  1051. if (f == NULL)
  1052. goto open_target_error;
  1053. tmp_fn = g_strdup_printf ("%s" TMP_SUFFIX, fn);
  1054. tmp_f = fopen (tmp_fn, "r");
  1055. if (tmp_f == NULL)
  1056. {
  1057. src_error = TRUE;
  1058. goto open_source_error;
  1059. }
  1060. /* put the new record */
  1061. if (line != 1 || column != 0 || bookmarks != NULL)
  1062. {
  1063. if (fprintf
  1064. (f, "%s %ld;%ld;%" PRIuMAX, vfs_path_as_str (filename_vpath), line, column,
  1065. (uintmax_t) offset) < 0)
  1066. goto write_position_error;
  1067. if (bookmarks != NULL)
  1068. for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++)
  1069. if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0)
  1070. goto write_position_error;
  1071. if (fprintf (f, "\n") < 0)
  1072. goto write_position_error;
  1073. }
  1074. i = 1;
  1075. while (fgets (buf, sizeof (buf), tmp_f) != NULL)
  1076. {
  1077. if (buf[len] == ' ' && strncmp (buf, vfs_path_as_str (filename_vpath), len) == 0
  1078. && strchr (&buf[len + 1], ' ') == NULL)
  1079. continue;
  1080. fprintf (f, "%s", buf);
  1081. if (++i > filepos_max_saved_entries)
  1082. break;
  1083. }
  1084. write_position_error:
  1085. fclose (tmp_f);
  1086. open_source_error:
  1087. g_free (tmp_fn);
  1088. fclose (f);
  1089. if (src_error)
  1090. mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
  1091. else
  1092. mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
  1093. open_target_error:
  1094. g_free (fn);
  1095. early_error:
  1096. if (bookmarks != NULL)
  1097. g_array_free (bookmarks, TRUE);
  1098. }
  1099. /* --------------------------------------------------------------------------------------------- */
  1100. extern int
  1101. ascii_alpha_to_cntrl (int ch)
  1102. {
  1103. if ((ch >= ASCII_A && ch <= ASCII_Z) || (ch >= ASCII_a && ch <= ASCII_z))
  1104. ch &= 0x1f;
  1105. return ch;
  1106. }
  1107. /* --------------------------------------------------------------------------------------------- */
  1108. const char *
  1109. Q_ (const char *s)
  1110. {
  1111. const char *result, *sep;
  1112. result = _(s);
  1113. sep = strchr (result, '|');
  1114. return sep != NULL ? sep + 1 : result;
  1115. }
  1116. /* --------------------------------------------------------------------------------------------- */
  1117. gboolean
  1118. mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffix)
  1119. {
  1120. struct stat stat_buf;
  1121. char *backup_path;
  1122. gboolean ret;
  1123. if (!exist_file (file_name))
  1124. return FALSE;
  1125. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1126. if (backup_path == NULL)
  1127. return FALSE;
  1128. ret = mc_util_write_backup_content (file_name, backup_path);
  1129. if (ret)
  1130. {
  1131. /* Backup file will have same ownership with main file. */
  1132. if (stat (file_name, &stat_buf) == 0)
  1133. chmod (backup_path, stat_buf.st_mode);
  1134. else
  1135. chmod (backup_path, S_IRUSR | S_IWUSR);
  1136. }
  1137. g_free (backup_path);
  1138. return ret;
  1139. }
  1140. /* --------------------------------------------------------------------------------------------- */
  1141. gboolean
  1142. mc_util_restore_from_backup_if_possible (const char *file_name, const char *backup_suffix)
  1143. {
  1144. gboolean ret;
  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. ret = mc_util_write_backup_content (backup_path, file_name);
  1150. g_free (backup_path);
  1151. return ret;
  1152. }
  1153. /* --------------------------------------------------------------------------------------------- */
  1154. gboolean
  1155. mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix)
  1156. {
  1157. char *backup_path;
  1158. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1159. if (backup_path == NULL)
  1160. return FALSE;
  1161. if (exist_file (backup_path))
  1162. {
  1163. vfs_path_t *vpath;
  1164. vpath = vfs_path_from_str (backup_path);
  1165. mc_unlink (vpath);
  1166. vfs_path_free (vpath, TRUE);
  1167. }
  1168. g_free (backup_path);
  1169. return TRUE;
  1170. }
  1171. /* --------------------------------------------------------------------------------------------- */
  1172. /**
  1173. * partly taken from dcigettext.c, returns "" for default locale
  1174. * value should be freed by calling function g_free()
  1175. */
  1176. char *
  1177. guess_message_value (void)
  1178. {
  1179. static const char *const var[] = {
  1180. /* Setting of LC_ALL overwrites all other. */
  1181. /* Do not use LANGUAGE for check user locale and drowing hints */
  1182. "LC_ALL",
  1183. /* Next comes the name of the desired category. */
  1184. "LC_MESSAGES",
  1185. /* Last possibility is the LANG environment variable. */
  1186. "LANG",
  1187. /* NULL exit loops */
  1188. NULL
  1189. };
  1190. size_t i;
  1191. const char *locale = NULL;
  1192. for (i = 0; var[i] != NULL; i++)
  1193. {
  1194. locale = getenv (var[i]);
  1195. if (locale != NULL && locale[0] != '\0')
  1196. break;
  1197. }
  1198. if (locale == NULL)
  1199. locale = "";
  1200. return g_strdup (locale);
  1201. }
  1202. /* --------------------------------------------------------------------------------------------- */
  1203. /**
  1204. * The "profile root" is the tree under which all of MC's user data &
  1205. * settings are stored.
  1206. *
  1207. * It defaults to the user's home dir. The user may override this default
  1208. * with the environment variable $MC_PROFILE_ROOT.
  1209. */
  1210. const char *
  1211. mc_get_profile_root (void)
  1212. {
  1213. static const char *profile_root = NULL;
  1214. if (profile_root == NULL)
  1215. {
  1216. profile_root = g_getenv ("MC_PROFILE_ROOT");
  1217. if (profile_root == NULL || *profile_root == '\0')
  1218. profile_root = mc_config_get_home_dir ();
  1219. }
  1220. return profile_root;
  1221. }
  1222. /* --------------------------------------------------------------------------------------------- */
  1223. /**
  1224. * Propagate error in simple way.
  1225. *
  1226. * @param dest error return location
  1227. * @param code error code
  1228. * @param format printf()-style format for error message
  1229. * @param ... parameters for message format
  1230. */
  1231. void
  1232. mc_propagate_error (GError ** dest, int code, const char *format, ...)
  1233. {
  1234. if (dest != NULL && *dest == NULL)
  1235. {
  1236. GError *tmp_error;
  1237. va_list args;
  1238. va_start (args, format);
  1239. tmp_error = g_error_new_valist (MC_ERROR, code, format, args);
  1240. va_end (args);
  1241. g_propagate_error (dest, tmp_error);
  1242. }
  1243. }
  1244. /* --------------------------------------------------------------------------------------------- */
  1245. /**
  1246. * Replace existing error in simple way.
  1247. *
  1248. * @param dest error return location
  1249. * @param code error code
  1250. * @param format printf()-style format for error message
  1251. * @param ... parameters for message format
  1252. */
  1253. void
  1254. mc_replace_error (GError ** dest, int code, const char *format, ...)
  1255. {
  1256. if (dest != NULL)
  1257. {
  1258. GError *tmp_error;
  1259. va_list args;
  1260. va_start (args, format);
  1261. tmp_error = g_error_new_valist (MC_ERROR, code, format, args);
  1262. va_end (args);
  1263. g_error_free (*dest);
  1264. *dest = NULL;
  1265. g_propagate_error (dest, tmp_error);
  1266. }
  1267. }
  1268. /* --------------------------------------------------------------------------------------------- */
  1269. /**
  1270. * Returns if the given duration has elapsed since the given timestamp,
  1271. * and if it has then updates the timestamp.
  1272. *
  1273. * @param timestamp the last timestamp in microseconds, updated if the given time elapsed
  1274. * @param delay amount of time in microseconds
  1275. * @return TRUE if clock skew detected, FALSE otherwise
  1276. */
  1277. gboolean
  1278. mc_time_elapsed (gint64 * timestamp, gint64 delay)
  1279. {
  1280. gint64 now;
  1281. now = g_get_real_time ();
  1282. if (now >= *timestamp && now < *timestamp + delay)
  1283. return FALSE;
  1284. *timestamp = now;
  1285. return TRUE;
  1286. }
  1287. /* --------------------------------------------------------------------------------------------- */