util.c 42 KB

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