util.c 39 KB

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