util.c 40 KB

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