util.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  1. /*
  2. Various utilities
  3. Copyright (C) 1994-2017
  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. #include "lib/timer.h"
  45. /*** global variables ****************************************************************************/
  46. /*** file scope macro definitions ****************************************************************/
  47. #define ismode(n,m) ((n & m) == m)
  48. /* Number of attempts to create a temporary file */
  49. #ifndef TMP_MAX
  50. #define TMP_MAX 16384
  51. #endif /* !TMP_MAX */
  52. #define TMP_SUFFIX ".tmp"
  53. #define ASCII_A (0x40 + 1)
  54. #define ASCII_Z (0x40 + 26)
  55. #define ASCII_a (0x60 + 1)
  56. #define ASCII_z (0x60 + 26)
  57. /*** file scope type declarations ****************************************************************/
  58. /*** file scope variables ************************************************************************/
  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)
  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. {
  196. return is_8bit_printable (c);
  197. }
  198. else
  199. return is_iso_printable (c);
  200. #endif /* !HAVE_CHARSET */
  201. }
  202. /* --------------------------------------------------------------------------------------------- */
  203. /**
  204. * Quote the filename for the purpose of inserting it into the command
  205. * line. If quote_percent is TRUE, replace "%" with "%%" - the percent is
  206. * processed by the mc command line.
  207. */
  208. char *
  209. name_quote (const char *s, gboolean quote_percent)
  210. {
  211. GString *ret;
  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, FALSE);
  259. }
  260. /* --------------------------------------------------------------------------------------------- */
  261. char *
  262. fake_name_quote (const char *s, gboolean quote_percent)
  263. {
  264. (void) quote_percent;
  265. return 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. char *secure_path;
  278. const char *ret;
  279. vpath = vfs_path_from_str (path);
  280. secure_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
  281. vfs_path_free (vpath);
  282. ret = str_trunc (secure_path, trunc_len);
  283. g_free (secure_path);
  284. return ret;
  285. }
  286. /* --------------------------------------------------------------------------------------------- */
  287. const char *
  288. size_trunc (uintmax_t size, gboolean use_si)
  289. {
  290. static char x[BUF_TINY];
  291. uintmax_t divisor = 1;
  292. const char *xtra = _("B");
  293. if (size > 999999999UL)
  294. {
  295. divisor = use_si ? 1000 : 1024;
  296. xtra = use_si ? _("kB") : _("KiB");
  297. if (size / divisor > 999999999UL)
  298. {
  299. divisor = use_si ? (1000 * 1000) : (1024 * 1024);
  300. xtra = use_si ? _("MB") : _("MiB");
  301. if (size / divisor > 999999999UL)
  302. {
  303. divisor = use_si ? (1000 * 1000 * 1000) : (1024 * 1024 * 1024);
  304. xtra = use_si ? _("GB") : _("GiB");
  305. }
  306. }
  307. }
  308. g_snprintf (x, sizeof (x), "%.0f %s", 1.0 * size / divisor, xtra);
  309. return x;
  310. }
  311. /* --------------------------------------------------------------------------------------------- */
  312. const char *
  313. size_trunc_sep (uintmax_t size, gboolean use_si)
  314. {
  315. static char x[60];
  316. int count;
  317. const char *p, *y;
  318. char *d;
  319. p = y = size_trunc (size, use_si);
  320. p += strlen (p) - 1;
  321. d = x + sizeof (x) - 1;
  322. *d-- = '\0';
  323. /* @size format is "size unit", i.e. "[digits][space][letters]".
  324. Copy all charactes after digits. */
  325. while (p >= y && !g_ascii_isdigit (*p))
  326. *d-- = *p--;
  327. for (count = 0; p >= y; count++)
  328. {
  329. if (count == 3)
  330. {
  331. *d-- = ',';
  332. count = 0;
  333. }
  334. *d-- = *p--;
  335. }
  336. d++;
  337. if (*d == ',')
  338. d++;
  339. return d;
  340. }
  341. /* --------------------------------------------------------------------------------------------- */
  342. /**
  343. * Print file SIZE to BUFFER, but don't exceed LEN characters,
  344. * not including trailing 0. BUFFER should be at least LEN+1 long.
  345. * This function is called for every file on panels, so avoid
  346. * floating point by any means.
  347. *
  348. * Units: size units (filesystem sizes are 1K blocks)
  349. * 0=bytes, 1=Kbytes, 2=Mbytes, etc.
  350. */
  351. void
  352. size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gboolean use_si)
  353. {
  354. /* Avoid taking power for every file. */
  355. /* *INDENT-OFF* */
  356. static const uintmax_t power10[] = {
  357. /* we hope that size of uintmax_t is 4 bytes at least */
  358. 1ULL,
  359. 10ULL,
  360. 100ULL,
  361. 1000ULL,
  362. 10000ULL,
  363. 100000ULL,
  364. 1000000ULL,
  365. 10000000ULL,
  366. 100000000ULL,
  367. 1000000000ULL
  368. /* maximum value of uintmax_t (in case of 4 bytes) is
  369. 4294967295
  370. */
  371. #if SIZEOF_UINTMAX_T == 8
  372. ,
  373. 10000000000ULL,
  374. 100000000000ULL,
  375. 1000000000000ULL,
  376. 10000000000000ULL,
  377. 100000000000000ULL,
  378. 1000000000000000ULL,
  379. 10000000000000000ULL,
  380. 100000000000000000ULL,
  381. 1000000000000000000ULL,
  382. 10000000000000000000ULL
  383. /* maximum value of uintmax_t (in case of 8 bytes) is
  384. 18447644073710439615
  385. */
  386. #endif
  387. };
  388. /* *INDENT-ON* */
  389. static const char *const suffix[] = { "", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL };
  390. static const char *const suffix_lc[] = { "", "k", "m", "g", "t", "p", "e", "z", "y", NULL };
  391. const char *const *sfx = use_si ? suffix_lc : suffix;
  392. int j = 0;
  393. if (len == 0)
  394. len = 9;
  395. #if SIZEOF_UINTMAX_T == 8
  396. /* 20 decimal digits are required to represent 8 bytes */
  397. else if (len > 19)
  398. len = 19;
  399. #else
  400. /* 10 decimal digits are required to represent 4 bytes */
  401. else if (len > 9)
  402. len = 9;
  403. #endif
  404. /*
  405. * recalculate from 1024 base to 1000 base if units>0
  406. * We can't just multiply by 1024 - that might cause overflow
  407. * if uintmax_t type is too small
  408. */
  409. if (use_si)
  410. for (j = 0; j < units; j++)
  411. {
  412. uintmax_t size_remain;
  413. size_remain = ((size % 125) * 1024) / 1000; /* size mod 125, recalculated */
  414. size /= 125; /* 128/125 = 1024/1000 */
  415. size *= 128; /* This will convert size from multiple of 1024 to multiple of 1000 */
  416. size += size_remain; /* Re-add remainder lost by division/multiplication */
  417. }
  418. for (j = units; sfx[j] != NULL; j++)
  419. {
  420. if (size == 0)
  421. {
  422. if (j == units)
  423. {
  424. /* Empty files will print "0" even with minimal width. */
  425. g_snprintf (buffer, len + 1, "%s", "0");
  426. }
  427. else
  428. {
  429. /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
  430. g_snprintf (buffer, len + 1, (len > 1) ? "~%s" : "%s", (j > 1) ? sfx[j - 1] : "B");
  431. }
  432. break;
  433. }
  434. if (size < power10[len - (j > 0 ? 1 : 0)])
  435. {
  436. g_snprintf (buffer, len + 1, "%" PRIuMAX "%s", size, sfx[j]);
  437. break;
  438. }
  439. /* Powers of 1000 or 1024, with rounding. */
  440. if (use_si)
  441. size = (size + 500) / 1000;
  442. else
  443. size = (size + 512) >> 10;
  444. }
  445. }
  446. /* --------------------------------------------------------------------------------------------- */
  447. const char *
  448. string_perm (mode_t mode_bits)
  449. {
  450. static char mode[11];
  451. strcpy (mode, "----------");
  452. if (S_ISDIR (mode_bits))
  453. mode[0] = 'd';
  454. if (S_ISCHR (mode_bits))
  455. mode[0] = 'c';
  456. if (S_ISBLK (mode_bits))
  457. mode[0] = 'b';
  458. if (S_ISLNK (mode_bits))
  459. mode[0] = 'l';
  460. if (S_ISFIFO (mode_bits))
  461. mode[0] = 'p';
  462. if (S_ISNAM (mode_bits))
  463. mode[0] = 'n';
  464. if (S_ISSOCK (mode_bits))
  465. mode[0] = 's';
  466. if (S_ISDOOR (mode_bits))
  467. mode[0] = 'D';
  468. if (ismode (mode_bits, S_IXOTH))
  469. mode[9] = 'x';
  470. if (ismode (mode_bits, S_IWOTH))
  471. mode[8] = 'w';
  472. if (ismode (mode_bits, S_IROTH))
  473. mode[7] = 'r';
  474. if (ismode (mode_bits, S_IXGRP))
  475. mode[6] = 'x';
  476. if (ismode (mode_bits, S_IWGRP))
  477. mode[5] = 'w';
  478. if (ismode (mode_bits, S_IRGRP))
  479. mode[4] = 'r';
  480. if (ismode (mode_bits, S_IXUSR))
  481. mode[3] = 'x';
  482. if (ismode (mode_bits, S_IWUSR))
  483. mode[2] = 'w';
  484. if (ismode (mode_bits, S_IRUSR))
  485. mode[1] = 'r';
  486. #ifdef S_ISUID
  487. if (ismode (mode_bits, S_ISUID))
  488. mode[3] = (mode[3] == 'x') ? 's' : 'S';
  489. #endif /* S_ISUID */
  490. #ifdef S_ISGID
  491. if (ismode (mode_bits, S_ISGID))
  492. mode[6] = (mode[6] == 'x') ? 's' : 'S';
  493. #endif /* S_ISGID */
  494. #ifdef S_ISVTX
  495. if (ismode (mode_bits, S_ISVTX))
  496. mode[9] = (mode[9] == 'x') ? 't' : 'T';
  497. #endif /* S_ISVTX */
  498. return mode;
  499. }
  500. /* --------------------------------------------------------------------------------------------- */
  501. const char *
  502. extension (const char *filename)
  503. {
  504. const char *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 && *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 != NULL) ? path_sep + 1 : s;
  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 = r;
  654. for (; *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. default:
  670. break;
  671. }
  672. }
  673. osc_out:
  674. ;
  675. }
  676. /*
  677. * Now we are at the last character of the sequence.
  678. * Skip it unless it's binary 0.
  679. */
  680. if (*r != '\0')
  681. r++;
  682. }
  683. else
  684. {
  685. char *n;
  686. n = str_get_next_char (r);
  687. if (str_isprint (r))
  688. {
  689. memmove (w, r, n - r);
  690. w += n - r;
  691. }
  692. r = n;
  693. }
  694. }
  695. *w = '\0';
  696. return s;
  697. }
  698. /* --------------------------------------------------------------------------------------------- */
  699. enum compression_type
  700. get_compression_type (int fd, const char *name)
  701. {
  702. unsigned char magic[16];
  703. size_t str_len;
  704. /* Read the magic signature */
  705. if (mc_read (fd, (char *) magic, 4) != 4)
  706. return COMPRESSION_NONE;
  707. /* GZIP_MAGIC and OLD_GZIP_MAGIC */
  708. if (magic[0] == 037 && (magic[1] == 0213 || magic[1] == 0236))
  709. {
  710. return COMPRESSION_GZIP;
  711. }
  712. /* PKZIP_MAGIC */
  713. if (magic[0] == 0120 && magic[1] == 0113 && magic[2] == 003 && magic[3] == 004)
  714. {
  715. /* Read compression type */
  716. mc_lseek (fd, 8, SEEK_SET);
  717. if (mc_read (fd, (char *) magic, 2) != 2)
  718. return COMPRESSION_NONE;
  719. /* Gzip can handle only deflated (8) or stored (0) files */
  720. if ((magic[0] != 8 && magic[0] != 0) || magic[1] != 0)
  721. return COMPRESSION_NONE;
  722. /* Compatible with gzip */
  723. return COMPRESSION_GZIP;
  724. }
  725. /* PACK_MAGIC and LZH_MAGIC and compress magic */
  726. if (magic[0] == 037 && (magic[1] == 036 || magic[1] == 0240 || magic[1] == 0235))
  727. {
  728. /* Compatible with gzip */
  729. return COMPRESSION_GZIP;
  730. }
  731. /* BZIP and BZIP2 files */
  732. if ((magic[0] == 'B') && (magic[1] == 'Z') && (magic[3] >= '1') && (magic[3] <= '9'))
  733. {
  734. switch (magic[2])
  735. {
  736. case '0':
  737. return COMPRESSION_BZIP;
  738. case 'h':
  739. return COMPRESSION_BZIP2;
  740. default:
  741. break;
  742. }
  743. }
  744. /* LZ4 format - v1.5.0 - 0x184D2204 (little endian) */
  745. if (magic[0] == 0x04 && magic[1] == 0x22 && magic[2] == 0x4d && magic[3] == 0x18)
  746. return COMPRESSION_LZ4;
  747. if (mc_read (fd, (char *) magic + 4, 2) != 2)
  748. return COMPRESSION_NONE;
  749. /* LZIP files */
  750. if (magic[0] == 'L'
  751. && magic[1] == 'Z'
  752. && magic[2] == 'I' && magic[3] == 'P' && (magic[4] == 0x00 || magic[4] == 0x01))
  753. return COMPRESSION_LZIP;
  754. /* Support for LZMA (only utils format with magic in header).
  755. * This is the default format of LZMA utils 4.32.1 and later. */
  756. if (magic[0] == 0xFF
  757. && magic[1] == 'L'
  758. && magic[2] == 'Z' && magic[3] == 'M' && magic[4] == 'A' && magic[5] == 0x00)
  759. return COMPRESSION_LZMA;
  760. /* XZ compression magic */
  761. if (magic[0] == 0xFD
  762. && magic[1] == 0x37
  763. && magic[2] == 0x7A && magic[3] == 0x58 && magic[4] == 0x5A && magic[5] == 0x00)
  764. return COMPRESSION_XZ;
  765. str_len = strlen (name);
  766. /* HACK: we must belive to extension of LZMA file :) ... */
  767. if ((str_len > 5 && strcmp (&name[str_len - 5], ".lzma") == 0) ||
  768. (str_len > 4 && strcmp (&name[str_len - 4], ".tlz") == 0))
  769. return COMPRESSION_LZMA;
  770. return COMPRESSION_NONE;
  771. }
  772. /* --------------------------------------------------------------------------------------------- */
  773. const char *
  774. decompress_extension (int type)
  775. {
  776. switch (type)
  777. {
  778. case COMPRESSION_GZIP:
  779. return "/ugz" VFS_PATH_URL_DELIMITER;
  780. case COMPRESSION_BZIP:
  781. return "/ubz" VFS_PATH_URL_DELIMITER;
  782. case COMPRESSION_BZIP2:
  783. return "/ubz2" VFS_PATH_URL_DELIMITER;
  784. case COMPRESSION_LZIP:
  785. return "/ulz" VFS_PATH_URL_DELIMITER;
  786. case COMPRESSION_LZ4:
  787. return "/ulz4" VFS_PATH_URL_DELIMITER;
  788. case COMPRESSION_LZMA:
  789. return "/ulzma" VFS_PATH_URL_DELIMITER;
  790. case COMPRESSION_XZ:
  791. return "/uxz" VFS_PATH_URL_DELIMITER;
  792. default:
  793. break;
  794. }
  795. /* Should never reach this place */
  796. fprintf (stderr, "Fatal: decompress_extension called with an unknown argument\n");
  797. return 0;
  798. }
  799. /* --------------------------------------------------------------------------------------------- */
  800. void
  801. wipe_password (char *passwd)
  802. {
  803. char *p = passwd;
  804. if (!p)
  805. return;
  806. for (; *p; p++)
  807. *p = 0;
  808. g_free (passwd);
  809. }
  810. /* --------------------------------------------------------------------------------------------- */
  811. /**
  812. * Convert "\E" -> esc character and ^x to control-x key and ^^ to ^ key
  813. *
  814. * @param p pointer to string
  815. *
  816. * @return newly allocated string
  817. */
  818. char *
  819. convert_controls (const char *p)
  820. {
  821. char *valcopy = g_strdup (p);
  822. char *q;
  823. /* Parse the escape special character */
  824. for (q = valcopy; *p;)
  825. {
  826. if (*p == '\\')
  827. {
  828. p++;
  829. if ((*p == 'e') || (*p == 'E'))
  830. {
  831. p++;
  832. *q++ = ESC_CHAR;
  833. }
  834. }
  835. else
  836. {
  837. if (*p == '^')
  838. {
  839. p++;
  840. if (*p == '^')
  841. *q++ = *p++;
  842. else
  843. {
  844. char c = (*p | 0x20);
  845. if (c >= 'a' && c <= 'z')
  846. {
  847. *q++ = c - 'a' + 1;
  848. p++;
  849. }
  850. else if (*p)
  851. p++;
  852. }
  853. }
  854. else
  855. *q++ = *p++;
  856. }
  857. }
  858. *q = 0;
  859. return valcopy;
  860. }
  861. /* --------------------------------------------------------------------------------------------- */
  862. /**
  863. * Finds out a relative path from first to second, i.e. goes as many ..
  864. * as needed up in first and then goes down using second
  865. */
  866. char *
  867. diff_two_paths (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
  868. {
  869. int j, prevlen = -1, currlen;
  870. char *my_first = NULL, *my_second = NULL;
  871. char *buf = NULL;
  872. my_first = resolve_symlinks (vpath1);
  873. if (my_first == NULL)
  874. goto ret;
  875. my_second = resolve_symlinks (vpath2);
  876. if (my_second == NULL)
  877. goto ret;
  878. for (j = 0; j < 2; j++)
  879. {
  880. char *p, *q;
  881. int i;
  882. p = my_first;
  883. q = my_second;
  884. while (TRUE)
  885. {
  886. char *r, *s;
  887. ptrdiff_t len;
  888. r = strchr (p, PATH_SEP);
  889. if (r == NULL)
  890. break;
  891. s = strchr (q, PATH_SEP);
  892. if (s == NULL)
  893. break;
  894. len = r - p;
  895. if (len != (s - q) || strncmp (p, q, (size_t) len) != 0)
  896. break;
  897. p = r + 1;
  898. q = s + 1;
  899. }
  900. p--;
  901. for (i = 0; (p = strchr (p + 1, PATH_SEP)) != NULL; i++)
  902. ;
  903. currlen = (i + 1) * 3 + strlen (q) + 1;
  904. if (j != 0)
  905. {
  906. if (currlen < prevlen)
  907. g_free (buf);
  908. else
  909. goto ret;
  910. }
  911. p = buf = g_malloc (currlen);
  912. prevlen = currlen;
  913. for (; i >= 0; i--, p += 3)
  914. strcpy (p, "../");
  915. strcpy (p, q);
  916. }
  917. ret:
  918. g_free (my_first);
  919. g_free (my_second);
  920. return buf;
  921. }
  922. /* --------------------------------------------------------------------------------------------- */
  923. /**
  924. * Append text to GList, remove all entries with the same text
  925. */
  926. GList *
  927. list_append_unique (GList * list, char *text)
  928. {
  929. GList *lc_link;
  930. /*
  931. * Go to the last position and traverse the list backwards
  932. * starting from the second last entry to make sure that we
  933. * are not removing the current link.
  934. */
  935. list = g_list_append (list, text);
  936. list = g_list_last (list);
  937. lc_link = g_list_previous (list);
  938. while (lc_link != NULL)
  939. {
  940. GList *newlink;
  941. newlink = g_list_previous (lc_link);
  942. if (strcmp ((char *) lc_link->data, text) == 0)
  943. {
  944. GList *tmp;
  945. g_free (lc_link->data);
  946. tmp = g_list_remove_link (list, lc_link);
  947. (void) tmp;
  948. g_list_free_1 (lc_link);
  949. }
  950. lc_link = newlink;
  951. }
  952. return list;
  953. }
  954. /* --------------------------------------------------------------------------------------------- */
  955. /**
  956. * Read and restore position for the given filename.
  957. * If there is no stored data, return line 1 and col 0.
  958. */
  959. void
  960. load_file_position (const vfs_path_t * filename_vpath, long *line, long *column, off_t * offset,
  961. GArray ** bookmarks)
  962. {
  963. char *fn;
  964. FILE *f;
  965. char buf[MC_MAXPATHLEN + 100];
  966. const size_t len = vfs_path_len (filename_vpath);
  967. /* defaults */
  968. *line = 1;
  969. *column = 0;
  970. *offset = 0;
  971. /* open file with positions */
  972. fn = mc_config_get_full_path (MC_FILEPOS_FILE);
  973. f = fopen (fn, "r");
  974. g_free (fn);
  975. if (f == NULL)
  976. return;
  977. /* prepare array for serialized bookmarks */
  978. if (bookmarks != NULL)
  979. *bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS);
  980. while (fgets (buf, sizeof (buf), f) != NULL)
  981. {
  982. const char *p;
  983. gchar **pos_tokens;
  984. /* check if the filename matches the beginning of string */
  985. if (strncmp (buf, vfs_path_as_str (filename_vpath), len) != 0)
  986. continue;
  987. /* followed by single space */
  988. if (buf[len] != ' ')
  989. continue;
  990. /* and string without spaces */
  991. p = &buf[len + 1];
  992. if (strchr (p, ' ') != NULL)
  993. continue;
  994. pos_tokens = g_strsplit (p, ";", 3 + MAX_SAVED_BOOKMARKS);
  995. if (pos_tokens[0] == NULL)
  996. {
  997. *line = 1;
  998. *column = 0;
  999. *offset = 0;
  1000. }
  1001. else
  1002. {
  1003. *line = strtol (pos_tokens[0], NULL, 10);
  1004. if (pos_tokens[1] == NULL)
  1005. {
  1006. *column = 0;
  1007. *offset = 0;
  1008. }
  1009. else
  1010. {
  1011. *column = strtol (pos_tokens[1], NULL, 10);
  1012. if (pos_tokens[2] == NULL)
  1013. *offset = 0;
  1014. else if (bookmarks != NULL)
  1015. {
  1016. size_t i;
  1017. *offset = (off_t) g_ascii_strtoll (pos_tokens[2], NULL, 10);
  1018. for (i = 0; i < MAX_SAVED_BOOKMARKS && pos_tokens[3 + i] != NULL; i++)
  1019. {
  1020. size_t val;
  1021. val = strtoul (pos_tokens[3 + i], NULL, 10);
  1022. g_array_append_val (*bookmarks, val);
  1023. }
  1024. }
  1025. }
  1026. }
  1027. g_strfreev (pos_tokens);
  1028. }
  1029. fclose (f);
  1030. }
  1031. /* --------------------------------------------------------------------------------------------- */
  1032. /**
  1033. * Save position for the given file
  1034. */
  1035. void
  1036. save_file_position (const vfs_path_t * filename_vpath, long line, long column, off_t offset,
  1037. GArray * bookmarks)
  1038. {
  1039. static size_t filepos_max_saved_entries = 0;
  1040. char *fn, *tmp_fn;
  1041. FILE *f, *tmp_f;
  1042. char buf[MC_MAXPATHLEN + 100];
  1043. size_t i;
  1044. const size_t len = vfs_path_len (filename_vpath);
  1045. gboolean src_error = FALSE;
  1046. if (filepos_max_saved_entries == 0)
  1047. filepos_max_saved_entries = mc_config_get_int (mc_global.main_config, CONFIG_APP_SECTION,
  1048. "filepos_max_saved_entries", 1024);
  1049. fn = mc_config_get_full_path (MC_FILEPOS_FILE);
  1050. if (fn == NULL)
  1051. goto early_error;
  1052. mc_util_make_backup_if_possible (fn, TMP_SUFFIX);
  1053. /* open file */
  1054. f = fopen (fn, "w");
  1055. if (f == NULL)
  1056. goto open_target_error;
  1057. tmp_fn = g_strdup_printf ("%s" TMP_SUFFIX, fn);
  1058. tmp_f = fopen (tmp_fn, "r");
  1059. if (tmp_f == NULL)
  1060. {
  1061. src_error = TRUE;
  1062. goto open_source_error;
  1063. }
  1064. /* put the new record */
  1065. if (line != 1 || column != 0 || bookmarks != NULL)
  1066. {
  1067. if (fprintf
  1068. (f, "%s %ld;%ld;%" PRIuMAX, vfs_path_as_str (filename_vpath), line, column,
  1069. (uintmax_t) offset) < 0)
  1070. goto write_position_error;
  1071. if (bookmarks != NULL)
  1072. for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++)
  1073. if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0)
  1074. goto write_position_error;
  1075. if (fprintf (f, "\n") < 0)
  1076. goto write_position_error;
  1077. }
  1078. i = 1;
  1079. while (fgets (buf, sizeof (buf), tmp_f) != NULL)
  1080. {
  1081. if (buf[len] == ' ' && strncmp (buf, vfs_path_as_str (filename_vpath), len) == 0
  1082. && strchr (&buf[len + 1], ' ') == NULL)
  1083. continue;
  1084. fprintf (f, "%s", buf);
  1085. if (++i > filepos_max_saved_entries)
  1086. break;
  1087. }
  1088. write_position_error:
  1089. fclose (tmp_f);
  1090. open_source_error:
  1091. g_free (tmp_fn);
  1092. fclose (f);
  1093. if (src_error)
  1094. mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
  1095. else
  1096. mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
  1097. open_target_error:
  1098. g_free (fn);
  1099. early_error:
  1100. if (bookmarks != NULL)
  1101. g_array_free (bookmarks, TRUE);
  1102. }
  1103. /* --------------------------------------------------------------------------------------------- */
  1104. extern int
  1105. ascii_alpha_to_cntrl (int ch)
  1106. {
  1107. if ((ch >= ASCII_A && ch <= ASCII_Z) || (ch >= ASCII_a && ch <= ASCII_z))
  1108. {
  1109. ch &= 0x1f;
  1110. }
  1111. return ch;
  1112. }
  1113. /* --------------------------------------------------------------------------------------------- */
  1114. const char *
  1115. Q_ (const char *s)
  1116. {
  1117. const char *result, *sep;
  1118. result = _(s);
  1119. sep = strchr (result, '|');
  1120. return (sep != NULL) ? sep + 1 : result;
  1121. }
  1122. /* --------------------------------------------------------------------------------------------- */
  1123. gboolean
  1124. mc_util_make_backup_if_possible (const char *file_name, const char *backup_suffix)
  1125. {
  1126. struct stat stat_buf;
  1127. char *backup_path;
  1128. gboolean ret;
  1129. if (!exist_file (file_name))
  1130. return FALSE;
  1131. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1132. if (backup_path == NULL)
  1133. return FALSE;
  1134. ret = mc_util_write_backup_content (file_name, backup_path);
  1135. if (ret)
  1136. {
  1137. /* Backup file will have same ownership with main file. */
  1138. if (stat (file_name, &stat_buf) == 0)
  1139. chmod (backup_path, stat_buf.st_mode);
  1140. else
  1141. chmod (backup_path, S_IRUSR | S_IWUSR);
  1142. }
  1143. g_free (backup_path);
  1144. return ret;
  1145. }
  1146. /* --------------------------------------------------------------------------------------------- */
  1147. gboolean
  1148. mc_util_restore_from_backup_if_possible (const char *file_name, const char *backup_suffix)
  1149. {
  1150. gboolean ret;
  1151. char *backup_path;
  1152. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1153. if (backup_path == NULL)
  1154. return FALSE;
  1155. ret = mc_util_write_backup_content (backup_path, file_name);
  1156. g_free (backup_path);
  1157. return ret;
  1158. }
  1159. /* --------------------------------------------------------------------------------------------- */
  1160. gboolean
  1161. mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suffix)
  1162. {
  1163. char *backup_path;
  1164. backup_path = g_strdup_printf ("%s%s", file_name, backup_suffix);
  1165. if (backup_path == NULL)
  1166. return FALSE;
  1167. if (exist_file (backup_path))
  1168. {
  1169. vfs_path_t *vpath;
  1170. vpath = vfs_path_from_str (backup_path);
  1171. mc_unlink (vpath);
  1172. vfs_path_free (vpath);
  1173. }
  1174. g_free (backup_path);
  1175. return TRUE;
  1176. }
  1177. /* --------------------------------------------------------------------------------------------- */
  1178. /**
  1179. * partly taken from dcigettext.c, returns "" for default locale
  1180. * value should be freed by calling function g_free()
  1181. */
  1182. char *
  1183. guess_message_value (void)
  1184. {
  1185. static const char *const var[] = {
  1186. /* Setting of LC_ALL overwrites all other. */
  1187. /* Do not use LANGUAGE for check user locale and drowing hints */
  1188. "LC_ALL",
  1189. /* Next comes the name of the desired category. */
  1190. "LC_MESSAGES",
  1191. /* Last possibility is the LANG environment variable. */
  1192. "LANG",
  1193. /* NULL exit loops */
  1194. NULL
  1195. };
  1196. unsigned i = 0;
  1197. const char *locale = NULL;
  1198. while (var[i] != NULL)
  1199. {
  1200. locale = getenv (var[i]);
  1201. if (locale != NULL && locale[0] != '\0')
  1202. break;
  1203. i++;
  1204. }
  1205. if (locale == NULL)
  1206. locale = "";
  1207. return g_strdup (locale);
  1208. }
  1209. /* --------------------------------------------------------------------------------------------- */
  1210. /**
  1211. * The "profile root" is the tree under which all of MC's user data &
  1212. * settings are stored.
  1213. *
  1214. * It defaults to the user's home dir. The user may override this default
  1215. * with the environment variable $MC_PROFILE_ROOT.
  1216. */
  1217. const char *
  1218. mc_get_profile_root (void)
  1219. {
  1220. static const char *profile_root = NULL;
  1221. if (profile_root == NULL)
  1222. {
  1223. profile_root = g_getenv ("MC_PROFILE_ROOT");
  1224. if (profile_root == NULL || *profile_root == '\0')
  1225. profile_root = mc_config_get_home_dir ();
  1226. }
  1227. return profile_root;
  1228. }
  1229. /* --------------------------------------------------------------------------------------------- */
  1230. /**
  1231. * Propagate error in simple way.
  1232. *
  1233. * @param dest error return location
  1234. * @param code error code
  1235. * @param format printf()-style format for error message
  1236. * @param ... parameters for message format
  1237. */
  1238. void
  1239. mc_propagate_error (GError ** dest, int code, const char *format, ...)
  1240. {
  1241. if (dest != NULL && *dest == NULL)
  1242. {
  1243. GError *tmp_error;
  1244. va_list args;
  1245. va_start (args, format);
  1246. tmp_error = g_error_new_valist (MC_ERROR, code, format, args);
  1247. va_end (args);
  1248. g_propagate_error (dest, tmp_error);
  1249. }
  1250. }
  1251. /* --------------------------------------------------------------------------------------------- */
  1252. /**
  1253. * Replace existing error in simple way.
  1254. *
  1255. * @param dest error return location
  1256. * @param code error code
  1257. * @param format printf()-style format for error message
  1258. * @param ... parameters for message format
  1259. */
  1260. void
  1261. mc_replace_error (GError ** dest, int code, const char *format, ...)
  1262. {
  1263. if (dest != NULL)
  1264. {
  1265. GError *tmp_error;
  1266. va_list args;
  1267. va_start (args, format);
  1268. tmp_error = g_error_new_valist (MC_ERROR, code, format, args);
  1269. va_end (args);
  1270. g_error_free (*dest);
  1271. *dest = NULL;
  1272. g_propagate_error (dest, tmp_error);
  1273. }
  1274. }
  1275. /* --------------------------------------------------------------------------------------------- */
  1276. /**
  1277. * Returns if the given duration has elapsed since the given timestamp,
  1278. * and if it has then updates the timestamp.
  1279. *
  1280. * @param timestamp the last timestamp in microseconds, updated if the given time elapsed
  1281. * @param deleay amount of time in microseconds
  1282. * @return TRUE if clock skew detected, FALSE otherwise
  1283. */
  1284. gboolean
  1285. mc_time_elapsed (guint64 * timestamp, guint64 delay)
  1286. {
  1287. guint64 now;
  1288. now = mc_timer_elapsed (mc_global.timer);
  1289. if (now >= *timestamp && now < *timestamp + delay)
  1290. return FALSE;
  1291. *timestamp = now;
  1292. return TRUE;
  1293. }
  1294. /* --------------------------------------------------------------------------------------------- */