util.c 38 KB

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