util.c 41 KB

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