util.c 42 KB

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