]> sjero.net Git - wget/blob - src/utils.c
Plug memory leak
[wget] / src / utils.c
1 /* Various utility functions.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation,
4    Inc.
5
6 This file is part of GNU Wget.
7
8 GNU Wget is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Wget 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
18 You should have received a copy of the GNU General Public License
19 along with Wget.  If not, see <http://www.gnu.org/licenses/>.
20
21 Additional permission under GNU GPL version 3 section 7
22
23 If you modify this program, or any covered work, by linking or
24 combining it with the OpenSSL project's OpenSSL library (or a
25 modified version of that library), containing parts covered by the
26 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
27 grants you additional permission to convey the resulting work.
28 Corresponding Source for a non-source form of such a combination
29 shall include the source code for the parts of OpenSSL used as well
30 as that of the covered work.  */
31
32 #include "wget.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_MMAP
40 # include <sys/mman.h>
41 #endif
42 #ifdef HAVE_PROCESS_H
43 # include <process.h>  /* getpid() */
44 #endif
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <assert.h>
48 #include <stdarg.h>
49 #include <locale.h>
50
51 #if HAVE_UTIME
52 # include <sys/types.h>
53 # ifdef HAVE_UTIME_H
54 #  include <utime.h>
55 # endif
56
57 # ifdef HAVE_SYS_UTIME_H
58 #  include <sys/utime.h>
59 # endif
60 #endif
61
62 #include <sys/time.h>
63
64 #include <sys/stat.h>
65
66 /* For TIOCGWINSZ and friends: */
67 #include <sys/ioctl.h>
68 #ifdef HAVE_TERMIOS_H
69 # include <termios.h>
70 #endif
71
72 /* Needed for Unix version of run_with_timeout. */
73 #include <signal.h>
74 #include <setjmp.h>
75
76 #include <regex.h>
77 #ifdef HAVE_LIBPCRE
78 # include <pcre.h>
79 #endif
80
81 #ifndef HAVE_SIGSETJMP
82 /* If sigsetjmp is a macro, configure won't pick it up. */
83 # ifdef sigsetjmp
84 #  define HAVE_SIGSETJMP
85 # endif
86 #endif
87
88 #if defined HAVE_SIGSETJMP || defined HAVE_SIGBLOCK
89 # define USE_SIGNAL_TIMEOUT
90 #endif
91
92 #include "utils.h"
93 #include "hash.h"
94
95 #ifdef __VMS
96 #include "vms.h"
97 #endif /* def __VMS */
98
99 #ifdef TESTING
100 #include "test.h"
101 #endif
102
103 static void
104 memfatal (const char *context, long attempted_size)
105 {
106   /* Make sure we don't try to store part of the log line, and thus
107      call malloc.  */
108   log_set_save_context (false);
109
110   /* We have different log outputs in different situations:
111      1) output without bytes information
112      2) output with bytes information  */
113   if (attempted_size == UNKNOWN_ATTEMPTED_SIZE)
114     {
115       logprintf (LOG_ALWAYS,
116                  _("%s: %s: Failed to allocate enough memory; memory exhausted.\n"),
117                  exec_name, context);
118     }
119   else
120     {
121       logprintf (LOG_ALWAYS,
122                  _("%s: %s: Failed to allocate %ld bytes; memory exhausted.\n"),
123                  exec_name, context, attempted_size);
124     }
125
126   exit (1);
127 }
128
129 /* Character property table for (re-)escaping VMS ODS5 extended file
130    names.  Note that this table ignores Unicode.
131
132    ODS2 valid characters: 0-9 A-Z a-z $ - _ ~
133
134    ODS5 Invalid characters:
135       C0 control codes (0x00 to 0x1F inclusive)
136       Asterisk (*)
137       Question mark (?)
138
139    ODS5 Invalid characters only in VMS V7.2 (which no one runs, right?):
140       Double quotation marks (")
141       Backslash (\)
142       Colon (:)
143       Left angle bracket (<)
144       Right angle bracket (>)
145       Slash (/)
146       Vertical bar (|)
147
148    Characters escaped by "^":
149       SP  !  "  #  %  &  '  (  )  +  ,  .  :  ;  =
150        @  [  \  ]  ^  `  {  |  }  ~
151
152    Either "^_" or "^ " is accepted as a space.  Period (.) is a special
153    case.  Note that un-escaped < and > can also confuse a directory
154    spec.
155
156    Characters put out as ^xx:
157       7F (DEL)
158       80-9F (C1 control characters)
159       A0 (nonbreaking space)
160       FF (Latin small letter y diaeresis)
161
162    Other cases:
163       Unicode: "^Uxxxx", where "xxxx" is four hex digits.
164
165     Property table values:
166       Normal escape:    1
167       Space:            2
168       Dot:              4
169       Hex-hex escape:   8
170       ODS2 normal:     16
171       ODS2 lower case: 32
172       Hex digit:       64
173 */
174
175 unsigned char char_prop[ 256] = {
176
177 /* NUL SOH STX ETX EOT ENQ ACK BEL   BS  HT  LF  VT  FF  CR  SO  SI */
178     0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  0,
179
180 /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB  CAN  EM SUB ESC  FS  GS  RS  US */
181     0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  0,
182
183 /*  SP  !   "   #   $   %   &   '    (   )   *   +   ,   -   .   /  */
184     2,  1,  1,  1, 16,  1,  1,  1,   1,  1,  0,  1,  1, 16,  4,  0,
185
186 /*  0   1   2   3   4   5   6   7    8   9   :   ;   <   =   >   ?  */
187    80, 80, 80, 80, 80, 80, 80, 80,  80, 80,  1,  1,  1,  1,  1,  1,
188
189 /*  @   A   B   C   D   E   F   G    H   I   J   K   L   M   N   O  */
190     1, 80, 80, 80, 80, 80, 80, 16,  16, 16, 16, 16, 16, 16, 16, 16,
191
192 /*  P   Q   R   S   T   U   V   W    X   Y   Z   [   \   ]   ^   _  */
193    16, 16, 16, 16, 16, 16, 16, 16,  16, 16, 16,  1,  1,  1,  1, 16,
194
195 /*  `   a   b   c   d   e   f   g    h   i   j   k   l   m   n   o  */
196     1, 96, 96, 96, 96, 96, 96, 32,  32, 32, 32, 32, 32, 32, 32, 32,
197
198 /*  p   q   r   s   t   u   v   w    x   y   z   {   |   }   ~  DEL */
199    32, 32, 32, 32, 32, 32, 32, 32,  32, 32, 32,  1,  1,  1, 17,  8,
200
201     8,  8,  8,  8,  8,  8,  8,  8,   8,  8,  8,  8,  8,  8,  8,  8,
202     8,  8,  8,  8,  8,  8,  8,  8,   8,  8,  8,  8,  8,  8,  8,  8,
203     8,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  0,
204     0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  0,
205     0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  0,
206     0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  0,
207     0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  0,
208     0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  8
209 };
210
211 /* Utility function: like xstrdup(), but also lowercases S.  */
212
213 char *
214 xstrdup_lower (const char *s)
215 {
216   char *copy = xstrdup (s);
217   char *p = copy;
218   for (; *p; p++)
219     *p = c_tolower (*p);
220   return copy;
221 }
222
223 /* Copy the string formed by two pointers (one on the beginning, other
224    on the char after the last char) to a new, malloc-ed location.
225    0-terminate it.  */
226 char *
227 strdupdelim (const char *beg, const char *end)
228 {
229   char *res = xmalloc (end - beg + 1);
230   memcpy (res, beg, end - beg);
231   res[end - beg] = '\0';
232   return res;
233 }
234
235 /* Parse a string containing comma-separated elements, and return a
236    vector of char pointers with the elements.  Spaces following the
237    commas are ignored.  */
238 char **
239 sepstring (const char *s)
240 {
241   char **res;
242   const char *p;
243   int i = 0;
244
245   if (!s || !*s)
246     return NULL;
247   res = NULL;
248   p = s;
249   while (*s)
250     {
251       if (*s == ',')
252         {
253           res = xrealloc (res, (i + 2) * sizeof (char *));
254           res[i] = strdupdelim (p, s);
255           res[++i] = NULL;
256           ++s;
257           /* Skip the blanks following the ','.  */
258           while (c_isspace (*s))
259             ++s;
260           p = s;
261         }
262       else
263         ++s;
264     }
265   res = xrealloc (res, (i + 2) * sizeof (char *));
266   res[i] = strdupdelim (p, s);
267   res[i + 1] = NULL;
268   return res;
269 }
270 \f
271 /* Like sprintf, but prints into a string of sufficient size freshly
272    allocated with malloc, which is returned.  If unable to print due
273    to invalid format, returns NULL.  Inability to allocate needed
274    memory results in abort, as with xmalloc.  This is in spirit
275    similar to the GNU/BSD extension asprintf, but somewhat easier to
276    use.
277
278    Internally the function either calls vasprintf or loops around
279    vsnprintf until the correct size is found.  Since Wget also ships a
280    fallback implementation of vsnprintf, this should be portable.  */
281
282 /* Constant is using for limits memory allocation for text buffer.
283    Applicable in situation when: vasprintf is not available in the system
284    and vsnprintf return -1 when long line is truncated (in old versions of
285    glibc and in other system where C99 doesn`t support) */
286
287 #define FMT_MAX_LENGTH 1048576
288
289 char *
290 aprintf (const char *fmt, ...)
291 {
292 #if defined HAVE_VASPRINTF && !defined DEBUG_MALLOC
293   /* Use vasprintf. */
294   int ret;
295   va_list args;
296   char *str;
297   va_start (args, fmt);
298   ret = vasprintf (&str, fmt, args);
299   va_end (args);
300   if (ret < 0 && errno == ENOMEM)
301     memfatal ("aprintf", UNKNOWN_ATTEMPTED_SIZE);  /* for consistency
302                                                       with xmalloc/xrealloc */
303   else if (ret < 0)
304     return NULL;
305   return str;
306 #else  /* not HAVE_VASPRINTF */
307
308   /* vasprintf is unavailable.  snprintf into a small buffer and
309      resize it as necessary. */
310   int size = 32;
311   char *str = xmalloc (size);
312
313   /* #### This code will infloop and eventually abort in xrealloc if
314      passed a FMT that causes snprintf to consistently return -1.  */
315
316   while (1)
317     {
318       int n;
319       va_list args;
320
321       va_start (args, fmt);
322       n = vsnprintf (str, size, fmt, args);
323       va_end (args);
324
325       /* If the printing worked, return the string. */
326       if (n > -1 && n < size)
327         return str;
328
329       /* Else try again with a larger buffer. */
330       if (n > -1)               /* C99 */
331         size = n + 1;           /* precisely what is needed */
332       else if (size >= FMT_MAX_LENGTH)  /* We have a huge buffer, */
333         {                               /* maybe we have some wrong
334                                            format string? */
335           logprintf (LOG_ALWAYS,
336                      _("%s: aprintf: text buffer is too big (%ld bytes), "
337                        "aborting.\n"),
338                      exec_name, size);  /* printout a log message */
339           abort ();                     /* and abort... */
340         }
341       else
342         {
343           /* else, we continue to grow our
344            * buffer: Twice the old size. */
345           size <<= 1;
346         }
347       str = xrealloc (str, size);
348     }
349 #endif /* not HAVE_VASPRINTF */
350 }
351
352 /* Concatenate the NULL-terminated list of string arguments into
353    freshly allocated space.  */
354
355 char *
356 concat_strings (const char *str0, ...)
357 {
358   va_list args;
359   int saved_lengths[5];         /* inspired by Apache's apr_pstrcat */
360   char *ret, *p;
361
362   const char *next_str;
363   int total_length = 0;
364   size_t argcount;
365
366   /* Calculate the length of and allocate the resulting string. */
367
368   argcount = 0;
369   va_start (args, str0);
370   for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
371     {
372       int len = strlen (next_str);
373       if (argcount < countof (saved_lengths))
374         saved_lengths[argcount++] = len;
375       total_length += len;
376     }
377   va_end (args);
378   p = ret = xmalloc (total_length + 1);
379
380   /* Copy the strings into the allocated space. */
381
382   argcount = 0;
383   va_start (args, str0);
384   for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
385     {
386       int len;
387       if (argcount < countof (saved_lengths))
388         len = saved_lengths[argcount++];
389       else
390         len = strlen (next_str);
391       memcpy (p, next_str, len);
392       p += len;
393     }
394   va_end (args);
395   *p = '\0';
396
397   return ret;
398 }
399 \f
400 /* Format the provided time according to the specified format.  The
401    format is a string with format elements supported by strftime.  */
402
403 static char *
404 fmttime (time_t t, const char *fmt)
405 {
406   static char output[32];
407   struct tm *tm = localtime(&t);
408   if (!tm)
409     abort ();
410   if (!strftime(output, sizeof(output), fmt, tm))
411     abort ();
412   return output;
413 }
414
415 /* Return pointer to a static char[] buffer in which zero-terminated
416    string-representation of TM (in form hh:mm:ss) is printed.
417
418    If TM is NULL, the current time will be used.  */
419
420 char *
421 time_str (time_t t)
422 {
423   return fmttime(t, "%H:%M:%S");
424 }
425
426 /* Like the above, but include the date: YYYY-MM-DD hh:mm:ss.  */
427
428 char *
429 datetime_str (time_t t)
430 {
431   return fmttime(t, "%Y-%m-%d %H:%M:%S");
432 }
433 \f
434 /* The Windows versions of the following two functions are defined in
435    mswindows.c. On MSDOS this function should never be called. */
436
437 #ifdef __VMS
438
439 void
440 fork_to_background (void)
441 {
442   return;
443 }
444
445 #else /* def __VMS */
446
447 #if !defined(WINDOWS) && !defined(MSDOS)
448 void
449 fork_to_background (void)
450 {
451   pid_t pid;
452   /* Whether we arrange our own version of opt.lfilename here.  */
453   bool logfile_changed = false;
454
455   if (!opt.lfilename && (!opt.quiet || opt.server_response))
456     {
457       /* We must create the file immediately to avoid either a race
458          condition (which arises from using unique_name and failing to
459          use fopen_excl) or lying to the user about the log file name
460          (which arises from using unique_name, printing the name, and
461          using fopen_excl later on.)  */
462       FILE *new_log_fp = unique_create (DEFAULT_LOGFILE, false, &opt.lfilename);
463       if (new_log_fp)
464         {
465           logfile_changed = true;
466           fclose (new_log_fp);
467         }
468     }
469   pid = fork ();
470   if (pid < 0)
471     {
472       /* parent, error */
473       perror ("fork");
474       exit (1);
475     }
476   else if (pid != 0)
477     {
478       /* parent, no error */
479       printf (_("Continuing in background, pid %d.\n"), (int) pid);
480       if (logfile_changed)
481         printf (_("Output will be written to %s.\n"), quote (opt.lfilename));
482       exit (0);                 /* #### should we use _exit()? */
483     }
484
485   /* child: give up the privileges and keep running. */
486   setsid ();
487   freopen ("/dev/null", "r", stdin);
488   freopen ("/dev/null", "w", stdout);
489   freopen ("/dev/null", "w", stderr);
490 }
491 #endif /* !WINDOWS && !MSDOS */
492
493 #endif /* def __VMS [else] */
494
495 \f
496 /* "Touch" FILE, i.e. make its mtime ("modified time") equal the time
497    specified with TM.  The atime ("access time") is set to the current
498    time.  */
499
500 void
501 touch (const char *file, time_t tm)
502 {
503 #if HAVE_UTIME
504 # ifdef HAVE_STRUCT_UTIMBUF
505   struct utimbuf times;
506 # else
507   struct {
508     time_t actime;
509     time_t modtime;
510   } times;
511 # endif
512   times.modtime = tm;
513   times.actime = time (NULL);
514   if (utime (file, &times) == -1)
515     logprintf (LOG_NOTQUIET, "utime(%s): %s\n", file, strerror (errno));
516 #else
517   struct timespec timespecs[2];
518   int fd;
519
520   fd = open (file, O_WRONLY);
521   if (fd < 0)
522     {
523       logprintf (LOG_NOTQUIET, "open(%s): %s\n", file, strerror (errno));
524       return;
525     }
526
527   timespecs[0].tv_sec = time (NULL);
528   timespecs[0].tv_nsec = 0L;
529   timespecs[1].tv_sec = tm;
530   timespecs[1].tv_nsec = 0L;
531
532   if (futimens (fd, timespecs) == -1)
533     logprintf (LOG_NOTQUIET, "futimens(%s): %s\n", file, strerror (errno));
534
535   close (fd);
536 #endif
537 }
538
539 /* Checks if FILE is a symbolic link, and removes it if it is.  Does
540    nothing under MS-Windows.  */
541 int
542 remove_link (const char *file)
543 {
544   int err = 0;
545   struct_stat st;
546
547   if (lstat (file, &st) == 0 && S_ISLNK (st.st_mode))
548     {
549       DEBUGP (("Unlinking %s (symlink).\n", file));
550       err = unlink (file);
551       if (err != 0)
552         logprintf (LOG_VERBOSE, _("Failed to unlink symlink %s: %s\n"),
553                    quote (file), strerror (errno));
554     }
555   return err;
556 }
557
558 /* Does FILENAME exist?  This is quite a lousy implementation, since
559    it supplies no error codes -- only a yes-or-no answer.  Thus it
560    will return that a file does not exist if, e.g., the directory is
561    unreadable.  I don't mind it too much currently, though.  The
562    proper way should, of course, be to have a third, error state,
563    other than true/false, but that would introduce uncalled-for
564    additional complexity to the callers.  */
565 bool
566 file_exists_p (const char *filename)
567 {
568 #ifdef HAVE_ACCESS
569   return access (filename, F_OK) >= 0;
570 #else
571   struct_stat buf;
572   return stat (filename, &buf) >= 0;
573 #endif
574 }
575
576 /* Returns 0 if PATH is a directory, 1 otherwise (any kind of file).
577    Returns 0 on error.  */
578 bool
579 file_non_directory_p (const char *path)
580 {
581   struct_stat buf;
582   /* Use lstat() rather than stat() so that symbolic links pointing to
583      directories can be identified correctly.  */
584   if (lstat (path, &buf) != 0)
585     return false;
586   return S_ISDIR (buf.st_mode) ? false : true;
587 }
588
589 /* Return the size of file named by FILENAME, or -1 if it cannot be
590    opened or seeked into. */
591 wgint
592 file_size (const char *filename)
593 {
594 #if defined(HAVE_FSEEKO) && defined(HAVE_FTELLO)
595   wgint size;
596   /* We use fseek rather than stat to determine the file size because
597      that way we can also verify that the file is readable without
598      explicitly checking for permissions.  Inspired by the POST patch
599      by Arnaud Wylie.  */
600   FILE *fp = fopen (filename, "rb");
601   if (!fp)
602     return -1;
603   fseeko (fp, 0, SEEK_END);
604   size = ftello (fp);
605   fclose (fp);
606   return size;
607 #else
608   struct_stat st;
609   if (stat (filename, &st) < 0)
610     return -1;
611   return st.st_size;
612 #endif
613 }
614
615 /* 2005-02-19 SMS.
616    If no UNIQ_SEP is defined (as on VMS), have unique_name() return the
617    original name.  With the VMS file systems' versioning, everything
618    should be fine, and appending ".NN" just causes trouble.
619 */
620
621 #ifdef UNIQ_SEP
622
623 /* stat file names named PREFIX.1, PREFIX.2, etc., until one that
624    doesn't exist is found.  Return a freshly allocated copy of the
625    unused file name.  */
626
627 static char *
628 unique_name_1 (const char *prefix)
629 {
630   int count = 1;
631   int plen = strlen (prefix);
632   char *template = (char *)alloca (plen + 1 + 24);
633   char *template_tail = template + plen;
634
635   memcpy (template, prefix, plen);
636   *template_tail++ = UNIQ_SEP;
637
638   do
639     number_to_string (template_tail, count++);
640   while (file_exists_p (template));
641
642   return xstrdup (template);
643 }
644
645 /* Return a unique file name, based on FILE.
646
647    More precisely, if FILE doesn't exist, it is returned unmodified.
648    If not, FILE.1 is tried, then FILE.2, etc.  The first FILE.<number>
649    file name that doesn't exist is returned.
650
651    2005-02-19 SMS.  "." is now UNIQ_SEP, and may be different.
652
653    The resulting file is not created, only verified that it didn't
654    exist at the point in time when the function was called.
655    Therefore, where security matters, don't rely that the file created
656    by this function exists until you open it with O_EXCL or
657    equivalent.
658
659    If ALLOW_PASSTHROUGH is 0, it always returns a freshly allocated
660    string.  Otherwise, it may return FILE if the file doesn't exist
661    (and therefore doesn't need changing).  */
662
663 char *
664 unique_name (const char *file, bool allow_passthrough)
665 {
666   /* If the FILE itself doesn't exist, return it without
667      modification. */
668   if (!file_exists_p (file))
669     return allow_passthrough ? (char *)file : xstrdup (file);
670
671   /* Otherwise, find a numeric suffix that results in unused file name
672      and return it.  */
673   return unique_name_1 (file);
674 }
675
676 #else /* def UNIQ_SEP */
677
678 /* Dummy unique_name() for VMS.  Return the original name as easily as
679    possible.
680 */
681 char *
682 unique_name (const char *file, bool allow_passthrough)
683 {
684   /* Return the FILE itself, without modification, irregardful. */
685   return allow_passthrough ? (char *)file : xstrdup (file);
686 }
687
688 #endif /* def UNIQ_SEP [else] */
689
690 /* Create a file based on NAME, except without overwriting an existing
691    file with that name.  Providing O_EXCL is correctly implemented,
692    this function does not have the race condition associated with
693    opening the file returned by unique_name.  */
694
695 FILE *
696 unique_create (const char *name, bool binary, char **opened_name)
697 {
698   /* unique file name, based on NAME */
699   char *uname = unique_name (name, false);
700   FILE *fp;
701   while ((fp = fopen_excl (uname, binary)) == NULL && errno == EEXIST)
702     {
703       xfree (uname);
704       uname = unique_name (name, false);
705     }
706   if (opened_name)
707     {
708       if (fp)
709         *opened_name = uname;
710       else
711         {
712           *opened_name = NULL;
713           xfree (uname);
714         }
715     }
716   else
717     xfree (uname);
718   return fp;
719 }
720
721 /* Open the file for writing, with the addition that the file is
722    opened "exclusively".  This means that, if the file already exists,
723    this function will *fail* and errno will be set to EEXIST.  If
724    BINARY is set, the file will be opened in binary mode, equivalent
725    to fopen's "wb".
726
727    If opening the file fails for any reason, including the file having
728    previously existed, this function returns NULL and sets errno
729    appropriately.  */
730
731 FILE *
732 fopen_excl (const char *fname, int binary)
733 {
734   int fd;
735 #ifdef O_EXCL
736
737 /* 2005-04-14 SMS.
738    VMS lacks O_BINARY, but makes up for it in weird and wonderful ways.
739    It also has file versions which obviate all the O_EXCL effort.
740    O_TRUNC (something of a misnomer) requests a new version.
741 */
742 # ifdef __VMS
743 /* Common open() optional arguments:
744    sequential access only, access callback function.
745 */
746 #  define OPEN_OPT_ARGS "fop=sqo", "acc", acc_cb, &open_id
747
748   int open_id;
749   int flags = O_WRONLY | O_CREAT | O_TRUNC;
750
751   if (binary > 1)
752     {
753       open_id = 11;
754       fd = open( fname,                 /* File name. */
755        flags,                           /* Flags. */
756        0777,                            /* Mode for default protection. */
757        "ctx=bin,stm",                   /* Binary, stream access. */
758        "rfm=stmlf",                     /* Stream_LF. */
759        OPEN_OPT_ARGS);                  /* Access callback. */
760     }
761   else if (binary)
762     {
763       open_id = 12;
764       fd = open( fname,                 /* File name. */
765        flags,                           /* Flags. */
766        0777,                            /* Mode for default protection. */
767        "ctx=bin,stm",                   /* Binary, stream access. */
768        "rfm=fix",                       /* Fixed-length, */
769        "mrs=512",                       /* 512-byte records. */
770        OPEN_OPT_ARGS);                  /* Access callback. */
771     }
772   else
773     {
774       open_id = 13;
775       fd = open( fname,                 /* File name. */
776        flags,                           /* Flags. */
777        0777,                            /* Mode for default protection. */
778        "rfm=stmlf",                     /* Stream_LF. */
779        OPEN_OPT_ARGS);                  /* Access callback. */
780     }
781 # else /* def __VMS */
782   int flags = O_WRONLY | O_CREAT | O_EXCL;
783 # ifdef O_BINARY
784   if (binary)
785     flags |= O_BINARY;
786 # endif
787   fd = open (fname, flags, 0666);
788 # endif /* def __VMS [else] */
789
790   if (fd < 0)
791     return NULL;
792   return fdopen (fd, binary ? "wb" : "w");
793 #else  /* not O_EXCL */
794   /* Manually check whether the file exists.  This is prone to race
795      conditions, but systems without O_EXCL haven't deserved
796      better.  */
797   if (file_exists_p (fname))
798     {
799       errno = EEXIST;
800       return NULL;
801     }
802   return fopen (fname, binary ? "wb" : "w");
803 #endif /* not O_EXCL */
804 }
805 \f
806 /* Create DIRECTORY.  If some of the pathname components of DIRECTORY
807    are missing, create them first.  In case any mkdir() call fails,
808    return its error status.  Returns 0 on successful completion.
809
810    The behaviour of this function should be identical to the behaviour
811    of `mkdir -p' on systems where mkdir supports the `-p' option.  */
812 int
813 make_directory (const char *directory)
814 {
815   int i, ret, quit = 0;
816   char *dir;
817
818   /* Make a copy of dir, to be able to write to it.  Otherwise, the
819      function is unsafe if called with a read-only char *argument.  */
820   STRDUP_ALLOCA (dir, directory);
821
822   /* If the first character of dir is '/', skip it (and thus enable
823      creation of absolute-pathname directories.  */
824   for (i = (*dir == '/'); 1; ++i)
825     {
826       for (; dir[i] && dir[i] != '/'; i++)
827         ;
828       if (!dir[i])
829         quit = 1;
830       dir[i] = '\0';
831       /* Check whether the directory already exists.  Allow creation of
832          of intermediate directories to fail, as the initial path components
833          are not necessarily directories!  */
834       if (!file_exists_p (dir))
835         ret = mkdir (dir, 0777);
836       else
837         ret = 0;
838       if (quit)
839         break;
840       else
841         dir[i] = '/';
842     }
843   return ret;
844 }
845
846 /* Merge BASE with FILE.  BASE can be a directory or a file name, FILE
847    should be a file name.
848
849    file_merge("/foo/bar", "baz")  => "/foo/baz"
850    file_merge("/foo/bar/", "baz") => "/foo/bar/baz"
851    file_merge("foo", "bar")       => "bar"
852
853    In other words, it's a simpler and gentler version of uri_merge.  */
854
855 char *
856 file_merge (const char *base, const char *file)
857 {
858   char *result;
859   const char *cut = (const char *)strrchr (base, '/');
860
861   if (!cut)
862     return xstrdup (file);
863
864   result = xmalloc (cut - base + 1 + strlen (file) + 1);
865   memcpy (result, base, cut - base);
866   result[cut - base] = '/';
867   strcpy (result + (cut - base) + 1, file);
868
869   return result;
870 }
871 \f
872 /* Like fnmatch, but performs a case-insensitive match.  */
873
874 int
875 fnmatch_nocase (const char *pattern, const char *string, int flags)
876 {
877 #ifdef FNM_CASEFOLD
878   /* The FNM_CASEFOLD flag started as a GNU extension, but it is now
879      also present on *BSD platforms, and possibly elsewhere.  */
880   return fnmatch (pattern, string, flags | FNM_CASEFOLD);
881 #else
882   /* Turn PATTERN and STRING to lower case and call fnmatch on them. */
883   char *patcopy = (char *) alloca (strlen (pattern) + 1);
884   char *strcopy = (char *) alloca (strlen (string) + 1);
885   char *p;
886   for (p = patcopy; *pattern; pattern++, p++)
887     *p = c_tolower (*pattern);
888   *p = '\0';
889   for (p = strcopy; *string; string++, p++)
890     *p = c_tolower (*string);
891   *p = '\0';
892   return fnmatch (patcopy, strcopy, flags);
893 #endif
894 }
895
896 static bool in_acclist (const char *const *, const char *, bool);
897
898 /* Determine whether a file is acceptable to be followed, according to
899    lists of patterns to accept/reject.  */
900 bool
901 acceptable (const char *s)
902 {
903   const char *p;
904
905   if (opt.output_document && strcmp (s, opt.output_document) == 0)
906     return true;
907
908   if ((p = strrchr (s, '/')))
909     s = p + 1;
910
911   if (opt.accepts)
912     {
913       if (opt.rejects)
914         return (in_acclist ((const char *const *)opt.accepts, s, true)
915                 && !in_acclist ((const char *const *)opt.rejects, s, true));
916       else
917         return in_acclist ((const char *const *)opt.accepts, s, true);
918     }
919   else if (opt.rejects)
920     return !in_acclist ((const char *const *)opt.rejects, s, true);
921
922   return true;
923 }
924
925 /* Determine whether an URL is acceptable to be followed, according to
926    regex patterns to accept/reject.  */
927 bool
928 accept_url (const char *s)
929 {
930   if (opt.acceptregex && !opt.regex_match_fun (opt.acceptregex, s))
931     return false;
932   if (opt.rejectregex && opt.regex_match_fun (opt.rejectregex, s))
933     return false;
934
935   return true;
936 }
937
938 /* Check if D2 is a subdirectory of D1.  E.g. if D1 is `/something', subdir_p()
939    will return true if and only if D2 begins with `/something/' or is exactly
940    '/something'.  */
941 bool
942 subdir_p (const char *d1, const char *d2)
943 {
944   if (*d1 == '\0')
945     return true;
946   if (!opt.ignore_case)
947     for (; *d1 && *d2 && (*d1 == *d2); ++d1, ++d2)
948       ;
949   else
950     for (; *d1 && *d2 && (c_tolower (*d1) == c_tolower (*d2)); ++d1, ++d2)
951       ;
952
953   return *d1 == '\0' && (*d2 == '\0' || *d2 == '/');
954 }
955
956 /* Iterate through DIRLIST (which must be NULL-terminated), and return the
957    first element that matches DIR, through wildcards or front comparison (as
958    appropriate).  */
959 static bool
960 dir_matches_p (char **dirlist, const char *dir)
961 {
962   char **x;
963   int (*matcher) (const char *, const char *, int)
964     = opt.ignore_case ? fnmatch_nocase : fnmatch;
965
966   for (x = dirlist; *x; x++)
967     {
968       /* Remove leading '/' */
969       char *p = *x + (**x == '/');
970       if (has_wildcards_p (p))
971         {
972           if (matcher (p, dir, FNM_PATHNAME) == 0)
973             break;
974         }
975       else
976         {
977           if (subdir_p (p, dir))
978             break;
979         }
980     }
981
982   return *x ? true : false;
983 }
984
985 /* Returns whether DIRECTORY is acceptable for download, wrt the
986    include/exclude lists.
987
988    The leading `/' is ignored in paths; relative and absolute paths
989    may be freely intermixed.  */
990
991 bool
992 accdir (const char *directory)
993 {
994   /* Remove starting '/'.  */
995   if (*directory == '/')
996     ++directory;
997   if (opt.includes)
998     {
999       if (!dir_matches_p (opt.includes, directory))
1000         return false;
1001     }
1002   if (opt.excludes)
1003     {
1004       if (dir_matches_p (opt.excludes, directory))
1005         return false;
1006     }
1007   return true;
1008 }
1009
1010 /* Return true if STRING ends with TAIL.  For instance:
1011
1012    match_tail ("abc", "bc", false)  -> 1
1013    match_tail ("abc", "ab", false)  -> 0
1014    match_tail ("abc", "abc", false) -> 1
1015
1016    If FOLD_CASE is true, the comparison will be case-insensitive.  */
1017
1018 bool
1019 match_tail (const char *string, const char *tail, bool fold_case)
1020 {
1021   int pos = strlen (string) - strlen (tail);
1022
1023   if (pos < 0)
1024     return false;  /* tail is longer than string.  */
1025
1026   if (!fold_case)
1027     return !strcmp (string + pos, tail);
1028   else
1029     return !strcasecmp (string + pos, tail);
1030 }
1031
1032 /* Checks whether string S matches each element of ACCEPTS.  A list
1033    element are matched either with fnmatch() or match_tail(),
1034    according to whether the element contains wildcards or not.
1035
1036    If the BACKWARD is false, don't do backward comparison -- just compare
1037    them normally.  */
1038 static bool
1039 in_acclist (const char *const *accepts, const char *s, bool backward)
1040 {
1041   for (; *accepts; accepts++)
1042     {
1043       if (has_wildcards_p (*accepts))
1044         {
1045           int res = opt.ignore_case
1046             ? fnmatch_nocase (*accepts, s, 0) : fnmatch (*accepts, s, 0);
1047           /* fnmatch returns 0 if the pattern *does* match the string.  */
1048           if (res == 0)
1049             return true;
1050         }
1051       else
1052         {
1053           if (backward)
1054             {
1055               if (match_tail (s, *accepts, opt.ignore_case))
1056                 return true;
1057             }
1058           else
1059             {
1060               int cmp = opt.ignore_case
1061                 ? strcasecmp (s, *accepts) : strcmp (s, *accepts);
1062               if (cmp == 0)
1063                 return true;
1064             }
1065         }
1066     }
1067   return false;
1068 }
1069
1070 /* Return the location of STR's suffix (file extension).  Examples:
1071    suffix ("foo.bar")       -> "bar"
1072    suffix ("foo.bar.baz")   -> "baz"
1073    suffix ("/foo/bar")      -> NULL
1074    suffix ("/foo.bar/baz")  -> NULL  */
1075 char *
1076 suffix (const char *str)
1077 {
1078   char *p;
1079
1080   if ((p = strrchr (str, '.')) && !strchr (p + 1, '/'))
1081     return p + 1;
1082
1083   return NULL;
1084 }
1085
1086 /* Return true if S contains globbing wildcards (`*', `?', `[' or
1087    `]').  */
1088
1089 bool
1090 has_wildcards_p (const char *s)
1091 {
1092   return !!strpbrk (s, "*?[]");
1093 }
1094
1095 /* Return true if FNAME ends with a typical HTML suffix.  The
1096    following (case-insensitive) suffixes are presumed to be HTML
1097    files:
1098
1099      html
1100      htm
1101      ?html (`?' matches one character)
1102
1103    #### CAVEAT.  This is not necessarily a good indication that FNAME
1104    refers to a file that contains HTML!  */
1105 bool
1106 has_html_suffix_p (const char *fname)
1107 {
1108   char *suf;
1109
1110   if ((suf = suffix (fname)) == NULL)
1111     return false;
1112   if (!strcasecmp (suf, "html"))
1113     return true;
1114   if (!strcasecmp (suf, "htm"))
1115     return true;
1116   if (suf[0] && !strcasecmp (suf + 1, "html"))
1117     return true;
1118   return false;
1119 }
1120
1121 /* Read FILE into memory.  A pointer to `struct file_memory' are
1122    returned; use struct element `content' to access file contents, and
1123    the element `length' to know the file length.  `content' is *not*
1124    zero-terminated, and you should *not* read or write beyond the [0,
1125    length) range of characters.
1126
1127    After you are done with the file contents, call wget_read_file_free to
1128    release the memory.
1129
1130    Depending on the operating system and the type of file that is
1131    being read, wget_read_file() either mmap's the file into memory, or
1132    reads the file into the core using read().
1133
1134    If file is named "-", fileno(stdin) is used for reading instead.
1135    If you want to read from a real file named "-", use "./-" instead.  */
1136
1137 struct file_memory *
1138 wget_read_file (const char *file)
1139 {
1140   int fd;
1141   struct file_memory *fm;
1142   long size;
1143   bool inhibit_close = false;
1144
1145   /* Some magic in the finest tradition of Perl and its kin: if FILE
1146      is "-", just use stdin.  */
1147   if (HYPHENP (file))
1148     {
1149       fd = fileno (stdin);
1150       inhibit_close = true;
1151       /* Note that we don't inhibit mmap() in this case.  If stdin is
1152          redirected from a regular file, mmap() will still work.  */
1153     }
1154   else
1155     fd = open (file, O_RDONLY);
1156   if (fd < 0)
1157     return NULL;
1158   fm = xnew (struct file_memory);
1159
1160 #ifdef HAVE_MMAP
1161   {
1162     struct_fstat buf;
1163     if (fstat (fd, &buf) < 0)
1164       goto mmap_lose;
1165     fm->length = buf.st_size;
1166     /* NOTE: As far as I know, the callers of this function never
1167        modify the file text.  Relying on this would enable us to
1168        specify PROT_READ and MAP_SHARED for a marginal gain in
1169        efficiency, but at some cost to generality.  */
1170     fm->content = mmap (NULL, fm->length, PROT_READ | PROT_WRITE,
1171                         MAP_PRIVATE, fd, 0);
1172     if (fm->content == (char *)MAP_FAILED)
1173       goto mmap_lose;
1174     if (!inhibit_close)
1175       close (fd);
1176
1177     fm->mmap_p = 1;
1178     return fm;
1179   }
1180
1181  mmap_lose:
1182   /* The most common reason why mmap() fails is that FD does not point
1183      to a plain file.  However, it's also possible that mmap() doesn't
1184      work for a particular type of file.  Therefore, whenever mmap()
1185      fails, we just fall back to the regular method.  */
1186 #endif /* HAVE_MMAP */
1187
1188   fm->length = 0;
1189   size = 512;                   /* number of bytes fm->contents can
1190                                    hold at any given time. */
1191   fm->content = xmalloc (size);
1192   while (1)
1193     {
1194       wgint nread;
1195       if (fm->length > size / 2)
1196         {
1197           /* #### I'm not sure whether the whole exponential-growth
1198              thing makes sense with kernel read.  On Linux at least,
1199              read() refuses to read more than 4K from a file at a
1200              single chunk anyway.  But other Unixes might optimize it
1201              better, and it doesn't *hurt* anything, so I'm leaving
1202              it.  */
1203
1204           /* Normally, we grow SIZE exponentially to make the number
1205              of calls to read() and realloc() logarithmic in relation
1206              to file size.  However, read() can read an amount of data
1207              smaller than requested, and it would be unreasonable to
1208              double SIZE every time *something* was read.  Therefore,
1209              we double SIZE only when the length exceeds half of the
1210              entire allocated size.  */
1211           size <<= 1;
1212           fm->content = xrealloc (fm->content, size);
1213         }
1214       nread = read (fd, fm->content + fm->length, size - fm->length);
1215       if (nread > 0)
1216         /* Successful read. */
1217         fm->length += nread;
1218       else if (nread < 0)
1219         /* Error. */
1220         goto lose;
1221       else
1222         /* EOF */
1223         break;
1224     }
1225   if (!inhibit_close)
1226     close (fd);
1227   if (size > fm->length && fm->length != 0)
1228     /* Due to exponential growth of fm->content, the allocated region
1229        might be much larger than what is actually needed.  */
1230     fm->content = xrealloc (fm->content, fm->length);
1231   fm->mmap_p = 0;
1232   return fm;
1233
1234  lose:
1235   if (!inhibit_close)
1236     close (fd);
1237   xfree (fm->content);
1238   xfree (fm);
1239   return NULL;
1240 }
1241
1242 /* Release the resources held by FM.  Specifically, this calls
1243    munmap() or xfree() on fm->content, depending whether mmap or
1244    malloc/read were used to read in the file.  It also frees the
1245    memory needed to hold the FM structure itself.  */
1246
1247 void
1248 wget_read_file_free (struct file_memory *fm)
1249 {
1250 #ifdef HAVE_MMAP
1251   if (fm->mmap_p)
1252     {
1253       munmap (fm->content, fm->length);
1254     }
1255   else
1256 #endif
1257     {
1258       xfree (fm->content);
1259     }
1260   xfree (fm);
1261 }
1262 \f
1263 /* Free the pointers in a NULL-terminated vector of pointers, then
1264    free the pointer itself.  */
1265 void
1266 free_vec (char **vec)
1267 {
1268   if (vec)
1269     {
1270       char **p = vec;
1271       while (*p)
1272         xfree (*p++);
1273       xfree (vec);
1274     }
1275 }
1276
1277 /* Append vector V2 to vector V1.  The function frees V2 and
1278    reallocates V1 (thus you may not use the contents of neither
1279    pointer after the call).  If V1 is NULL, V2 is returned.  */
1280 char **
1281 merge_vecs (char **v1, char **v2)
1282 {
1283   int i, j;
1284
1285   if (!v1)
1286     return v2;
1287   if (!v2)
1288     return v1;
1289   if (!*v2)
1290     {
1291       /* To avoid j == 0 */
1292       xfree (v2);
1293       return v1;
1294     }
1295   /* Count v1.  */
1296   for (i = 0; v1[i]; i++)
1297     ;
1298   /* Count v2.  */
1299   for (j = 0; v2[j]; j++)
1300     ;
1301   /* Reallocate v1.  */
1302   v1 = xrealloc (v1, (i + j + 1) * sizeof (char **));
1303   memcpy (v1 + i, v2, (j + 1) * sizeof (char *));
1304   xfree (v2);
1305   return v1;
1306 }
1307
1308 /* Append a freshly allocated copy of STR to VEC.  If VEC is NULL, it
1309    is allocated as needed.  Return the new value of the vector. */
1310
1311 char **
1312 vec_append (char **vec, const char *str)
1313 {
1314   int cnt;                      /* count of vector elements, including
1315                                    the one we're about to append */
1316   if (vec != NULL)
1317     {
1318       for (cnt = 0; vec[cnt]; cnt++)
1319         ;
1320       ++cnt;
1321     }
1322   else
1323     cnt = 1;
1324   /* Reallocate the array to fit the new element and the NULL. */
1325   vec = xrealloc (vec, (cnt + 1) * sizeof (char *));
1326   /* Append a copy of STR to the vector. */
1327   vec[cnt - 1] = xstrdup (str);
1328   vec[cnt] = NULL;
1329   return vec;
1330 }
1331 \f
1332 /* Sometimes it's useful to create "sets" of strings, i.e. special
1333    hash tables where you want to store strings as keys and merely
1334    query for their existence.  Here is a set of utility routines that
1335    makes that transparent.  */
1336
1337 void
1338 string_set_add (struct hash_table *ht, const char *s)
1339 {
1340   /* First check whether the set element already exists.  If it does,
1341      do nothing so that we don't have to free() the old element and
1342      then strdup() a new one.  */
1343   if (hash_table_contains (ht, s))
1344     return;
1345
1346   /* We use "1" as value.  It provides us a useful and clear arbitrary
1347      value, and it consumes no memory -- the pointers to the same
1348      string "1" will be shared by all the key-value pairs in all `set'
1349      hash tables.  */
1350   hash_table_put (ht, xstrdup (s), "1");
1351 }
1352
1353 /* Synonym for hash_table_contains... */
1354
1355 int
1356 string_set_contains (struct hash_table *ht, const char *s)
1357 {
1358   return hash_table_contains (ht, s);
1359 }
1360
1361 /* Convert the specified string set to array.  ARRAY should be large
1362    enough to hold hash_table_count(ht) char pointers.  */
1363
1364 void string_set_to_array (struct hash_table *ht, char **array)
1365 {
1366   hash_table_iterator iter;
1367   for (hash_table_iterate (ht, &iter); hash_table_iter_next (&iter); )
1368     *array++ = iter.key;
1369 }
1370
1371 /* Free the string set.  This frees both the storage allocated for
1372    keys and the actual hash table.  (hash_table_destroy would only
1373    destroy the hash table.)  */
1374
1375 void
1376 string_set_free (struct hash_table *ht)
1377 {
1378   hash_table_iterator iter;
1379   for (hash_table_iterate (ht, &iter); hash_table_iter_next (&iter); )
1380     xfree (iter.key);
1381   hash_table_destroy (ht);
1382 }
1383
1384 /* Utility function: simply call xfree() on all keys and values of HT.  */
1385
1386 void
1387 free_keys_and_values (struct hash_table *ht)
1388 {
1389   hash_table_iterator iter;
1390   for (hash_table_iterate (ht, &iter); hash_table_iter_next (&iter); )
1391     {
1392       xfree (iter.key);
1393       xfree (iter.value);
1394     }
1395 }
1396 \f
1397 /* Get digit grouping data for thousand separors by calling
1398    localeconv().  The data includes separator string and grouping info
1399    and is cached after the first call to the function.
1400
1401    In locales that don't set a thousand separator (such as the "C"
1402    locale), this forces it to be ",".  We are now only showing
1403    thousand separators in one place, so this shouldn't be a problem in
1404    practice.  */
1405
1406 static void
1407 get_grouping_data (const char **sep, const char **grouping)
1408 {
1409   static const char *cached_sep;
1410   static const char *cached_grouping;
1411   static bool initialized;
1412   if (!initialized)
1413     {
1414       /* Get the grouping info from the locale. */
1415       struct lconv *lconv = localeconv ();
1416       cached_sep = lconv->thousands_sep;
1417       cached_grouping = lconv->grouping;
1418 #if ! USE_NLS_PROGRESS_BAR
1419       /* We can't count column widths, so ensure that the separator
1420        * is single-byte only (let check below determine what byte). */
1421       if (strlen(cached_sep) > 1)
1422         cached_sep = "";
1423 #endif
1424       if (!*cached_sep)
1425         {
1426           /* Many locales (such as "C" or "hr_HR") don't specify
1427              grouping, which we still want to use it for legibility.
1428              In those locales set the sep char to ',', unless that
1429              character is used for decimal point, in which case set it
1430              to ".".  */
1431           if (*lconv->decimal_point != ',')
1432             cached_sep = ",";
1433           else
1434             cached_sep = ".";
1435           cached_grouping = "\x03";
1436         }
1437       initialized = true;
1438     }
1439   *sep = cached_sep;
1440   *grouping = cached_grouping;
1441 }
1442
1443 /* Return a printed representation of N with thousand separators.
1444    This should respect locale settings, with the exception of the "C"
1445    locale which mandates no separator, but we use one anyway.
1446
1447    Unfortunately, we cannot use %'d (in fact it would be %'j) to get
1448    the separators because it's too non-portable, and it's hard to test
1449    for this feature at configure time.  Besides, it wouldn't display
1450    separators in the "C" locale, still used by many Unix users.  */
1451
1452 const char *
1453 with_thousand_seps (wgint n)
1454 {
1455   static char outbuf[48];
1456   char *p = outbuf + sizeof outbuf;
1457
1458   /* Info received from locale */
1459   const char *grouping, *sep;
1460   int seplen;
1461
1462   /* State information */
1463   int i = 0, groupsize;
1464   const char *atgroup;
1465
1466   bool negative = n < 0;
1467
1468   /* Initialize grouping data. */
1469   get_grouping_data (&sep, &grouping);
1470   seplen = strlen (sep);
1471   atgroup = grouping;
1472   groupsize = *atgroup++;
1473
1474   /* This would overflow on WGINT_MIN, but printing negative numbers
1475      is not an important goal of this fuinction.  */
1476   if (negative)
1477     n = -n;
1478
1479   /* Write the number into the buffer, backwards, inserting the
1480      separators as necessary.  */
1481   *--p = '\0';
1482   while (1)
1483     {
1484       *--p = n % 10 + '0';
1485       n /= 10;
1486       if (n == 0)
1487         break;
1488       /* Prepend SEP to every groupsize'd digit and get new groupsize.  */
1489       if (++i == groupsize)
1490         {
1491           if (seplen == 1)
1492             *--p = *sep;
1493           else
1494             memcpy (p -= seplen, sep, seplen);
1495           i = 0;
1496           if (*atgroup)
1497             groupsize = *atgroup++;
1498         }
1499     }
1500   if (negative)
1501     *--p = '-';
1502
1503   return p;
1504 }
1505
1506 /* N, a byte quantity, is converted to a human-readable abberviated
1507    form a la sizes printed by `ls -lh'.  The result is written to a
1508    static buffer, a pointer to which is returned.
1509
1510    Unlike `with_thousand_seps', this approximates to the nearest unit.
1511    Quoting GNU libit: "Most people visually process strings of 3-4
1512    digits effectively, but longer strings of digits are more prone to
1513    misinterpretation.  Hence, converting to an abbreviated form
1514    usually improves readability."
1515
1516    This intentionally uses kilobyte (KB), megabyte (MB), etc. in their
1517    original computer-related meaning of "powers of 1024".  We don't
1518    use the "*bibyte" names invented in 1998, and seldom used in
1519    practice.  Wikipedia's entry on "binary prefix" discusses this in
1520    some detail.  */
1521
1522 char *
1523 human_readable (HR_NUMTYPE n)
1524 {
1525   /* These suffixes are compatible with those of GNU `ls -lh'. */
1526   static char powers[] =
1527     {
1528       'K',                      /* kilobyte, 2^10 bytes */
1529       'M',                      /* megabyte, 2^20 bytes */
1530       'G',                      /* gigabyte, 2^30 bytes */
1531       'T',                      /* terabyte, 2^40 bytes */
1532       'P',                      /* petabyte, 2^50 bytes */
1533       'E',                      /* exabyte,  2^60 bytes */
1534     };
1535   static char buf[8];
1536   size_t i;
1537
1538   /* If the quantity is smaller than 1K, just print it. */
1539   if (n < 1024)
1540     {
1541       snprintf (buf, sizeof (buf), "%d", (int) n);
1542       return buf;
1543     }
1544
1545   /* Loop over powers, dividing N with 1024 in each iteration.  This
1546      works unchanged for all sizes of wgint, while still avoiding
1547      non-portable `long double' arithmetic.  */
1548   for (i = 0; i < countof (powers); i++)
1549     {
1550       /* At each iteration N is greater than the *subsequent* power.
1551          That way N/1024.0 produces a decimal number in the units of
1552          *this* power.  */
1553       if ((n / 1024) < 1024 || i == countof (powers) - 1)
1554         {
1555           double val = n / 1024.0;
1556           /* Print values smaller than 10 with one decimal digits, and
1557              others without any decimals.  */
1558           snprintf (buf, sizeof (buf), "%.*f%c",
1559                     val < 10 ? 1 : 0, val, powers[i]);
1560           return buf;
1561         }
1562       n /= 1024;
1563     }
1564   return NULL;                  /* unreached */
1565 }
1566
1567 /* Count the digits in the provided number.  Used to allocate space
1568    when printing numbers.  */
1569
1570 int
1571 numdigit (wgint number)
1572 {
1573   int cnt = 1;
1574   if (number < 0)
1575     ++cnt;                      /* accomodate '-' */
1576   while ((number /= 10) != 0)
1577     ++cnt;
1578   return cnt;
1579 }
1580
1581 #define PR(mask) *p++ = n / (mask) + '0'
1582
1583 /* DIGITS_<D> is used to print a D-digit number and should be called
1584    with mask==10^(D-1).  It prints n/mask (the first digit), reducing
1585    n to n%mask (the remaining digits), and calling DIGITS_<D-1>.
1586    Recursively this continues until DIGITS_1 is invoked.  */
1587
1588 #define DIGITS_1(mask) PR (mask)
1589 #define DIGITS_2(mask) PR (mask), n %= (mask), DIGITS_1 ((mask) / 10)
1590 #define DIGITS_3(mask) PR (mask), n %= (mask), DIGITS_2 ((mask) / 10)
1591 #define DIGITS_4(mask) PR (mask), n %= (mask), DIGITS_3 ((mask) / 10)
1592 #define DIGITS_5(mask) PR (mask), n %= (mask), DIGITS_4 ((mask) / 10)
1593 #define DIGITS_6(mask) PR (mask), n %= (mask), DIGITS_5 ((mask) / 10)
1594 #define DIGITS_7(mask) PR (mask), n %= (mask), DIGITS_6 ((mask) / 10)
1595 #define DIGITS_8(mask) PR (mask), n %= (mask), DIGITS_7 ((mask) / 10)
1596 #define DIGITS_9(mask) PR (mask), n %= (mask), DIGITS_8 ((mask) / 10)
1597 #define DIGITS_10(mask) PR (mask), n %= (mask), DIGITS_9 ((mask) / 10)
1598
1599 /* DIGITS_<11-20> are only used on machines with 64-bit wgints. */
1600
1601 #define DIGITS_11(mask) PR (mask), n %= (mask), DIGITS_10 ((mask) / 10)
1602 #define DIGITS_12(mask) PR (mask), n %= (mask), DIGITS_11 ((mask) / 10)
1603 #define DIGITS_13(mask) PR (mask), n %= (mask), DIGITS_12 ((mask) / 10)
1604 #define DIGITS_14(mask) PR (mask), n %= (mask), DIGITS_13 ((mask) / 10)
1605 #define DIGITS_15(mask) PR (mask), n %= (mask), DIGITS_14 ((mask) / 10)
1606 #define DIGITS_16(mask) PR (mask), n %= (mask), DIGITS_15 ((mask) / 10)
1607 #define DIGITS_17(mask) PR (mask), n %= (mask), DIGITS_16 ((mask) / 10)
1608 #define DIGITS_18(mask) PR (mask), n %= (mask), DIGITS_17 ((mask) / 10)
1609 #define DIGITS_19(mask) PR (mask), n %= (mask), DIGITS_18 ((mask) / 10)
1610
1611 /* Shorthand for casting to wgint. */
1612 #define W wgint
1613
1614 /* Print NUMBER to BUFFER in base 10.  This is equivalent to
1615    `sprintf(buffer, "%lld", (long long) number)', only typically much
1616    faster and portable to machines without long long.
1617
1618    The speedup may make a difference in programs that frequently
1619    convert numbers to strings.  Some implementations of sprintf,
1620    particularly the one in some versions of GNU libc, have been known
1621    to be quite slow when converting integers to strings.
1622
1623    Return the pointer to the location where the terminating zero was
1624    printed.  (Equivalent to calling buffer+strlen(buffer) after the
1625    function is done.)
1626
1627    BUFFER should be large enough to accept as many bytes as you expect
1628    the number to take up.  On machines with 64-bit wgints the maximum
1629    needed size is 24 bytes.  That includes the digits needed for the
1630    largest 64-bit number, the `-' sign in case it's negative, and the
1631    terminating '\0'.  */
1632
1633 char *
1634 number_to_string (char *buffer, wgint number)
1635 {
1636   char *p = buffer;
1637   wgint n = number;
1638
1639   int last_digit_char = 0;
1640
1641 #if (SIZEOF_WGINT != 4) && (SIZEOF_WGINT != 8)
1642   /* We are running in a very strange environment.  Leave the correct
1643      printing to sprintf.  */
1644   p += sprintf (buf, "%j", (intmax_t) (n));
1645 #else  /* (SIZEOF_WGINT == 4) || (SIZEOF_WGINT == 8) */
1646
1647   if (n < 0)
1648     {
1649       if (n < -WGINT_MAX)
1650         {
1651           /* n = -n would overflow because -n would evaluate to a
1652              wgint value larger than WGINT_MAX.  Need to make n
1653              smaller and handle the last digit separately.  */
1654           int last_digit = n % 10;
1655           /* The sign of n%10 is implementation-defined. */
1656           if (last_digit < 0)
1657             last_digit_char = '0' - last_digit;
1658           else
1659             last_digit_char = '0' + last_digit;
1660           /* After n is made smaller, -n will not overflow. */
1661           n /= 10;
1662         }
1663
1664       *p++ = '-';
1665       n = -n;
1666     }
1667
1668   /* Use the DIGITS_ macro appropriate for N's number of digits.  That
1669      way printing any N is fully open-coded without a loop or jump.
1670      (Also see description of DIGITS_*.)  */
1671
1672   if      (n < 10)                       DIGITS_1 (1);
1673   else if (n < 100)                      DIGITS_2 (10);
1674   else if (n < 1000)                     DIGITS_3 (100);
1675   else if (n < 10000)                    DIGITS_4 (1000);
1676   else if (n < 100000)                   DIGITS_5 (10000);
1677   else if (n < 1000000)                  DIGITS_6 (100000);
1678   else if (n < 10000000)                 DIGITS_7 (1000000);
1679   else if (n < 100000000)                DIGITS_8 (10000000);
1680   else if (n < 1000000000)               DIGITS_9 (100000000);
1681 #if SIZEOF_WGINT == 4
1682   /* wgint is 32 bits wide: no number has more than 10 digits. */
1683   else                                   DIGITS_10 (1000000000);
1684 #else
1685   /* wgint is 64 bits wide: handle numbers with 9-19 decimal digits.
1686      Constants are constructed by compile-time multiplication to avoid
1687      dealing with different notations for 64-bit constants
1688      (nL/nLL/nI64, depending on the compiler and architecture).  */
1689   else if (n < 10*(W)1000000000)         DIGITS_10 (1000000000);
1690   else if (n < 100*(W)1000000000)        DIGITS_11 (10*(W)1000000000);
1691   else if (n < 1000*(W)1000000000)       DIGITS_12 (100*(W)1000000000);
1692   else if (n < 10000*(W)1000000000)      DIGITS_13 (1000*(W)1000000000);
1693   else if (n < 100000*(W)1000000000)     DIGITS_14 (10000*(W)1000000000);
1694   else if (n < 1000000*(W)1000000000)    DIGITS_15 (100000*(W)1000000000);
1695   else if (n < 10000000*(W)1000000000)   DIGITS_16 (1000000*(W)1000000000);
1696   else if (n < 100000000*(W)1000000000)  DIGITS_17 (10000000*(W)1000000000);
1697   else if (n < 1000000000*(W)1000000000) DIGITS_18 (100000000*(W)1000000000);
1698   else                                   DIGITS_19 (1000000000*(W)1000000000);
1699 #endif
1700
1701   if (last_digit_char)
1702     *p++ = last_digit_char;
1703
1704   *p = '\0';
1705 #endif /* (SIZEOF_WGINT == 4) || (SIZEOF_WGINT == 8) */
1706
1707   return p;
1708 }
1709
1710 #undef PR
1711 #undef W
1712 #undef SPRINTF_WGINT
1713 #undef DIGITS_1
1714 #undef DIGITS_2
1715 #undef DIGITS_3
1716 #undef DIGITS_4
1717 #undef DIGITS_5
1718 #undef DIGITS_6
1719 #undef DIGITS_7
1720 #undef DIGITS_8
1721 #undef DIGITS_9
1722 #undef DIGITS_10
1723 #undef DIGITS_11
1724 #undef DIGITS_12
1725 #undef DIGITS_13
1726 #undef DIGITS_14
1727 #undef DIGITS_15
1728 #undef DIGITS_16
1729 #undef DIGITS_17
1730 #undef DIGITS_18
1731 #undef DIGITS_19
1732
1733 #define RING_SIZE 3
1734
1735 /* Print NUMBER to a statically allocated string and return a pointer
1736    to the printed representation.
1737
1738    This function is intended to be used in conjunction with printf.
1739    It is hard to portably print wgint values:
1740     a) you cannot use printf("%ld", number) because wgint can be long
1741        long on 32-bit machines with LFS.
1742     b) you cannot use printf("%lld", number) because NUMBER could be
1743        long on 32-bit machines without LFS, or on 64-bit machines,
1744        which do not require LFS.  Also, Windows doesn't support %lld.
1745     c) you cannot use printf("%j", (int_max_t) number) because not all
1746        versions of printf support "%j", the most notable being the one
1747        on Windows.
1748     d) you cannot #define WGINT_FMT to the appropriate format and use
1749        printf(WGINT_FMT, number) because that would break translations
1750        for user-visible messages, such as printf("Downloaded: %d
1751        bytes\n", number).
1752
1753    What you should use instead is printf("%s", number_to_static_string
1754    (number)).
1755
1756    CAVEAT: since the function returns pointers to static data, you
1757    must be careful to copy its result before calling it again.
1758    However, to make it more useful with printf, the function maintains
1759    an internal ring of static buffers to return.  That way things like
1760    printf("%s %s", number_to_static_string (num1),
1761    number_to_static_string (num2)) work as expected.  Three buffers
1762    are currently used, which means that "%s %s %s" will work, but "%s
1763    %s %s %s" won't.  If you need to print more than three wgints,
1764    bump the RING_SIZE (or rethink your message.)  */
1765
1766 char *
1767 number_to_static_string (wgint number)
1768 {
1769   static char ring[RING_SIZE][24];
1770   static int ringpos;
1771   char *buf = ring[ringpos];
1772   number_to_string (buf, number);
1773   ringpos = (ringpos + 1) % RING_SIZE;
1774   return buf;
1775 }
1776
1777 /* Converts the byte to bits format if --report-bps option is enabled
1778  */
1779 wgint
1780 convert_to_bits (wgint num)
1781 {
1782   if (opt.report_bps)
1783     return num * 8;
1784   return num;
1785 }
1786
1787 \f
1788 /* Determine the width of the terminal we're running on.  If that's
1789    not possible, return 0.  */
1790
1791 int
1792 determine_screen_width (void)
1793 {
1794   /* If there's a way to get the terminal size using POSIX
1795      tcgetattr(), somebody please tell me.  */
1796 #ifdef TIOCGWINSZ
1797   int fd;
1798   struct winsize wsz;
1799
1800   if (opt.lfilename != NULL)
1801     return 0;
1802
1803   fd = fileno (stderr);
1804   if (ioctl (fd, TIOCGWINSZ, &wsz) < 0)
1805     return 0;                   /* most likely ENOTTY */
1806
1807   return wsz.ws_col;
1808 #elif defined(WINDOWS)
1809   CONSOLE_SCREEN_BUFFER_INFO csbi;
1810   if (!GetConsoleScreenBufferInfo (GetStdHandle (STD_ERROR_HANDLE), &csbi))
1811     return 0;
1812   return csbi.dwSize.X;
1813 #else  /* neither TIOCGWINSZ nor WINDOWS */
1814   return 0;
1815 #endif /* neither TIOCGWINSZ nor WINDOWS */
1816 }
1817 \f
1818 /* Whether the rnd system (either rand or [dl]rand48) has been
1819    seeded.  */
1820 static int rnd_seeded;
1821
1822 /* Return a random number between 0 and MAX-1, inclusive.
1823
1824    If the system does not support lrand48 and MAX is greater than the
1825    value of RAND_MAX+1 on the system, the returned value will be in
1826    the range [0, RAND_MAX].  This may be fixed in a future release.
1827    The random number generator is seeded automatically the first time
1828    it is called.
1829
1830    This uses lrand48 where available, rand elsewhere.  DO NOT use it
1831    for cryptography.  It is only meant to be used in situations where
1832    quality of the random numbers returned doesn't really matter.  */
1833
1834 int
1835 random_number (int max)
1836 {
1837 #ifdef HAVE_DRAND48
1838   if (!rnd_seeded)
1839     {
1840       srand48 ((long) time (NULL) ^ (long) getpid ());
1841       rnd_seeded = 1;
1842     }
1843   return lrand48 () % max;
1844 #else  /* not HAVE_DRAND48 */
1845
1846   double bounded;
1847   int rnd;
1848   if (!rnd_seeded)
1849     {
1850       srand ((unsigned) time (NULL) ^ (unsigned) getpid ());
1851       rnd_seeded = 1;
1852     }
1853   rnd = rand ();
1854
1855   /* Like rand() % max, but uses the high-order bits for better
1856      randomness on architectures where rand() is implemented using a
1857      simple congruential generator.  */
1858
1859   bounded = (double) max * rnd / (RAND_MAX + 1.0);
1860   return (int) bounded;
1861
1862 #endif /* not HAVE_DRAND48 */
1863 }
1864
1865 /* Return a random uniformly distributed floating point number in the
1866    [0, 1) range.  Uses drand48 where available, and a really lame
1867    kludge elsewhere.  */
1868
1869 double
1870 random_float (void)
1871 {
1872 #ifdef HAVE_DRAND48
1873   if (!rnd_seeded)
1874     {
1875       srand48 ((long) time (NULL) ^ (long) getpid ());
1876       rnd_seeded = 1;
1877     }
1878   return drand48 ();
1879 #else  /* not HAVE_DRAND48 */
1880   return (  random_number (10000) / 10000.0
1881           + random_number (10000) / (10000.0 * 10000.0)
1882           + random_number (10000) / (10000.0 * 10000.0 * 10000.0)
1883           + random_number (10000) / (10000.0 * 10000.0 * 10000.0 * 10000.0));
1884 #endif /* not HAVE_DRAND48 */
1885 }
1886 \f
1887 /* Implementation of run_with_timeout, a generic timeout-forcing
1888    routine for systems with Unix-like signal handling.  */
1889
1890 #ifdef USE_SIGNAL_TIMEOUT
1891 # ifdef HAVE_SIGSETJMP
1892 #  define SETJMP(env) sigsetjmp (env, 1)
1893
1894 static sigjmp_buf run_with_timeout_env;
1895
1896 static void
1897 abort_run_with_timeout (int sig)
1898 {
1899   assert (sig == SIGALRM);
1900   siglongjmp (run_with_timeout_env, -1);
1901 }
1902 # else /* not HAVE_SIGSETJMP */
1903 #  define SETJMP(env) setjmp (env)
1904
1905 static jmp_buf run_with_timeout_env;
1906
1907 static void
1908 abort_run_with_timeout (int sig)
1909 {
1910   assert (sig == SIGALRM);
1911   /* We don't have siglongjmp to preserve the set of blocked signals;
1912      if we longjumped out of the handler at this point, SIGALRM would
1913      remain blocked.  We must unblock it manually. */
1914   sigset_t set;
1915   sigemptyset (&set);
1916   sigaddset (&set, SIGALRM);
1917   sigprocmask (SIG_BLOCK, &set, NULL);
1918
1919   /* Now it's safe to longjump. */
1920   longjmp (run_with_timeout_env, -1);
1921 }
1922 # endif /* not HAVE_SIGSETJMP */
1923
1924 /* Arrange for SIGALRM to be delivered in TIMEOUT seconds.  This uses
1925    setitimer where available, alarm otherwise.
1926
1927    TIMEOUT should be non-zero.  If the timeout value is so small that
1928    it would be rounded to zero, it is rounded to the least legal value
1929    instead (1us for setitimer, 1s for alarm).  That ensures that
1930    SIGALRM will be delivered in all cases.  */
1931
1932 static void
1933 alarm_set (double timeout)
1934 {
1935 #ifdef ITIMER_REAL
1936   /* Use the modern itimer interface. */
1937   struct itimerval itv;
1938   xzero (itv);
1939   itv.it_value.tv_sec = (long) timeout;
1940   itv.it_value.tv_usec = 1000000 * (timeout - (long)timeout);
1941   if (itv.it_value.tv_sec == 0 && itv.it_value.tv_usec == 0)
1942     /* Ensure that we wait for at least the minimum interval.
1943        Specifying zero would mean "wait forever".  */
1944     itv.it_value.tv_usec = 1;
1945   setitimer (ITIMER_REAL, &itv, NULL);
1946 #else  /* not ITIMER_REAL */
1947   /* Use the old alarm() interface. */
1948   int secs = (int) timeout;
1949   if (secs == 0)
1950     /* Round TIMEOUTs smaller than 1 to 1, not to zero.  This is
1951        because alarm(0) means "never deliver the alarm", i.e. "wait
1952        forever", which is not what someone who specifies a 0.5s
1953        timeout would expect.  */
1954     secs = 1;
1955   alarm (secs);
1956 #endif /* not ITIMER_REAL */
1957 }
1958
1959 /* Cancel the alarm set with alarm_set. */
1960
1961 static void
1962 alarm_cancel (void)
1963 {
1964 #ifdef ITIMER_REAL
1965   struct itimerval disable;
1966   xzero (disable);
1967   setitimer (ITIMER_REAL, &disable, NULL);
1968 #else  /* not ITIMER_REAL */
1969   alarm (0);
1970 #endif /* not ITIMER_REAL */
1971 }
1972
1973 /* Call FUN(ARG), but don't allow it to run for more than TIMEOUT
1974    seconds.  Returns true if the function was interrupted with a
1975    timeout, false otherwise.
1976
1977    This works by setting up SIGALRM to be delivered in TIMEOUT seconds
1978    using setitimer() or alarm().  The timeout is enforced by
1979    longjumping out of the SIGALRM handler.  This has several
1980    advantages compared to the traditional approach of relying on
1981    signals causing system calls to exit with EINTR:
1982
1983      * The callback function is *forcibly* interrupted after the
1984        timeout expires, (almost) regardless of what it was doing and
1985        whether it was in a syscall.  For example, a calculation that
1986        takes a long time is interrupted as reliably as an IO
1987        operation.
1988
1989      * It works with both SYSV and BSD signals because it doesn't
1990        depend on the default setting of SA_RESTART.
1991
1992      * It doesn't require special handler setup beyond a simple call
1993        to signal().  (It does use sigsetjmp/siglongjmp, but they're
1994        optional.)
1995
1996    The only downside is that, if FUN allocates internal resources that
1997    are normally freed prior to exit from the functions, they will be
1998    lost in case of timeout.  */
1999
2000 bool
2001 run_with_timeout (double timeout, void (*fun) (void *), void *arg)
2002 {
2003   int saved_errno;
2004
2005   if (timeout == 0)
2006     {
2007       fun (arg);
2008       return false;
2009     }
2010
2011   signal (SIGALRM, abort_run_with_timeout);
2012   if (SETJMP (run_with_timeout_env) != 0)
2013     {
2014       /* Longjumped out of FUN with a timeout. */
2015       signal (SIGALRM, SIG_DFL);
2016       return true;
2017     }
2018   alarm_set (timeout);
2019   fun (arg);
2020
2021   /* Preserve errno in case alarm() or signal() modifies it. */
2022   saved_errno = errno;
2023   alarm_cancel ();
2024   signal (SIGALRM, SIG_DFL);
2025   errno = saved_errno;
2026
2027   return false;
2028 }
2029
2030 #else  /* not USE_SIGNAL_TIMEOUT */
2031
2032 #ifndef WINDOWS
2033 /* A stub version of run_with_timeout that just calls FUN(ARG).  Don't
2034    define it under Windows, because Windows has its own version of
2035    run_with_timeout that uses threads.  */
2036
2037 bool
2038 run_with_timeout (double timeout, void (*fun) (void *), void *arg)
2039 {
2040   fun (arg);
2041   return false;
2042 }
2043 #endif /* not WINDOWS */
2044 #endif /* not USE_SIGNAL_TIMEOUT */
2045 \f
2046 #ifndef WINDOWS
2047
2048 /* Sleep the specified amount of seconds.  On machines without
2049    nanosleep(), this may sleep shorter if interrupted by signals.  */
2050
2051 void
2052 xsleep (double seconds)
2053 {
2054 #ifdef HAVE_NANOSLEEP
2055   /* nanosleep is the preferred interface because it offers high
2056      accuracy and, more importantly, because it allows us to reliably
2057      restart receiving a signal such as SIGWINCH.  (There was an
2058      actual Debian bug report about --limit-rate malfunctioning while
2059      the terminal was being resized.)  */
2060   struct timespec sleep, remaining;
2061   sleep.tv_sec = (long) seconds;
2062   sleep.tv_nsec = 1000000000 * (seconds - (long) seconds);
2063   while (nanosleep (&sleep, &remaining) < 0 && errno == EINTR)
2064     /* If nanosleep has been interrupted by a signal, adjust the
2065        sleeping period and return to sleep.  */
2066     sleep = remaining;
2067 #elif defined(HAVE_USLEEP)
2068   /* If usleep is available, use it in preference to select.  */
2069   if (seconds >= 1)
2070     {
2071       /* On some systems, usleep cannot handle values larger than
2072          1,000,000.  If the period is larger than that, use sleep
2073          first, then add usleep for subsecond accuracy.  */
2074       sleep (seconds);
2075       seconds -= (long) seconds;
2076     }
2077   usleep (seconds * 1000000);
2078 #else /* fall back select */
2079   /* Note that, although Windows supports select, it can't be used to
2080      implement sleeping because Winsock's select doesn't implement
2081      timeout when it is passed NULL pointers for all fd sets.  (But it
2082      does under Cygwin, which implements Unix-compatible select.)  */
2083   struct timeval sleep;
2084   sleep.tv_sec = (long) seconds;
2085   sleep.tv_usec = 1000000 * (seconds - (long) seconds);
2086   select (0, NULL, NULL, NULL, &sleep);
2087   /* If select returns -1 and errno is EINTR, it means we were
2088      interrupted by a signal.  But without knowing how long we've
2089      actually slept, we can't return to sleep.  Using gettimeofday to
2090      track sleeps is slow and unreliable due to clock skew.  */
2091 #endif
2092 }
2093
2094 #endif /* not WINDOWS */
2095
2096 /* Encode the octets in DATA of length LENGTH to base64 format,
2097    storing the result to DEST.  The output will be zero-terminated,
2098    and must point to a writable buffer of at least
2099    1+BASE64_LENGTH(length) bytes.  The function returns the length of
2100    the resulting base64 data, not counting the terminating zero.
2101
2102    This implementation does not emit newlines after 76 characters of
2103    base64 data.  */
2104
2105 int
2106 base64_encode (const void *data, int length, char *dest)
2107 {
2108   /* Conversion table.  */
2109   static const char tbl[64] = {
2110     'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
2111     'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
2112     'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
2113     'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
2114   };
2115   /* Access bytes in DATA as unsigned char, otherwise the shifts below
2116      don't work for data with MSB set. */
2117   const unsigned char *s = data;
2118   /* Theoretical ANSI violation when length < 3. */
2119   const unsigned char *end = (const unsigned char *) data + length - 2;
2120   char *p = dest;
2121
2122   /* Transform the 3x8 bits to 4x6 bits, as required by base64.  */
2123   for (; s < end; s += 3)
2124     {
2125       *p++ = tbl[s[0] >> 2];
2126       *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
2127       *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
2128       *p++ = tbl[s[2] & 0x3f];
2129     }
2130
2131   /* Pad the result if necessary...  */
2132   switch (length % 3)
2133     {
2134     case 1:
2135       *p++ = tbl[s[0] >> 2];
2136       *p++ = tbl[(s[0] & 3) << 4];
2137       *p++ = '=';
2138       *p++ = '=';
2139       break;
2140     case 2:
2141       *p++ = tbl[s[0] >> 2];
2142       *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
2143       *p++ = tbl[((s[1] & 0xf) << 2)];
2144       *p++ = '=';
2145       break;
2146     }
2147   /* ...and zero-terminate it.  */
2148   *p = '\0';
2149
2150   return p - dest;
2151 }
2152
2153 /* Store in C the next non-whitespace character from the string, or \0
2154    when end of string is reached.  */
2155 #define NEXT_CHAR(c, p) do {                    \
2156   c = (unsigned char) *p++;                     \
2157 } while (c_isspace (c))
2158
2159 #define IS_ASCII(c) (((c) & 0x80) == 0)
2160
2161 /* Decode data from BASE64 (a null-terminated string) into memory
2162    pointed to by DEST.  DEST is assumed to be large enough to
2163    accomodate the decoded data, which is guaranteed to be no more than
2164    3/4*strlen(base64).
2165
2166    Since DEST is assumed to contain binary data, it is not
2167    NUL-terminated.  The function returns the length of the data
2168    written to TO.  -1 is returned in case of error caused by malformed
2169    base64 input.
2170
2171    This function originates from Free Recode.  */
2172
2173 int
2174 base64_decode (const char *base64, void *dest)
2175 {
2176   /* Table of base64 values for first 128 characters.  Note that this
2177      assumes ASCII (but so does Wget in other places).  */
2178   static const signed char base64_char_to_value[128] =
2179     {
2180       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  /*   0-  9 */
2181       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  /*  10- 19 */
2182       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  /*  20- 29 */
2183       -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  /*  30- 39 */
2184       -1,  -1,  -1,  62,  -1,  -1,  -1,  63,  52,  53,  /*  40- 49 */
2185       54,  55,  56,  57,  58,  59,  60,  61,  -1,  -1,  /*  50- 59 */
2186       -1,  -1,  -1,  -1,  -1,  0,   1,   2,   3,   4,   /*  60- 69 */
2187       5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  /*  70- 79 */
2188       15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  /*  80- 89 */
2189       25,  -1,  -1,  -1,  -1,  -1,  -1,  26,  27,  28,  /*  90- 99 */
2190       29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  /* 100-109 */
2191       39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  /* 110-119 */
2192       49,  50,  51,  -1,  -1,  -1,  -1,  -1             /* 120-127 */
2193     };
2194 #define BASE64_CHAR_TO_VALUE(c) ((int) base64_char_to_value[c])
2195 #define IS_BASE64(c) ((IS_ASCII (c) && BASE64_CHAR_TO_VALUE (c) >= 0) || c == '=')
2196
2197   const char *p = base64;
2198   char *q = dest;
2199
2200   while (1)
2201     {
2202       unsigned char c;
2203       unsigned long value;
2204
2205       /* Process first byte of a quadruplet.  */
2206       NEXT_CHAR (c, p);
2207       if (!c)
2208         break;
2209       if (c == '=' || !IS_BASE64 (c))
2210         return -1;              /* illegal char while decoding base64 */
2211       value = BASE64_CHAR_TO_VALUE (c) << 18;
2212
2213       /* Process second byte of a quadruplet.  */
2214       NEXT_CHAR (c, p);
2215       if (!c)
2216         return -1;              /* premature EOF while decoding base64 */
2217       if (c == '=' || !IS_BASE64 (c))
2218         return -1;              /* illegal char while decoding base64 */
2219       value |= BASE64_CHAR_TO_VALUE (c) << 12;
2220       *q++ = value >> 16;
2221
2222       /* Process third byte of a quadruplet.  */
2223       NEXT_CHAR (c, p);
2224       if (!c)
2225         return -1;              /* premature EOF while decoding base64 */
2226       if (!IS_BASE64 (c))
2227         return -1;              /* illegal char while decoding base64 */
2228
2229       if (c == '=')
2230         {
2231           NEXT_CHAR (c, p);
2232           if (!c)
2233             return -1;          /* premature EOF while decoding base64 */
2234           if (c != '=')
2235             return -1;          /* padding `=' expected but not found */
2236           continue;
2237         }
2238
2239       value |= BASE64_CHAR_TO_VALUE (c) << 6;
2240       *q++ = 0xff & value >> 8;
2241
2242       /* Process fourth byte of a quadruplet.  */
2243       NEXT_CHAR (c, p);
2244       if (!c)
2245         return -1;              /* premature EOF while decoding base64 */
2246       if (c == '=')
2247         continue;
2248       if (!IS_BASE64 (c))
2249         return -1;              /* illegal char while decoding base64 */
2250
2251       value |= BASE64_CHAR_TO_VALUE (c);
2252       *q++ = 0xff & value;
2253     }
2254 #undef IS_BASE64
2255 #undef BASE64_CHAR_TO_VALUE
2256
2257   return q - (char *) dest;
2258 }
2259
2260 #ifdef HAVE_LIBPCRE
2261 /* Compiles the PCRE regex. */
2262 void *
2263 compile_pcre_regex (const char *str)
2264 {
2265   const char *errbuf;
2266   int erroffset;
2267   pcre *regex = pcre_compile (str, 0, &errbuf, &erroffset, 0);
2268   if (! regex)
2269     {
2270       fprintf (stderr, _("Invalid regular expression %s, %s\n"),
2271                quote (str), errbuf);
2272       return false;
2273     }
2274   return regex;
2275 }
2276 #endif
2277
2278 /* Compiles the POSIX regex. */
2279 void *
2280 compile_posix_regex (const char *str)
2281 {
2282   regex_t *regex = xmalloc (sizeof (regex_t));
2283   int errcode = regcomp ((regex_t *) regex, str, REG_EXTENDED | REG_NOSUB);
2284   if (errcode != 0)
2285     {
2286       int errbuf_size = regerror (errcode, (regex_t *) regex, NULL, 0);
2287       char *errbuf = xmalloc (errbuf_size);
2288       regerror (errcode, (regex_t *) regex, errbuf, errbuf_size);
2289       fprintf (stderr, _("Invalid regular expression %s, %s\n"),
2290                quote (str), errbuf);
2291       xfree (errbuf);
2292       return NULL;
2293     }
2294
2295   return regex;
2296 }
2297
2298 #ifdef HAVE_LIBPCRE
2299 #define OVECCOUNT 30
2300 /* Matches a PCRE regex.  */
2301 bool
2302 match_pcre_regex (const void *regex, const char *str)
2303 {
2304   int l = strlen (str);
2305   int ovector[OVECCOUNT];
2306
2307   int rc = pcre_exec ((pcre *) regex, 0, str, l, 0, 0, ovector, OVECCOUNT);
2308   if (rc == PCRE_ERROR_NOMATCH)
2309     return false;
2310   else if (rc < 0)
2311     {
2312       logprintf (LOG_VERBOSE, _("Error while matching %s: %d\n"),
2313                  quote (str), rc);
2314       return false;
2315     }
2316   else
2317     return true;
2318 }
2319 #undef OVECCOUNT
2320 #endif
2321
2322 /* Matches a POSIX regex.  */
2323 bool
2324 match_posix_regex (const void *regex, const char *str)
2325 {
2326   int rc = regexec ((regex_t *) regex, str, 0, NULL, 0);
2327   if (rc == REG_NOMATCH)
2328     return false;
2329   else if (rc == 0)
2330     return true;
2331   else
2332     {
2333       int errbuf_size = regerror (rc, opt.acceptregex, NULL, 0);
2334       char *errbuf = xmalloc (errbuf_size);
2335       regerror (rc, opt.acceptregex, errbuf, errbuf_size);
2336       logprintf (LOG_VERBOSE, _("Error while matching %s: %d\n"),
2337                  quote (str), rc);
2338       xfree (errbuf);
2339       return false;
2340     }
2341 }
2342
2343 #undef IS_ASCII
2344 #undef NEXT_CHAR
2345 \f
2346 /* Simple merge sort for use by stable_sort.  Implementation courtesy
2347    Zeljko Vrba with additional debugging by Nenad Barbutov.  */
2348
2349 static void
2350 mergesort_internal (void *base, void *temp, size_t size, size_t from, size_t to,
2351                     int (*cmpfun) (const void *, const void *))
2352 {
2353 #define ELT(array, pos) ((char *)(array) + (pos) * size)
2354   if (from < to)
2355     {
2356       size_t i, j, k;
2357       size_t mid = (to + from) / 2;
2358       mergesort_internal (base, temp, size, from, mid, cmpfun);
2359       mergesort_internal (base, temp, size, mid + 1, to, cmpfun);
2360       i = from;
2361       j = mid + 1;
2362       for (k = from; (i <= mid) && (j <= to); k++)
2363         if (cmpfun (ELT (base, i), ELT (base, j)) <= 0)
2364           memcpy (ELT (temp, k), ELT (base, i++), size);
2365         else
2366           memcpy (ELT (temp, k), ELT (base, j++), size);
2367       while (i <= mid)
2368         memcpy (ELT (temp, k++), ELT (base, i++), size);
2369       while (j <= to)
2370         memcpy (ELT (temp, k++), ELT (base, j++), size);
2371       for (k = from; k <= to; k++)
2372         memcpy (ELT (base, k), ELT (temp, k), size);
2373     }
2374 #undef ELT
2375 }
2376
2377 /* Stable sort with interface exactly like standard library's qsort.
2378    Uses mergesort internally, allocating temporary storage with
2379    alloca.  */
2380
2381 void
2382 stable_sort (void *base, size_t nmemb, size_t size,
2383              int (*cmpfun) (const void *, const void *))
2384 {
2385   if (size > 1)
2386     {
2387       void *temp = alloca (nmemb * size * sizeof (void *));
2388       mergesort_internal (base, temp, size, 0, nmemb - 1, cmpfun);
2389     }
2390 }
2391 \f
2392 /* Print a decimal number.  If it is equal to or larger than ten, the
2393    number is rounded.  Otherwise it is printed with one significant
2394    digit without trailing zeros and with no more than three fractional
2395    digits total.  For example, 0.1 is printed as "0.1", 0.035 is
2396    printed as "0.04", 0.0091 as "0.009", and 0.0003 as simply "0".
2397
2398    This is useful for displaying durations because it provides
2399    order-of-magnitude information without unnecessary clutter --
2400    long-running downloads are shown without the fractional part, and
2401    short ones still retain one significant digit.  */
2402
2403 const char *
2404 print_decimal (double number)
2405 {
2406   static char buf[32];
2407   double n = number >= 0 ? number : -number;
2408
2409   if (n >= 9.95)
2410     /* Cut off at 9.95 because the below %.1f would round 9.96 to
2411        "10.0" instead of "10".  OTOH 9.94 will print as "9.9".  */
2412     snprintf (buf, sizeof buf, "%.0f", number);
2413   else if (n >= 0.95)
2414     snprintf (buf, sizeof buf, "%.1f", number);
2415   else if (n >= 0.001)
2416     snprintf (buf, sizeof buf, "%.1g", number);
2417   else if (n >= 0.0005)
2418     /* round [0.0005, 0.001) to 0.001 */
2419     snprintf (buf, sizeof buf, "%.3f", number);
2420   else
2421     /* print numbers close to 0 as 0, not 0.000 */
2422     strcpy (buf, "0");
2423
2424   return buf;
2425 }
2426
2427 /* Get the maximum name length for the given path. */
2428 /* Return 0 if length is unknown. */
2429 size_t
2430 get_max_length (const char *path, int length, int name)
2431 {
2432   long ret;
2433   char *p, *d;
2434
2435   /* Make a copy of the path that we can modify. */
2436   p = path ? strdupdelim (path, path + length) : strdup ("");
2437
2438   for (;;)
2439     {
2440       errno = 0;
2441       /* For an empty path query the current directory. */
2442 #if HAVE_PATHCONF
2443       ret = pathconf (*p ? p : ".", name);
2444       if (!(ret < 0 && errno == ENOENT))
2445         break;
2446 #else
2447       ret = PATH_MAX;
2448 #endif
2449
2450       /* The path does not exist yet, but may be created. */
2451       /* Already at current or root directory, give up. */
2452       if (!*p || strcmp (p, "/") == 0)
2453         break;
2454
2455       /* Remove one directory level and try again. */
2456       d = strrchr (p, '/');
2457       if (d == p)
2458         p[1] = '\0';  /* check root directory */
2459       else if (d)
2460         *d = '\0';  /* remove last directory part */
2461       else
2462         *p = '\0';  /* check current directory */
2463     }
2464
2465   xfree (p);
2466
2467   if (ret < 0)
2468     {
2469       /* pathconf() has a message for us. */
2470       if (errno != 0)
2471           perror ("pathconf");
2472
2473       /* If (errno == 0) then there is no max length.
2474          Even on error return 0 so the caller can continue. */
2475       return 0;
2476     }
2477
2478   return ret;
2479 }
2480
2481 #ifdef TESTING
2482
2483 const char *
2484 test_subdir_p()
2485 {
2486   static struct {
2487     const char *d1;
2488     const char *d2;
2489     bool result;
2490   } test_array[] = {
2491     { "/somedir", "/somedir", true },
2492     { "/somedir", "/somedir/d2", true },
2493     { "/somedir/d1", "/somedir", false },
2494   };
2495   unsigned i;
2496
2497   for (i = 0; i < countof(test_array); ++i)
2498     {
2499       bool res = subdir_p (test_array[i].d1, test_array[i].d2);
2500
2501       mu_assert ("test_subdir_p: wrong result",
2502                  res == test_array[i].result);
2503     }
2504
2505   return NULL;
2506 }
2507
2508 const char *
2509 test_dir_matches_p()
2510 {
2511   static struct {
2512     char *dirlist[3];
2513     const char *dir;
2514     bool result;
2515   } test_array[] = {
2516     { { "/somedir", "/someotherdir", NULL }, "somedir", true },
2517     { { "/somedir", "/someotherdir", NULL }, "anotherdir", false },
2518     { { "/somedir", "/*otherdir", NULL }, "anotherdir", true },
2519     { { "/somedir/d1", "/someotherdir", NULL }, "somedir/d1", true },
2520     { { "*/*d1", "/someotherdir", NULL }, "somedir/d1", true },
2521     { { "/somedir/d1", "/someotherdir", NULL }, "d1", false },
2522     { { "!COMPLETE", NULL, NULL }, "!COMPLETE", true },
2523     { { "*COMPLETE", NULL, NULL }, "!COMPLETE", true },
2524     { { "*/!COMPLETE", NULL, NULL }, "foo/!COMPLETE", true },
2525     { { "*COMPLETE", NULL, NULL }, "foo/!COMPLETE", false },
2526     { { "*/*COMPLETE", NULL, NULL }, "foo/!COMPLETE", true },
2527     { { "/dir with spaces", NULL, NULL }, "dir with spaces", true },
2528     { { "/dir*with*spaces", NULL, NULL }, "dir with spaces", true },
2529     { { "/Tmp/has", NULL, NULL }, "/Tmp/has space", false },
2530     { { "/Tmp/has", NULL, NULL }, "/Tmp/has,comma", false },
2531   };
2532   unsigned i;
2533
2534   for (i = 0; i < countof(test_array); ++i)
2535     {
2536       bool res = dir_matches_p (test_array[i].dirlist, test_array[i].dir);
2537
2538       mu_assert ("test_dir_matches_p: wrong result",
2539                  res == test_array[i].result);
2540     }
2541
2542   return NULL;
2543 }
2544
2545 #endif /* TESTING */
2546