util.c 41 KB

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