util.c 41 KB

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