util.c 40 KB

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