util.c 41 KB

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