util.c 40 KB

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