~ubuntu-branches/ubuntu/precise/wget/precise-proposed

« back to all changes in this revision

Viewing changes to src/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2005-06-26 16:46:25 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050626164625-jjcde8hyztx7xq7o
Tags: 1.10-2
* wget-fix_error--save-headers patch from upstream
  (closes: Bug#314728)
* don't pattern-match server redirects patch from upstream
  (closes: Bug#163243)
* correct de.po typos
  (closes: Bug#313883)
* wget-E_html_behind_file_counting fix problem with adding the
  numbers after the html extension
* updated Standards-Version: to 3.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Various functions of utilitarian nature.
2
 
   Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001
3
 
   Free Software Foundation, Inc.
 
1
/* Various utility functions.
 
2
   Copyright (C) 2005 Free Software Foundation, Inc.
4
3
 
5
4
This file is part of GNU Wget.
6
5
 
47
46
#ifdef HAVE_PWD_H
48
47
# include <pwd.h>
49
48
#endif
50
 
#include <limits.h>
 
49
#ifdef HAVE_LIMITS_H
 
50
# include <limits.h>
 
51
#endif
51
52
#ifdef HAVE_UTIME_H
52
53
# include <utime.h>
53
54
#endif
60
61
#endif
61
62
#include <fcntl.h>
62
63
#include <assert.h>
 
64
#ifdef WGET_USE_STDARG
 
65
# include <stdarg.h>
 
66
#else
 
67
# include <varargs.h>
 
68
#endif
63
69
 
64
70
/* For TIOCGWINSZ and friends: */
65
71
#ifdef HAVE_SYS_IOCTL_H
102
108
extern int errno;
103
109
#endif
104
110
 
105
 
/* This section implements several wrappers around the basic
106
 
   allocation routines.  This is done for two reasons: first, so that
107
 
   the callers of these functions need not consistently check for
108
 
   errors.  If there is not enough virtual memory for running Wget,
109
 
   something is seriously wrong, and Wget exits with an appropriate
110
 
   error message.
111
 
 
112
 
   The second reason why these are useful is that, if DEBUG_MALLOC is
113
 
   defined, they also provide a handy (if crude) malloc debugging
114
 
   interface that checks memory leaks.  */
115
 
 
116
 
/* Croak the fatal memory error and bail out with non-zero exit
117
 
   status.  */
118
 
static void
119
 
memfatal (const char *what)
120
 
{
121
 
  /* Make sure we don't try to store part of the log line, and thus
122
 
     call malloc.  */
123
 
  log_set_save_context (0);
124
 
  logprintf (LOG_ALWAYS, _("%s: %s: Not enough memory.\n"), exec_name, what);
125
 
  exit (1);
126
 
}
127
 
 
128
 
/* These functions end with _real because they need to be
129
 
   distinguished from the debugging functions, and from the macros.
130
 
   Explanation follows:
131
 
 
132
 
   If memory debugging is not turned on, wget.h defines these:
133
 
 
134
 
     #define xmalloc xmalloc_real
135
 
     #define xrealloc xrealloc_real
136
 
     #define xstrdup xstrdup_real
137
 
     #define xfree free
138
 
 
139
 
   In case of memory debugging, the definitions are a bit more
140
 
   complex, because we want to provide more information, *and* we want
141
 
   to call the debugging code.  (The former is the reason why xmalloc
142
 
   and friends need to be macros in the first place.)  Then it looks
143
 
   like this:
144
 
 
145
 
     #define xmalloc(a) xmalloc_debug (a, __FILE__, __LINE__)
146
 
     #define xfree(a)   xfree_debug (a, __FILE__, __LINE__)
147
 
     #define xrealloc(a, b) xrealloc_debug (a, b, __FILE__, __LINE__)
148
 
     #define xstrdup(a) xstrdup_debug (a, __FILE__, __LINE__)
149
 
 
150
 
   Each of the *_debug function does its magic and calls the real one.  */
151
 
 
152
 
#ifdef DEBUG_MALLOC
153
 
# define STATIC_IF_DEBUG static
154
 
#else
155
 
# define STATIC_IF_DEBUG
156
 
#endif
157
 
 
158
 
STATIC_IF_DEBUG void *
159
 
xmalloc_real (size_t size)
160
 
{
161
 
  void *ptr = malloc (size);
162
 
  if (!ptr)
163
 
    memfatal ("malloc");
164
 
  return ptr;
165
 
}
166
 
 
167
 
STATIC_IF_DEBUG void *
168
 
xrealloc_real (void *ptr, size_t newsize)
169
 
{
170
 
  void *newptr;
171
 
 
172
 
  /* Not all Un*xes have the feature of realloc() that calling it with
173
 
     a NULL-pointer is the same as malloc(), but it is easy to
174
 
     simulate.  */
175
 
  if (ptr)
176
 
    newptr = realloc (ptr, newsize);
177
 
  else
178
 
    newptr = malloc (newsize);
179
 
  if (!newptr)
180
 
    memfatal ("realloc");
181
 
  return newptr;
182
 
}
183
 
 
184
 
STATIC_IF_DEBUG char *
185
 
xstrdup_real (const char *s)
186
 
{
187
 
  char *copy;
188
 
 
189
 
#ifndef HAVE_STRDUP
190
 
  int l = strlen (s);
191
 
  copy = malloc (l + 1);
192
 
  if (!copy)
193
 
    memfatal ("strdup");
194
 
  memcpy (copy, s, l + 1);
195
 
#else  /* HAVE_STRDUP */
196
 
  copy = strdup (s);
197
 
  if (!copy)
198
 
    memfatal ("strdup");
199
 
#endif /* HAVE_STRDUP */
200
 
 
201
 
  return copy;
202
 
}
203
 
 
204
 
#ifdef DEBUG_MALLOC
205
 
 
206
 
/* Crude home-grown routines for debugging some malloc-related
207
 
   problems.  Featured:
208
 
 
209
 
   * Counting the number of malloc and free invocations, and reporting
210
 
     the "balance", i.e. how many times more malloc was called than it
211
 
     was the case with free.
212
 
 
213
 
   * Making malloc store its entry into a simple array and free remove
214
 
     stuff from that array.  At the end, print the pointers which have
215
 
     not been freed, along with the source file and the line number.
216
 
     This also has the side-effect of detecting freeing memory that
217
 
     was never allocated.
218
 
 
219
 
   Note that this kind of memory leak checking strongly depends on
220
 
   every malloc() being followed by a free(), even if the program is
221
 
   about to finish.  Wget is careful to free the data structure it
222
 
   allocated in init.c.  */
223
 
 
224
 
static int malloc_count, free_count;
225
 
 
226
 
static struct {
227
 
  char *ptr;
228
 
  const char *file;
229
 
  int line;
230
 
} malloc_debug[100000];
231
 
 
232
 
/* Both register_ptr and unregister_ptr take O(n) operations to run,
233
 
   which can be a real problem.  It would be nice to use a hash table
234
 
   for malloc_debug, but the functions in hash.c are not suitable
235
 
   because they can call malloc() themselves.  Maybe it would work if
236
 
   the hash table were preallocated to a huge size, and if we set the
237
 
   rehash threshold to 1.0.  */
238
 
 
239
 
/* Register PTR in malloc_debug.  Abort if this is not possible
240
 
   (presumably due to the number of current allocations exceeding the
241
 
   size of malloc_debug.)  */
242
 
 
243
 
static void
244
 
register_ptr (void *ptr, const char *file, int line)
245
 
{
246
 
  int i;
247
 
  for (i = 0; i < countof (malloc_debug); i++)
248
 
    if (malloc_debug[i].ptr == NULL)
249
 
      {
250
 
        malloc_debug[i].ptr = ptr;
251
 
        malloc_debug[i].file = file;
252
 
        malloc_debug[i].line = line;
253
 
        return;
254
 
      }
255
 
  abort ();
256
 
}
257
 
 
258
 
/* Unregister PTR from malloc_debug.  Abort if PTR is not present in
259
 
   malloc_debug.  (This catches calling free() with a bogus pointer.)  */
260
 
 
261
 
static void
262
 
unregister_ptr (void *ptr)
263
 
{
264
 
  int i;
265
 
  for (i = 0; i < countof (malloc_debug); i++)
266
 
    if (malloc_debug[i].ptr == ptr)
267
 
      {
268
 
        malloc_debug[i].ptr = NULL;
269
 
        return;
270
 
      }
271
 
  abort ();
272
 
}
273
 
 
274
 
/* Print the malloc debug stats that can be gathered from the above
275
 
   information.  Currently this is the count of mallocs, frees, the
276
 
   difference between the two, and the dump of the contents of
277
 
   malloc_debug.  The last part are the memory leaks.  */
278
 
 
279
 
void
280
 
print_malloc_debug_stats (void)
281
 
{
282
 
  int i;
283
 
  printf ("\nMalloc:  %d\nFree:    %d\nBalance: %d\n\n",
284
 
          malloc_count, free_count, malloc_count - free_count);
285
 
  for (i = 0; i < countof (malloc_debug); i++)
286
 
    if (malloc_debug[i].ptr != NULL)
287
 
      printf ("0x%08ld: %s:%d\n", (long)malloc_debug[i].ptr,
288
 
              malloc_debug[i].file, malloc_debug[i].line);
289
 
}
290
 
 
291
 
void *
292
 
xmalloc_debug (size_t size, const char *source_file, int source_line)
293
 
{
294
 
  void *ptr = xmalloc_real (size);
295
 
  ++malloc_count;
296
 
  register_ptr (ptr, source_file, source_line);
297
 
  return ptr;
298
 
}
299
 
 
300
 
void
301
 
xfree_debug (void *ptr, const char *source_file, int source_line)
302
 
{
303
 
  assert (ptr != NULL);
304
 
  ++free_count;
305
 
  unregister_ptr (ptr);
306
 
  free (ptr);
307
 
}
308
 
 
309
 
void *
310
 
xrealloc_debug (void *ptr, size_t newsize, const char *source_file, int source_line)
311
 
{
312
 
  void *newptr = xrealloc_real (ptr, newsize);
313
 
  if (!ptr)
314
 
    {
315
 
      ++malloc_count;
316
 
      register_ptr (newptr, source_file, source_line);
317
 
    }
318
 
  else if (newptr != ptr)
319
 
    {
320
 
      unregister_ptr (ptr);
321
 
      register_ptr (newptr, source_file, source_line);
322
 
    }
323
 
  return newptr;
324
 
}
325
 
 
326
 
char *
327
 
xstrdup_debug (const char *s, const char *source_file, int source_line)
328
 
{
329
 
  char *copy = xstrdup_real (s);
330
 
  ++malloc_count;
331
 
  register_ptr (copy, source_file, source_line);
332
 
  return copy;
333
 
}
334
 
 
335
 
#endif /* DEBUG_MALLOC */
336
 
 
337
111
/* Utility function: like xstrdup(), but also lowercases S.  */
338
112
 
339
113
char *
346
120
  return copy;
347
121
}
348
122
 
349
 
/* Return a count of how many times CHR occurs in STRING. */
350
 
 
351
 
int
352
 
count_char (const char *string, char chr)
353
 
{
354
 
  const char *p;
355
 
  int count = 0;
356
 
  for (p = string; *p; p++)
357
 
    if (*p == chr)
358
 
      ++count;
359
 
  return count;
360
 
}
361
 
 
362
123
/* Copy the string formed by two pointers (one on the beginning, other
363
124
   on the char after the last char) to a new, malloc-ed location.
364
125
   0-terminate it.  */
407
168
  return res;
408
169
}
409
170
 
 
171
#ifdef WGET_USE_STDARG
 
172
# define VA_START(args, arg1) va_start (args, arg1)
 
173
#else
 
174
# define VA_START(args, ignored) va_start (args)
 
175
#endif
 
176
 
 
177
/* Like sprintf, but allocates a string of sufficient size with malloc
 
178
   and returns it.  GNU libc has a similar function named asprintf,
 
179
   which requires the pointer to the string to be passed.  */
 
180
 
 
181
char *
 
182
aprintf (const char *fmt, ...)
 
183
{
 
184
  /* This function is implemented using vsnprintf, which we provide
 
185
     for the systems that don't have it.  Therefore, it should be 100%
 
186
     portable.  */
 
187
 
 
188
  int size = 32;
 
189
  char *str = xmalloc (size);
 
190
 
 
191
  while (1)
 
192
    {
 
193
      int n;
 
194
      va_list args;
 
195
 
 
196
      /* See log_vprintf_internal for explanation why it's OK to rely
 
197
         on the return value of vsnprintf.  */
 
198
 
 
199
      VA_START (args, fmt);
 
200
      n = vsnprintf (str, size, fmt, args);
 
201
      va_end (args);
 
202
 
 
203
      /* If the printing worked, return the string. */
 
204
      if (n > -1 && n < size)
 
205
        return str;
 
206
 
 
207
      /* Else try again with a larger buffer. */
 
208
      if (n > -1)               /* C99 */
 
209
        size = n + 1;           /* precisely what is needed */
 
210
      else
 
211
        size <<= 1;             /* twice the old size */
 
212
      str = xrealloc (str, size);
 
213
    }
 
214
}
 
215
 
 
216
/* Concatenate the NULL-terminated list of string arguments into
 
217
   freshly allocated space.  */
 
218
 
 
219
char *
 
220
concat_strings (const char *str0, ...)
 
221
{
 
222
  va_list args;
 
223
  int saved_lengths[5];         /* inspired by Apache's apr_pstrcat */
 
224
  char *ret, *p;
 
225
 
 
226
  const char *next_str;
 
227
  int total_length = 0;
 
228
  int argcount;
 
229
 
 
230
  /* Calculate the length of and allocate the resulting string. */
 
231
 
 
232
  argcount = 0;
 
233
  VA_START (args, str0);
 
234
  for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
 
235
    {
 
236
      int len = strlen (next_str);
 
237
      if (argcount < countof (saved_lengths))
 
238
        saved_lengths[argcount++] = len;
 
239
      total_length += len;
 
240
    }
 
241
  va_end (args);
 
242
  p = ret = xmalloc (total_length + 1);
 
243
 
 
244
  /* Copy the strings into the allocated space. */
 
245
 
 
246
  argcount = 0;
 
247
  VA_START (args, str0);
 
248
  for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
 
249
    {
 
250
      int len;
 
251
      if (argcount < countof (saved_lengths))
 
252
        len = saved_lengths[argcount++];
 
253
      else
 
254
        len = strlen (next_str);
 
255
      memcpy (p, next_str, len);
 
256
      p += len;
 
257
    }
 
258
  va_end (args);
 
259
  *p = '\0';
 
260
 
 
261
  return ret;
 
262
}
 
263
 
410
264
/* Return pointer to a static char[] buffer in which zero-terminated
411
265
   string-representation of TM (in form hh:mm:ss) is printed.
412
266
 
413
 
   If TM is non-NULL, the current time-in-seconds will be stored
414
 
   there.
415
 
 
416
 
   (#### This is misleading: one would expect TM would be used instead
417
 
   of the current time in that case.  This design was probably
418
 
   influenced by the design time(2), and should be changed at some
419
 
   points.  No callers use non-NULL TM anyway.)  */
 
267
   If TM is NULL, the current time will be used.  */
420
268
 
421
269
char *
422
270
time_str (time_t *tm)
423
271
{
424
272
  static char output[15];
425
273
  struct tm *ptm;
426
 
  time_t secs = time (tm);
 
274
  time_t secs = tm ? *tm : time (NULL);
427
275
 
428
276
  if (secs == -1)
429
277
    {
444
292
{
445
293
  static char output[20];       /* "YYYY-MM-DD hh:mm:ss" + \0 */
446
294
  struct tm *ptm;
447
 
  time_t secs = time (tm);
 
295
  time_t secs = tm ? *tm : time (NULL);
448
296
 
449
297
  if (secs == -1)
450
298
    {
469
317
{
470
318
  pid_t pid;
471
319
  /* Whether we arrange our own version of opt.lfilename here.  */
472
 
  int changedp = 0;
 
320
  int logfile_changed = 0;
473
321
 
474
322
  if (!opt.lfilename)
475
323
    {
476
 
      opt.lfilename = unique_name (DEFAULT_LOGFILE, 0);
477
 
      changedp = 1;
 
324
      /* We must create the file immediately to avoid either a race
 
325
         condition (which arises from using unique_name and failing to
 
326
         use fopen_excl) or lying to the user about the log file name
 
327
         (which arises from using unique_name, printing the name, and
 
328
         using fopen_excl later on.)  */
 
329
      FILE *new_log_fp = unique_create (DEFAULT_LOGFILE, 0, &opt.lfilename);
 
330
      if (new_log_fp)
 
331
        {
 
332
          logfile_changed = 1;
 
333
          fclose (new_log_fp);
 
334
        }
478
335
    }
479
336
  pid = fork ();
480
337
  if (pid < 0)
487
344
    {
488
345
      /* parent, no error */
489
346
      printf (_("Continuing in background, pid %d.\n"), (int)pid);
490
 
      if (changedp)
 
347
      if (logfile_changed)
491
348
        printf (_("Output will be written to `%s'.\n"), opt.lfilename);
492
349
      exit (0);                 /* #### should we use _exit()? */
493
350
    }
500
357
}
501
358
#endif /* not WINDOWS */
502
359
 
503
 
/* "Touch" FILE, i.e. make its atime and mtime equal to the time
504
 
   specified with TM.  */
 
360
/* "Touch" FILE, i.e. make its mtime ("modified time") equal the time
 
361
   specified with TM.  The atime ("access time") is set to the current
 
362
   time.  */
 
363
 
505
364
void
506
365
touch (const char *file, time_t tm)
507
366
{
508
367
#ifdef HAVE_STRUCT_UTIMBUF
509
368
  struct utimbuf times;
510
 
  times.actime = times.modtime = tm;
511
369
#else
512
 
  time_t times[2];
513
 
  times[0] = times[1] = tm;
 
370
  struct {
 
371
    time_t actime;
 
372
    time_t modtime;
 
373
  } times;
514
374
#endif
515
 
 
 
375
  times.modtime = tm;
 
376
  times.actime = time (NULL);
516
377
  if (utime (file, &times) == -1)
517
378
    logprintf (LOG_NOTQUIET, "utime(%s): %s\n", file, strerror (errno));
518
379
}
523
384
remove_link (const char *file)
524
385
{
525
386
  int err = 0;
526
 
  struct stat st;
 
387
  struct_stat st;
527
388
 
528
389
  if (lstat (file, &st) == 0 && S_ISLNK (st.st_mode))
529
390
    {
549
410
#ifdef HAVE_ACCESS
550
411
  return access (filename, F_OK) >= 0;
551
412
#else
552
 
  struct stat buf;
 
413
  struct_stat buf;
553
414
  return stat (filename, &buf) >= 0;
554
415
#endif
555
416
}
559
420
int
560
421
file_non_directory_p (const char *path)
561
422
{
562
 
  struct stat buf;
 
423
  struct_stat buf;
563
424
  /* Use lstat() rather than stat() so that symbolic links pointing to
564
425
     directories can be identified correctly.  */
565
426
  if (lstat (path, &buf) != 0)
569
430
 
570
431
/* Return the size of file named by FILENAME, or -1 if it cannot be
571
432
   opened or seeked into. */
572
 
long
 
433
wgint
573
434
file_size (const char *filename)
574
435
{
575
 
  long size;
 
436
#if defined(HAVE_FSEEKO) && defined(HAVE_FTELLO)
 
437
  wgint size;
576
438
  /* We use fseek rather than stat to determine the file size because
577
 
     that way we can also verify whether the file is readable.
578
 
     Inspired by the POST patch by Arnaud Wylie.  */
 
439
     that way we can also verify that the file is readable without
 
440
     explicitly checking for permissions.  Inspired by the POST patch
 
441
     by Arnaud Wylie.  */
579
442
  FILE *fp = fopen (filename, "rb");
580
443
  if (!fp)
581
444
    return -1;
582
 
  fseek (fp, 0, SEEK_END);
583
 
  size = ftell (fp);
 
445
  fseeko (fp, 0, SEEK_END);
 
446
  size = ftello (fp);
584
447
  fclose (fp);
585
448
  return size;
 
449
#else
 
450
  struct_stat st;
 
451
  if (stat (filename, &st) < 0)
 
452
    return -1;
 
453
  return st.st_size;
 
454
#endif
586
455
}
587
456
 
588
457
/* stat file names named PREFIX.1, PREFIX.2, etc., until one that
617
486
   exist at the point in time when the function was called.
618
487
   Therefore, where security matters, don't rely that the file created
619
488
   by this function exists until you open it with O_EXCL or
620
 
   something.
 
489
   equivalent.
621
490
 
622
491
   If ALLOW_PASSTHROUGH is 0, it always returns a freshly allocated
623
492
   string.  Otherwise, it may return FILE if the file doesn't exist
635
504
     and return it.  */
636
505
  return unique_name_1 (file);
637
506
}
 
507
 
 
508
/* Create a file based on NAME, except without overwriting an existing
 
509
   file with that name.  Providing O_EXCL is correctly implemented,
 
510
   this function does not have the race condition associated with
 
511
   opening the file returned by unique_name.  */
 
512
 
 
513
FILE *
 
514
unique_create (const char *name, int binary, char **opened_name)
 
515
{
 
516
  /* unique file name, based on NAME */
 
517
  char *uname = unique_name (name, 0);
 
518
  FILE *fp;
 
519
  while ((fp = fopen_excl (uname, binary)) == NULL && errno == EEXIST)
 
520
    {
 
521
      xfree (uname);
 
522
      uname = unique_name (name, 0);
 
523
    }
 
524
  if (opened_name && fp != NULL)
 
525
    {
 
526
      if (fp)
 
527
        *opened_name = uname;
 
528
      else
 
529
        {
 
530
          *opened_name = NULL;
 
531
          xfree (uname);
 
532
        }
 
533
    }
 
534
  else
 
535
    xfree (uname);
 
536
  return fp;
 
537
}
 
538
 
 
539
/* Open the file for writing, with the addition that the file is
 
540
   opened "exclusively".  This means that, if the file already exists,
 
541
   this function will *fail* and errno will be set to EEXIST.  If
 
542
   BINARY is set, the file will be opened in binary mode, equivalent
 
543
   to fopen's "wb".
 
544
 
 
545
   If opening the file fails for any reason, including the file having
 
546
   previously existed, this function returns NULL and sets errno
 
547
   appropriately.  */
 
548
   
 
549
FILE *
 
550
fopen_excl (const char *fname, int binary)
 
551
{
 
552
  int fd;
 
553
#ifdef O_EXCL
 
554
  int flags = O_WRONLY | O_CREAT | O_EXCL;
 
555
# ifdef O_BINARY
 
556
  if (binary)
 
557
    flags |= O_BINARY;
 
558
# endif
 
559
  fd = open (fname, flags, 0666);
 
560
  if (fd < 0)
 
561
    return NULL;
 
562
  return fdopen (fd, binary ? "wb" : "w");
 
563
#else  /* not O_EXCL */
 
564
  /* Manually check whether the file exists.  This is prone to race
 
565
     conditions, but systems without O_EXCL haven't deserved
 
566
     better.  */
 
567
  if (file_exists_p (fname))
 
568
    {
 
569
      errno = EEXIST;
 
570
      return NULL;
 
571
    }
 
572
  return fopen (fname, binary ? "wb" : "w");
 
573
#endif /* not O_EXCL */
 
574
}
638
575
 
639
576
/* Create DIRECTORY.  If some of the pathname components of DIRECTORY
640
577
   are missing, create them first.  In case any mkdir() call fails,
645
582
int
646
583
make_directory (const char *directory)
647
584
{
648
 
  int quit = 0;
649
 
  int i;
650
 
  int ret = 0;
 
585
  int i, ret, quit = 0;
651
586
  char *dir;
652
587
 
653
588
  /* Make a copy of dir, to be able to write to it.  Otherwise, the
746
681
proclist (char **strlist, const char *s, enum accd flags)
747
682
{
748
683
  char **x;
749
 
 
750
684
  for (x = strlist; *x; x++)
751
 
    if (has_wildcards_p (*x))
752
 
      {
753
 
        if (fnmatch (*x, s, FNM_PATHNAME) == 0)
754
 
          break;
755
 
      }
756
 
    else
757
 
      {
758
 
        char *p = *x + ((flags & ALLABS) && (**x == '/')); /* Remove '/' */
759
 
        if (frontcmp (p, s))
760
 
          break;
761
 
      }
 
685
    {
 
686
      /* Remove leading '/' if ALLABS */
 
687
      char *p = *x + ((flags & ALLABS) && (**x == '/'));
 
688
      if (has_wildcards_p (p))
 
689
        {
 
690
          if (fnmatch (p, s, FNM_PATHNAME) == 0)
 
691
            break;
 
692
        }
 
693
      else
 
694
        {
 
695
          if (frontcmp (p, s))
 
696
            break;
 
697
        }
 
698
    }
762
699
  return *x;
763
700
}
764
701
 
1001
938
    fd = open (file, O_RDONLY);
1002
939
  if (fd < 0)
1003
940
    return NULL;
1004
 
  fm = xmalloc (sizeof (struct file_memory));
 
941
  fm = xnew (struct file_memory);
1005
942
 
1006
943
#ifdef HAVE_MMAP
1007
944
  {
1008
 
    struct stat buf;
 
945
    struct_stat buf;
1009
946
    if (fstat (fd, &buf) < 0)
1010
947
      goto mmap_lose;
1011
948
    fm->length = buf.st_size;
1037
974
  fm->content = xmalloc (size);
1038
975
  while (1)
1039
976
    {
1040
 
      long nread;
 
977
      wgint nread;
1041
978
      if (fm->length > size / 2)
1042
979
        {
1043
980
          /* #### I'm not sure whether the whole exponential-growth
1149
1086
  return v1;
1150
1087
}
1151
1088
 
1152
 
/* A set of simple-minded routines to store strings in a linked list.
1153
 
   This used to also be used for searching, but now we have hash
1154
 
   tables for that.  */
1155
 
 
1156
 
/* It's a shame that these simple things like linked lists and hash
1157
 
   tables (see hash.c) need to be implemented over and over again.  It
1158
 
   would be nice to be able to use the routines from glib -- see
1159
 
   www.gtk.org for details.  However, that would make Wget depend on
1160
 
   glib, and I want to avoid dependencies to external libraries for
1161
 
   reasons of convenience and portability (I suspect Wget is more
1162
 
   portable than anything ever written for Gnome).  */
1163
 
 
1164
 
/* Append an element to the list.  If the list has a huge number of
1165
 
   elements, this can get slow because it has to find the list's
1166
 
   ending.  If you think you have to call slist_append in a loop,
1167
 
   think about calling slist_prepend() followed by slist_nreverse().  */
1168
 
 
1169
 
slist *
1170
 
slist_append (slist *l, const char *s)
1171
 
{
1172
 
  slist *newel = (slist *)xmalloc (sizeof (slist));
1173
 
  slist *beg = l;
1174
 
 
1175
 
  newel->string = xstrdup (s);
1176
 
  newel->next = NULL;
1177
 
 
1178
 
  if (!l)
1179
 
    return newel;
1180
 
  /* Find the last element.  */
1181
 
  while (l->next)
1182
 
    l = l->next;
1183
 
  l->next = newel;
1184
 
  return beg;
1185
 
}
1186
 
 
1187
 
/* Prepend S to the list.  Unlike slist_append(), this is O(1).  */
1188
 
 
1189
 
slist *
1190
 
slist_prepend (slist *l, const char *s)
1191
 
{
1192
 
  slist *newel = (slist *)xmalloc (sizeof (slist));
1193
 
  newel->string = xstrdup (s);
1194
 
  newel->next = l;
1195
 
  return newel;
1196
 
}
1197
 
 
1198
 
/* Destructively reverse L. */
1199
 
 
1200
 
slist *
1201
 
slist_nreverse (slist *l)
1202
 
{
1203
 
  slist *prev = NULL;
1204
 
  while (l)
1205
 
    {
1206
 
      slist *next = l->next;
1207
 
      l->next = prev;
1208
 
      prev = l;
1209
 
      l = next;
1210
 
    }
1211
 
  return prev;
1212
 
}
1213
 
 
1214
 
/* Is there a specific entry in the list?  */
1215
 
int
1216
 
slist_contains (slist *l, const char *s)
1217
 
{
1218
 
  for (; l; l = l->next)
1219
 
    if (!strcmp (l->string, s))
1220
 
      return 1;
1221
 
  return 0;
1222
 
}
1223
 
 
1224
 
/* Free the whole slist.  */
1225
 
void
1226
 
slist_free (slist *l)
1227
 
{
1228
 
  while (l)
1229
 
    {
1230
 
      slist *n = l->next;
1231
 
      xfree (l->string);
1232
 
      xfree (l);
1233
 
      l = n;
1234
 
    }
 
1089
/* Append a freshly allocated copy of STR to VEC.  If VEC is NULL, it
 
1090
   is allocated as needed.  Return the new value of the vector. */
 
1091
 
 
1092
char **
 
1093
vec_append (char **vec, const char *str)
 
1094
{
 
1095
  int cnt;                      /* count of vector elements, including
 
1096
                                   the one we're about to append */
 
1097
  if (vec != NULL)
 
1098
    {
 
1099
      for (cnt = 0; vec[cnt]; cnt++)
 
1100
        ;
 
1101
      ++cnt;
 
1102
    }
 
1103
  else
 
1104
    cnt = 1;
 
1105
  /* Reallocate the array to fit the new element and the NULL. */
 
1106
  vec = xrealloc (vec, (cnt + 1) * sizeof (char *));
 
1107
  /* Append a copy of STR to the vector. */
 
1108
  vec[cnt - 1] = xstrdup (str);
 
1109
  vec[cnt] = NULL;
 
1110
  return vec;
1235
1111
}
1236
1112
 
1237
1113
/* Sometimes it's useful to create "sets" of strings, i.e. special
1264
1140
}
1265
1141
 
1266
1142
static int
 
1143
string_set_to_array_mapper (void *key, void *value_ignored, void *arg)
 
1144
{
 
1145
  char ***arrayptr = (char ***) arg;
 
1146
  *(*arrayptr)++ = (char *) key;
 
1147
  return 0;
 
1148
}
 
1149
 
 
1150
/* Convert the specified string set to array.  ARRAY should be large
 
1151
   enough to hold hash_table_count(ht) char pointers.  */
 
1152
 
 
1153
void string_set_to_array (struct hash_table *ht, char **array)
 
1154
{
 
1155
  hash_table_map (ht, string_set_to_array_mapper, &array);
 
1156
}
 
1157
 
 
1158
static int
1267
1159
string_set_free_mapper (void *key, void *value_ignored, void *arg_ignored)
1268
1160
{
1269
1161
  xfree (key);
1294
1186
}
1295
1187
 
1296
1188
 
1297
 
/* Engine for legible and legible_large_int; add thousand separators
1298
 
   to numbers printed in strings.  */
 
1189
/* Add thousand separators to a number already in string form.  Used
 
1190
   by with_thousand_seps and with_thousand_seps_large.  */
1299
1191
 
1300
1192
static char *
1301
 
legible_1 (const char *repr)
 
1193
add_thousand_seps (const char *repr)
1302
1194
{
1303
1195
  static char outbuf[48];
1304
1196
  int i, i1, mod;
1334
1226
  return outbuf;
1335
1227
}
1336
1228
 
1337
 
/* Legible -- return a static pointer to the legibly printed long.  */
 
1229
/* Return a static pointer to the number printed with thousand
 
1230
   separators inserted at the right places.  */
1338
1231
 
1339
1232
char *
1340
 
legible (long l)
 
1233
with_thousand_seps (wgint l)
1341
1234
{
1342
1235
  char inbuf[24];
1343
1236
  /* Print the number into the buffer.  */
1344
1237
  number_to_string (inbuf, l);
1345
 
  return legible_1 (inbuf);
 
1238
  return add_thousand_seps (inbuf);
1346
1239
}
1347
1240
 
1348
1241
/* Write a string representation of LARGE_INT NUMBER into the provided
1349
 
   buffer.  The buffer should be able to accept 24 characters,
1350
 
   including the terminating zero.
 
1242
   buffer.
1351
1243
 
1352
1244
   It would be dangerous to use sprintf, because the code wouldn't
1353
1245
   work on a machine with gcc-provided long long support, but without
1354
 
   libc support for "%lld".  However, such platforms will typically
1355
 
   not have snprintf and will use our version, which does support
1356
 
   "%lld" where long longs are available.  */
 
1246
   libc support for "%lld".  However, such old systems platforms
 
1247
   typically lack snprintf and will end up using our version, which
 
1248
   does support "%lld" whereever long longs are available.  */
1357
1249
 
1358
1250
static void
1359
 
large_int_to_string (char *buffer, LARGE_INT number)
 
1251
large_int_to_string (char *buffer, int bufsize, LARGE_INT number)
1360
1252
{
1361
 
  snprintf (buffer, 24, LARGE_INT_FMT, number);
 
1253
  snprintf (buffer, bufsize, LARGE_INT_FMT, number);
1362
1254
}
1363
1255
 
1364
 
/* The same as legible(), but works on LARGE_INT.  */
 
1256
/* The same as with_thousand_seps, but works on LARGE_INT.  */
1365
1257
 
1366
1258
char *
1367
 
legible_large_int (LARGE_INT l)
 
1259
with_thousand_seps_large (LARGE_INT l)
1368
1260
{
1369
1261
  char inbuf[48];
1370
 
  large_int_to_string (inbuf, l);
1371
 
  return legible_1 (inbuf);
1372
 
}
1373
 
 
1374
 
/* Count the digits in a (long) integer.  */
 
1262
  large_int_to_string (inbuf, sizeof (inbuf), l);
 
1263
  return add_thousand_seps (inbuf);
 
1264
}
 
1265
 
 
1266
/* N, a byte quantity, is converted to a human-readable abberviated
 
1267
   form a la sizes printed by `ls -lh'.  The result is written to a
 
1268
   static buffer, a pointer to which is returned.
 
1269
 
 
1270
   Unlike `with_thousand_seps', this approximates to the nearest unit.
 
1271
   Quoting GNU libit: "Most people visually process strings of 3-4
 
1272
   digits effectively, but longer strings of digits are more prone to
 
1273
   misinterpretation.  Hence, converting to an abbreviated form
 
1274
   usually improves readability."
 
1275
 
 
1276
   This intentionally uses kilobyte (KB), megabyte (MB), etc. in their
 
1277
   original computer science meaning of "powers of 1024".  Powers of
 
1278
   1000 would be useless since Wget already displays sizes with
 
1279
   thousand separators.  We don't use the "*bibyte" names invented in
 
1280
   1998, and seldom used in practice.  Wikipedia's entry on kilobyte
 
1281
   discusses this in some detail.  */
 
1282
 
 
1283
char *
 
1284
human_readable (wgint n)
 
1285
{
 
1286
  /* These suffixes are compatible with those of GNU `ls -lh'. */
 
1287
  static char powers[] =
 
1288
    {
 
1289
      'K',                      /* kilobyte, 2^10 bytes */
 
1290
      'M',                      /* megabyte, 2^20 bytes */
 
1291
      'G',                      /* gigabyte, 2^30 bytes */
 
1292
      'T',                      /* terabyte, 2^40 bytes */
 
1293
      'P',                      /* petabyte, 2^50 bytes */
 
1294
      'E',                      /* exabyte,  2^60 bytes */
 
1295
    };
 
1296
  static char buf[8];
 
1297
  int i;
 
1298
 
 
1299
  /* If the quantity is smaller than 1K, just print it. */
 
1300
  if (n < 1024)
 
1301
    {
 
1302
      snprintf (buf, sizeof (buf), "%d", (int) n);
 
1303
      return buf;
 
1304
    }
 
1305
 
 
1306
  /* Loop over powers, dividing N with 1024 in each iteration.  This
 
1307
     works unchanged for all sizes of wgint, while still avoiding
 
1308
     non-portable `long double' arithmetic.  */
 
1309
  for (i = 0; i < countof (powers); i++)
 
1310
    {
 
1311
      /* At each iteration N is greater than the *subsequent* power.
 
1312
         That way N/1024.0 produces a decimal number in the units of
 
1313
         *this* power.  */
 
1314
      if ((n >> 10) < 1024 || i == countof (powers) - 1)
 
1315
        {
 
1316
          /* Must cast to long first because MS VC can't directly cast
 
1317
             __int64 to double.  (This is safe because N is known to
 
1318
             be <2**20.)  */
 
1319
          double val = (double) (long) n / 1024.0;
 
1320
          /* Print values smaller than 10 with one decimal digits, and
 
1321
             others without any decimals.  */
 
1322
          snprintf (buf, sizeof (buf), "%.*f%c",
 
1323
                    val < 10 ? 1 : 0, val, powers[i]);
 
1324
          return buf;
 
1325
        }
 
1326
      n >>= 10;
 
1327
    }
 
1328
  return NULL;                  /* unreached */
 
1329
}
 
1330
 
 
1331
/* Count the digits in the provided number.  Used to allocate space
 
1332
   when printing numbers.  */
 
1333
 
1375
1334
int
1376
 
numdigit (long number)
 
1335
numdigit (wgint number)
1377
1336
{
1378
1337
  int cnt = 1;
1379
1338
  if (number < 0)
1380
 
    {
1381
 
      number = -number;
1382
 
      ++cnt;
1383
 
    }
1384
 
  while ((number /= 10) > 0)
 
1339
    ++cnt;                      /* accomodate '-' */
 
1340
  while ((number /= 10) != 0)
1385
1341
    ++cnt;
1386
1342
  return cnt;
1387
1343
}
1388
1344
 
1389
 
/* A half-assed implementation of INT_MAX on machines that don't
1390
 
   bother to define one. */
1391
 
#ifndef INT_MAX
1392
 
# define INT_MAX ((int) ~((unsigned)1 << 8 * sizeof (int) - 1))
 
1345
#define PR(mask) *p++ = n / (mask) + '0'
 
1346
 
 
1347
/* DIGITS_<D> is used to print a D-digit number and should be called
 
1348
   with mask==10^(D-1).  It prints n/mask (the first digit), reducing
 
1349
   n to n%mask (the remaining digits), and calling DIGITS_<D-1>.
 
1350
   Recursively this continues until DIGITS_1 is invoked.  */
 
1351
 
 
1352
#define DIGITS_1(mask) PR (mask)
 
1353
#define DIGITS_2(mask) PR (mask), n %= (mask), DIGITS_1 ((mask) / 10)
 
1354
#define DIGITS_3(mask) PR (mask), n %= (mask), DIGITS_2 ((mask) / 10)
 
1355
#define DIGITS_4(mask) PR (mask), n %= (mask), DIGITS_3 ((mask) / 10)
 
1356
#define DIGITS_5(mask) PR (mask), n %= (mask), DIGITS_4 ((mask) / 10)
 
1357
#define DIGITS_6(mask) PR (mask), n %= (mask), DIGITS_5 ((mask) / 10)
 
1358
#define DIGITS_7(mask) PR (mask), n %= (mask), DIGITS_6 ((mask) / 10)
 
1359
#define DIGITS_8(mask) PR (mask), n %= (mask), DIGITS_7 ((mask) / 10)
 
1360
#define DIGITS_9(mask) PR (mask), n %= (mask), DIGITS_8 ((mask) / 10)
 
1361
#define DIGITS_10(mask) PR (mask), n %= (mask), DIGITS_9 ((mask) / 10)
 
1362
 
 
1363
/* DIGITS_<11-20> are only used on machines with 64-bit wgints. */
 
1364
 
 
1365
#define DIGITS_11(mask) PR (mask), n %= (mask), DIGITS_10 ((mask) / 10)
 
1366
#define DIGITS_12(mask) PR (mask), n %= (mask), DIGITS_11 ((mask) / 10)
 
1367
#define DIGITS_13(mask) PR (mask), n %= (mask), DIGITS_12 ((mask) / 10)
 
1368
#define DIGITS_14(mask) PR (mask), n %= (mask), DIGITS_13 ((mask) / 10)
 
1369
#define DIGITS_15(mask) PR (mask), n %= (mask), DIGITS_14 ((mask) / 10)
 
1370
#define DIGITS_16(mask) PR (mask), n %= (mask), DIGITS_15 ((mask) / 10)
 
1371
#define DIGITS_17(mask) PR (mask), n %= (mask), DIGITS_16 ((mask) / 10)
 
1372
#define DIGITS_18(mask) PR (mask), n %= (mask), DIGITS_17 ((mask) / 10)
 
1373
#define DIGITS_19(mask) PR (mask), n %= (mask), DIGITS_18 ((mask) / 10)
 
1374
 
 
1375
/* SPRINTF_WGINT is used by number_to_string to handle pathological
 
1376
   cases and to portably support strange sizes of wgint.  Ideally this
 
1377
   would just use "%j" and intmax_t, but many systems don't support
 
1378
   it, so it's used only if nothing else works.  */
 
1379
#if SIZEOF_LONG >= SIZEOF_WGINT
 
1380
#  define SPRINTF_WGINT(buf, n) sprintf (buf, "%ld", (long) (n))
 
1381
#else
 
1382
# if SIZEOF_LONG_LONG >= SIZEOF_WGINT
 
1383
#   define SPRINTF_WGINT(buf, n) sprintf (buf, "%lld", (long long) (n))
 
1384
# else
 
1385
#  ifdef WINDOWS
 
1386
#   define SPRINTF_WGINT(buf, n) sprintf (buf, "%I64", (__int64) (n))
 
1387
#  else
 
1388
#   define SPRINTF_WGINT(buf, n) sprintf (buf, "%j", (intmax_t) (n))
 
1389
#  endif
 
1390
# endif
1393
1391
#endif
1394
1392
 
1395
 
#define ONE_DIGIT(figure) *p++ = n / (figure) + '0'
1396
 
#define ONE_DIGIT_ADVANCE(figure) (ONE_DIGIT (figure), n %= (figure))
1397
 
 
1398
 
#define DIGITS_1(figure) ONE_DIGIT (figure)
1399
 
#define DIGITS_2(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_1 ((figure) / 10)
1400
 
#define DIGITS_3(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_2 ((figure) / 10)
1401
 
#define DIGITS_4(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_3 ((figure) / 10)
1402
 
#define DIGITS_5(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_4 ((figure) / 10)
1403
 
#define DIGITS_6(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_5 ((figure) / 10)
1404
 
#define DIGITS_7(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_6 ((figure) / 10)
1405
 
#define DIGITS_8(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_7 ((figure) / 10)
1406
 
#define DIGITS_9(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_8 ((figure) / 10)
1407
 
#define DIGITS_10(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_9 ((figure) / 10)
1408
 
 
1409
 
/* DIGITS_<11-20> are only used on machines with 64-bit longs. */
1410
 
 
1411
 
#define DIGITS_11(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_10 ((figure) / 10)
1412
 
#define DIGITS_12(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_11 ((figure) / 10)
1413
 
#define DIGITS_13(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_12 ((figure) / 10)
1414
 
#define DIGITS_14(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_13 ((figure) / 10)
1415
 
#define DIGITS_15(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_14 ((figure) / 10)
1416
 
#define DIGITS_16(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_15 ((figure) / 10)
1417
 
#define DIGITS_17(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_16 ((figure) / 10)
1418
 
#define DIGITS_18(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_17 ((figure) / 10)
1419
 
#define DIGITS_19(figure) ONE_DIGIT_ADVANCE (figure); DIGITS_18 ((figure) / 10)
1420
 
 
1421
 
/* Print NUMBER to BUFFER in base 10.  This should be completely
1422
 
   equivalent to `sprintf(buffer, "%ld", number)', only much faster.
 
1393
/* Shorthand for casting to wgint. */
 
1394
#define W wgint
 
1395
 
 
1396
/* Print NUMBER to BUFFER in base 10.  This is equivalent to
 
1397
   `sprintf(buffer, "%lld", (long long) number)', only typically much
 
1398
   faster and portable to machines without long long.
1423
1399
 
1424
1400
   The speedup may make a difference in programs that frequently
1425
1401
   convert numbers to strings.  Some implementations of sprintf,
1426
1402
   particularly the one in GNU libc, have been known to be extremely
1427
 
   slow compared to this function.
 
1403
   slow when converting integers to strings.
1428
1404
 
1429
1405
   Return the pointer to the location where the terminating zero was
1430
1406
   printed.  (Equivalent to calling buffer+strlen(buffer) after the
1437
1413
   terminating '\0'.  */
1438
1414
 
1439
1415
char *
1440
 
number_to_string (char *buffer, long number)
 
1416
number_to_string (char *buffer, wgint number)
1441
1417
{
1442
1418
  char *p = buffer;
1443
 
  long n = number;
 
1419
  wgint n = number;
1444
1420
 
1445
 
#if (SIZEOF_LONG != 4) && (SIZEOF_LONG != 8)
 
1421
#if (SIZEOF_WGINT != 4) && (SIZEOF_WGINT != 8)
1446
1422
  /* We are running in a strange or misconfigured environment.  Let
1447
1423
     sprintf cope with it.  */
1448
 
  sprintf (buffer, "%ld", n);
 
1424
  SPRINTF_WGINT (buffer, n);
1449
1425
  p += strlen (buffer);
1450
 
#else  /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
 
1426
#else  /* (SIZEOF_WGINT == 4) || (SIZEOF_WGINT == 8) */
1451
1427
 
1452
1428
  if (n < 0)
1453
1429
    {
1454
 
      if (n < -INT_MAX)
 
1430
      if (n < -WGINT_MAX)
1455
1431
        {
1456
 
          /* We cannot print a '-' and assign -n to n because -n would
1457
 
             overflow.  Let sprintf deal with this border case.  */
1458
 
          sprintf (buffer, "%ld", n);
 
1432
          /* -n would overflow.  Have sprintf deal with this.  */
 
1433
          SPRINTF_WGINT (buffer, n);
1459
1434
          p += strlen (buffer);
1460
1435
          return p;
1461
1436
        }
1464
1439
      n = -n;
1465
1440
    }
1466
1441
 
1467
 
  if      (n < 10)                   { DIGITS_1 (1); }
1468
 
  else if (n < 100)                  { DIGITS_2 (10); }
1469
 
  else if (n < 1000)                 { DIGITS_3 (100); }
1470
 
  else if (n < 10000)                { DIGITS_4 (1000); }
1471
 
  else if (n < 100000)               { DIGITS_5 (10000); }
1472
 
  else if (n < 1000000)              { DIGITS_6 (100000); }
1473
 
  else if (n < 10000000)             { DIGITS_7 (1000000); }
1474
 
  else if (n < 100000000)            { DIGITS_8 (10000000); }
1475
 
  else if (n < 1000000000)           { DIGITS_9 (100000000); }
1476
 
#if SIZEOF_LONG == 4
1477
 
  /* ``if (1)'' serves only to preserve editor indentation. */
1478
 
  else if (1)                        { DIGITS_10 (1000000000); }
1479
 
#else  /* SIZEOF_LONG != 4 */
1480
 
  else if (n < 10000000000L)         { DIGITS_10 (1000000000L); }
1481
 
  else if (n < 100000000000L)        { DIGITS_11 (10000000000L); }
1482
 
  else if (n < 1000000000000L)       { DIGITS_12 (100000000000L); }
1483
 
  else if (n < 10000000000000L)      { DIGITS_13 (1000000000000L); }
1484
 
  else if (n < 100000000000000L)     { DIGITS_14 (10000000000000L); }
1485
 
  else if (n < 1000000000000000L)    { DIGITS_15 (100000000000000L); }
1486
 
  else if (n < 10000000000000000L)   { DIGITS_16 (1000000000000000L); }
1487
 
  else if (n < 100000000000000000L)  { DIGITS_17 (10000000000000000L); }
1488
 
  else if (n < 1000000000000000000L) { DIGITS_18 (100000000000000000L); }
1489
 
  else                               { DIGITS_19 (1000000000000000000L); }
1490
 
#endif /* SIZEOF_LONG != 4 */
 
1442
  /* Use the DIGITS_ macro appropriate for N's number of digits.  That
 
1443
     way printing any N is fully open-coded without a loop or jump.
 
1444
     (Also see description of DIGITS_*.)  */
 
1445
 
 
1446
  if      (n < 10)                       DIGITS_1 (1);
 
1447
  else if (n < 100)                      DIGITS_2 (10);
 
1448
  else if (n < 1000)                     DIGITS_3 (100);
 
1449
  else if (n < 10000)                    DIGITS_4 (1000);
 
1450
  else if (n < 100000)                   DIGITS_5 (10000);
 
1451
  else if (n < 1000000)                  DIGITS_6 (100000);
 
1452
  else if (n < 10000000)                 DIGITS_7 (1000000);
 
1453
  else if (n < 100000000)                DIGITS_8 (10000000);
 
1454
  else if (n < 1000000000)               DIGITS_9 (100000000);
 
1455
#if SIZEOF_WGINT == 4
 
1456
  /* wgint is 32 bits wide: no number has more than 10 digits. */
 
1457
  else                                   DIGITS_10 (1000000000);
 
1458
#else
 
1459
  /* wgint is 64 bits wide: handle numbers with more than 9 decimal
 
1460
     digits.  Constants are constructed by compile-time multiplication
 
1461
     to avoid dealing with different notations for 64-bit constants
 
1462
     (nnnL, nnnLL, and nnnI64, depending on the compiler).  */
 
1463
  else if (n < 10*(W)1000000000)         DIGITS_10 (1000000000);
 
1464
  else if (n < 100*(W)1000000000)        DIGITS_11 (10*(W)1000000000);
 
1465
  else if (n < 1000*(W)1000000000)       DIGITS_12 (100*(W)1000000000);
 
1466
  else if (n < 10000*(W)1000000000)      DIGITS_13 (1000*(W)1000000000);
 
1467
  else if (n < 100000*(W)1000000000)     DIGITS_14 (10000*(W)1000000000);
 
1468
  else if (n < 1000000*(W)1000000000)    DIGITS_15 (100000*(W)1000000000);
 
1469
  else if (n < 10000000*(W)1000000000)   DIGITS_16 (1000000*(W)1000000000);
 
1470
  else if (n < 100000000*(W)1000000000)  DIGITS_17 (10000000*(W)1000000000);
 
1471
  else if (n < 1000000000*(W)1000000000) DIGITS_18 (100000000*(W)1000000000);
 
1472
  else                                   DIGITS_19 (1000000000*(W)1000000000);
 
1473
#endif
1491
1474
 
1492
1475
  *p = '\0';
1493
 
#endif /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
 
1476
#endif /* (SIZEOF_WGINT == 4) || (SIZEOF_WGINT == 8) */
1494
1477
 
1495
1478
  return p;
1496
1479
}
1497
1480
 
1498
 
#undef ONE_DIGIT
1499
 
#undef ONE_DIGIT_ADVANCE
1500
 
 
 
1481
#undef PR
 
1482
#undef W
1501
1483
#undef DIGITS_1
1502
1484
#undef DIGITS_2
1503
1485
#undef DIGITS_3
1517
1499
#undef DIGITS_17
1518
1500
#undef DIGITS_18
1519
1501
#undef DIGITS_19
1520
 
 
1521
 
/* Support for timers. */
1522
 
 
1523
 
#undef TIMER_WINDOWS
1524
 
#undef TIMER_GETTIMEOFDAY
1525
 
#undef TIMER_TIME
1526
 
 
1527
 
/* Depending on the OS and availability of gettimeofday(), one and
1528
 
   only one of the above constants will be defined.  Virtually all
1529
 
   modern Unix systems will define TIMER_GETTIMEOFDAY; Windows will
1530
 
   use TIMER_WINDOWS.  TIMER_TIME is a catch-all method for
1531
 
   non-Windows systems without gettimeofday.
1532
 
 
1533
 
   #### Perhaps we should also support ftime(), which exists on old
1534
 
   BSD 4.2-influenced systems?  (It also existed under MS DOS Borland
1535
 
   C, if memory serves me.)  */
1536
 
 
1537
 
#ifdef WINDOWS
1538
 
# define TIMER_WINDOWS
1539
 
#else  /* not WINDOWS */
1540
 
# ifdef HAVE_GETTIMEOFDAY
1541
 
#  define TIMER_GETTIMEOFDAY
1542
 
# else
1543
 
#  define TIMER_TIME
1544
 
# endif
1545
 
#endif /* not WINDOWS */
1546
 
 
1547
 
#ifdef TIMER_GETTIMEOFDAY
1548
 
typedef struct timeval wget_sys_time;
1549
 
#endif
1550
 
 
1551
 
#ifdef TIMER_TIME
1552
 
typedef time_t wget_sys_time;
1553
 
#endif
1554
 
 
1555
 
#ifdef TIMER_WINDOWS
1556
 
typedef ULARGE_INTEGER wget_sys_time;
1557
 
#endif
1558
 
 
1559
 
struct wget_timer {
1560
 
  /* The starting point in time which, subtracted from the current
1561
 
     time, yields elapsed time. */
1562
 
  wget_sys_time start;
1563
 
 
1564
 
  /* The most recent elapsed time, calculated by wtimer_elapsed().
1565
 
     Measured in milliseconds.  */
1566
 
  double elapsed_last;
1567
 
 
1568
 
  /* Approximately, the time elapsed between the true start of the
1569
 
     measurement and the time represented by START.  */
1570
 
  double elapsed_pre_start;
1571
 
};
1572
 
 
1573
 
/* Allocate a timer.  It is not legal to do anything with a freshly
1574
 
   allocated timer, except call wtimer_reset() or wtimer_delete().  */
1575
 
 
1576
 
struct wget_timer *
1577
 
wtimer_allocate (void)
1578
 
{
1579
 
  struct wget_timer *wt =
1580
 
    (struct wget_timer *)xmalloc (sizeof (struct wget_timer));
1581
 
  return wt;
1582
 
}
1583
 
 
1584
 
/* Allocate a new timer and reset it.  Return the new timer. */
1585
 
 
1586
 
struct wget_timer *
1587
 
wtimer_new (void)
1588
 
{
1589
 
  struct wget_timer *wt = wtimer_allocate ();
1590
 
  wtimer_reset (wt);
1591
 
  return wt;
1592
 
}
1593
 
 
1594
 
/* Free the resources associated with the timer.  Its further use is
1595
 
   prohibited.  */
1596
 
 
1597
 
void
1598
 
wtimer_delete (struct wget_timer *wt)
1599
 
{
1600
 
  xfree (wt);
1601
 
}
1602
 
 
1603
 
/* Store system time to WST.  */
1604
 
 
1605
 
static void
1606
 
wtimer_sys_set (wget_sys_time *wst)
1607
 
{
1608
 
#ifdef TIMER_GETTIMEOFDAY
1609
 
  gettimeofday (wst, NULL);
1610
 
#endif
1611
 
 
1612
 
#ifdef TIMER_TIME
1613
 
  time (wst);
1614
 
#endif
1615
 
 
1616
 
#ifdef TIMER_WINDOWS
1617
 
  /* We use GetSystemTime to get the elapsed time.  MSDN warns that
1618
 
     system clock adjustments can skew the output of GetSystemTime
1619
 
     when used as a timer and gives preference to GetTickCount and
1620
 
     high-resolution timers.  But GetTickCount can overflow, and hires
1621
 
     timers are typically used for profiling, not for regular time
1622
 
     measurement.  Since we handle clock skew anyway, we just use
1623
 
     GetSystemTime.  */
1624
 
  FILETIME ft;
1625
 
  SYSTEMTIME st;
1626
 
  GetSystemTime (&st);
1627
 
 
1628
 
  /* As recommended by MSDN, we convert SYSTEMTIME to FILETIME, copy
1629
 
     FILETIME to ULARGE_INTEGER, and use regular 64-bit integer
1630
 
     arithmetic on that.  */
1631
 
  SystemTimeToFileTime (&st, &ft);
1632
 
  wst->HighPart = ft.dwHighDateTime;
1633
 
  wst->LowPart  = ft.dwLowDateTime;
1634
 
#endif
1635
 
}
1636
 
 
1637
 
/* Reset timer WT.  This establishes the starting point from which
1638
 
   wtimer_elapsed() will return the number of elapsed
1639
 
   milliseconds.  It is allowed to reset a previously used timer.  */
1640
 
 
1641
 
void
1642
 
wtimer_reset (struct wget_timer *wt)
1643
 
{
1644
 
  /* Set the start time to the current time. */
1645
 
  wtimer_sys_set (&wt->start);
1646
 
  wt->elapsed_last = 0;
1647
 
  wt->elapsed_pre_start = 0;
1648
 
}
1649
 
 
1650
 
static double
1651
 
wtimer_sys_diff (wget_sys_time *wst1, wget_sys_time *wst2)
1652
 
{
1653
 
#ifdef TIMER_GETTIMEOFDAY
1654
 
  return ((double)(wst1->tv_sec - wst2->tv_sec) * 1000
1655
 
          + (double)(wst1->tv_usec - wst2->tv_usec) / 1000);
1656
 
#endif
1657
 
 
1658
 
#ifdef TIMER_TIME
1659
 
  return 1000 * (*wst1 - *wst2);
1660
 
#endif
1661
 
 
1662
 
#ifdef WINDOWS
1663
 
  /* VC++ 6 doesn't support direct cast of uint64 to double.  To work
1664
 
     around this, we subtract, then convert to signed, then finally to
1665
 
     double.  */
1666
 
  return (double)(signed __int64)(wst1->QuadPart - wst2->QuadPart) / 10000;
1667
 
#endif
1668
 
}
1669
 
 
1670
 
/* Return the number of milliseconds elapsed since the timer was last
1671
 
   reset.  It is allowed to call this function more than once to get
1672
 
   increasingly higher elapsed values.  These timers handle clock
1673
 
   skew.  */
1674
 
 
1675
 
double
1676
 
wtimer_elapsed (struct wget_timer *wt)
1677
 
{
1678
 
  wget_sys_time now;
1679
 
  double elapsed;
1680
 
 
1681
 
  wtimer_sys_set (&now);
1682
 
  elapsed = wt->elapsed_pre_start + wtimer_sys_diff (&now, &wt->start);
1683
 
 
1684
 
  /* Ideally we'd just return the difference between NOW and
1685
 
     wt->start.  However, the system timer can be set back, and we
1686
 
     could return a value smaller than when we were last called, even
1687
 
     a negative value.  Both of these would confuse the callers, which
1688
 
     expect us to return monotonically nondecreasing values.
1689
 
 
1690
 
     Therefore: if ELAPSED is smaller than its previous known value,
1691
 
     we reset wt->start to the current time and effectively start
1692
 
     measuring from this point.  But since we don't want the elapsed
1693
 
     value to start from zero, we set elapsed_pre_start to the last
1694
 
     elapsed time and increment all future calculations by that
1695
 
     amount.  */
1696
 
 
1697
 
  if (elapsed < wt->elapsed_last)
1698
 
    {
1699
 
      wt->start = now;
1700
 
      wt->elapsed_pre_start = wt->elapsed_last;
1701
 
      elapsed = wt->elapsed_last;
1702
 
    }
1703
 
 
1704
 
  wt->elapsed_last = elapsed;
1705
 
  return elapsed;
1706
 
}
1707
 
 
1708
 
/* Return the assessed granularity of the timer implementation, in
1709
 
   milliseconds.  This is used by code that tries to substitute a
1710
 
   better value for timers that have returned zero.  */
1711
 
 
1712
 
double
1713
 
wtimer_granularity (void)
1714
 
{
1715
 
#ifdef TIMER_GETTIMEOFDAY
1716
 
  /* Granularity of gettimeofday varies wildly between architectures.
1717
 
     However, it appears that on modern machines it tends to be better
1718
 
     than 1ms.  Assume 100 usecs.  (Perhaps the configure process
1719
 
     could actually measure this?)  */
1720
 
  return 0.1;
1721
 
#endif
1722
 
 
1723
 
#ifdef TIMER_TIME
1724
 
  return 1000;
1725
 
#endif
1726
 
 
1727
 
#ifdef TIMER_WINDOWS
1728
 
  /* According to MSDN, GetSystemTime returns a broken-down time
1729
 
     structure the smallest member of which are milliseconds.  */
1730
 
  return 1;
1731
 
#endif
1732
 
}
1733
 
 
1734
 
/* This should probably be at a better place, but it doesn't really
1735
 
   fit into html-parse.c.  */
1736
 
 
1737
 
/* The function returns the pointer to the malloc-ed quoted version of
1738
 
   string s.  It will recognize and quote numeric and special graphic
1739
 
   entities, as per RFC1866:
1740
 
 
1741
 
   `&' -> `&amp;'
1742
 
   `<' -> `&lt;'
1743
 
   `>' -> `&gt;'
1744
 
   `"' -> `&quot;'
1745
 
   SP  -> `&#32;'
1746
 
 
1747
 
   No other entities are recognized or replaced.  */
 
1502
 
 
1503
#define RING_SIZE 3
 
1504
 
 
1505
/* Print NUMBER to a statically allocated string and return a pointer
 
1506
   to the printed representation.
 
1507
 
 
1508
   This function is intended to be used in conjunction with printf.
 
1509
   It is hard to portably print wgint values:
 
1510
    a) you cannot use printf("%ld", number) because wgint can be long
 
1511
       long on 32-bit machines with LFS.
 
1512
    b) you cannot use printf("%lld", number) because NUMBER could be
 
1513
       long on 32-bit machines without LFS, or on 64-bit machines,
 
1514
       which do not require LFS.  Also, Windows doesn't support %lld.
 
1515
    c) you cannot use printf("%j", (int_max_t) number) because not all
 
1516
       versions of printf support "%j", the most notable being the one
 
1517
       on Windows.
 
1518
    d) you cannot #define WGINT_FMT to the appropriate format and use
 
1519
       printf(WGINT_FMT, number) because that would break translations
 
1520
       for user-visible messages, such as printf("Downloaded: %d
 
1521
       bytes\n", number).
 
1522
 
 
1523
   What you should use instead is printf("%s", number_to_static_string
 
1524
   (number)).
 
1525
 
 
1526
   CAVEAT: since the function returns pointers to static data, you
 
1527
   must be careful to copy its result before calling it again.
 
1528
   However, to make it more useful with printf, the function maintains
 
1529
   an internal ring of static buffers to return.  That way things like
 
1530
   printf("%s %s", number_to_static_string (num1),
 
1531
   number_to_static_string (num2)) work as expected.  Three buffers
 
1532
   are currently used, which means that "%s %s %s" will work, but "%s
 
1533
   %s %s %s" won't.  If you need to print more than three wgints,
 
1534
   bump the RING_SIZE (or rethink your message.)  */
 
1535
 
1748
1536
char *
1749
 
html_quote_string (const char *s)
 
1537
number_to_static_string (wgint number)
1750
1538
{
1751
 
  const char *b = s;
1752
 
  char *p, *res;
1753
 
  int i;
1754
 
 
1755
 
  /* Pass through the string, and count the new size.  */
1756
 
  for (i = 0; *s; s++, i++)
1757
 
    {
1758
 
      if (*s == '&')
1759
 
        i += 4;                 /* `amp;' */
1760
 
      else if (*s == '<' || *s == '>')
1761
 
        i += 3;                 /* `lt;' and `gt;' */
1762
 
      else if (*s == '\"')
1763
 
        i += 5;                 /* `quot;' */
1764
 
      else if (*s == ' ')
1765
 
        i += 4;                 /* #32; */
1766
 
    }
1767
 
  res = (char *)xmalloc (i + 1);
1768
 
  s = b;
1769
 
  for (p = res; *s; s++)
1770
 
    {
1771
 
      switch (*s)
1772
 
        {
1773
 
        case '&':
1774
 
          *p++ = '&';
1775
 
          *p++ = 'a';
1776
 
          *p++ = 'm';
1777
 
          *p++ = 'p';
1778
 
          *p++ = ';';
1779
 
          break;
1780
 
        case '<': case '>':
1781
 
          *p++ = '&';
1782
 
          *p++ = (*s == '<' ? 'l' : 'g');
1783
 
          *p++ = 't';
1784
 
          *p++ = ';';
1785
 
          break;
1786
 
        case '\"':
1787
 
          *p++ = '&';
1788
 
          *p++ = 'q';
1789
 
          *p++ = 'u';
1790
 
          *p++ = 'o';
1791
 
          *p++ = 't';
1792
 
          *p++ = ';';
1793
 
          break;
1794
 
        case ' ':
1795
 
          *p++ = '&';
1796
 
          *p++ = '#';
1797
 
          *p++ = '3';
1798
 
          *p++ = '2';
1799
 
          *p++ = ';';
1800
 
          break;
1801
 
        default:
1802
 
          *p++ = *s;
1803
 
        }
1804
 
    }
1805
 
  *p = '\0';
1806
 
  return res;
 
1539
  static char ring[RING_SIZE][24];
 
1540
  static int ringpos;
 
1541
  char *buf = ring[ringpos];
 
1542
  number_to_string (buf, number);
 
1543
  ringpos = (ringpos + 1) % RING_SIZE;
 
1544
  return buf;
1807
1545
}
1808
 
 
 
1546
 
1809
1547
/* Determine the width of the terminal we're running on.  If that's
1810
1548
   not possible, return 0.  */
1811
1549
 
1814
1552
{
1815
1553
  /* If there's a way to get the terminal size using POSIX
1816
1554
     tcgetattr(), somebody please tell me.  */
1817
 
#ifndef TIOCGWINSZ
1818
 
  return 0;
1819
 
#else  /* TIOCGWINSZ */
 
1555
#ifdef TIOCGWINSZ
1820
1556
  int fd;
1821
1557
  struct winsize wsz;
1822
1558
 
1828
1564
    return 0;                   /* most likely ENOTTY */
1829
1565
 
1830
1566
  return wsz.ws_col;
1831
 
#endif /* TIOCGWINSZ */
 
1567
#else  /* not TIOCGWINSZ */
 
1568
# ifdef WINDOWS
 
1569
  CONSOLE_SCREEN_BUFFER_INFO csbi;
 
1570
  if (!GetConsoleScreenBufferInfo (GetStdHandle (STD_ERROR_HANDLE), &csbi))
 
1571
    return 0;
 
1572
  return csbi.dwSize.X;
 
1573
# else /* neither WINDOWS nor TIOCGWINSZ */
 
1574
  return 0;
 
1575
#endif /* neither WINDOWS nor TIOCGWINSZ */
 
1576
#endif /* not TIOCGWINSZ */
1832
1577
}
1833
1578
 
1834
1579
/* Return a random number between 0 and MAX-1, inclusive.
1893
1638
  int rnd3 = random_number (1000);
1894
1639
  return rnd1 / 1000.0 + rnd2 / 1000000.0 + rnd3 / 1000000000.0;
1895
1640
}
1896
 
 
1897
 
#if 0
1898
 
/* A debugging function for checking whether an MD5 library works. */
1899
 
 
1900
 
#include "gen-md5.h"
1901
 
 
1902
 
char *
1903
 
debug_test_md5 (char *buf)
1904
 
{
1905
 
  unsigned char raw[16];
1906
 
  static char res[33];
1907
 
  unsigned char *p1;
1908
 
  char *p2;
1909
 
  int cnt;
1910
 
  ALLOCA_MD5_CONTEXT (ctx);
1911
 
 
1912
 
  gen_md5_init (ctx);
1913
 
  gen_md5_update ((unsigned char *)buf, strlen (buf), ctx);
1914
 
  gen_md5_finish (ctx, raw);
1915
 
 
1916
 
  p1 = raw;
1917
 
  p2 = res;
1918
 
  cnt = 16;
1919
 
  while (cnt--)
1920
 
    {
1921
 
      *p2++ = XNUM_TO_digit (*p1 >> 4);
1922
 
      *p2++ = XNUM_TO_digit (*p1 & 0xf);
1923
 
      ++p1;
1924
 
    }
1925
 
  *p2 = '\0';
1926
 
 
1927
 
  return res;
1928
 
}
1929
 
#endif
1930
1641
 
1931
1642
/* Implementation of run_with_timeout, a generic timeout-forcing
1932
1643
   routine for systems with Unix-like signal handling.  */
1978
1689
#ifdef ITIMER_REAL
1979
1690
  /* Use the modern itimer interface. */
1980
1691
  struct itimerval itv;
1981
 
  memset (&itv, 0, sizeof (itv));
 
1692
  xzero (itv);
1982
1693
  itv.it_value.tv_sec = (long) timeout;
1983
 
  itv.it_value.tv_usec = 1000000L * (timeout - (long)timeout);
 
1694
  itv.it_value.tv_usec = 1000000 * (timeout - (long)timeout);
1984
1695
  if (itv.it_value.tv_sec == 0 && itv.it_value.tv_usec == 0)
1985
1696
    /* Ensure that we wait for at least the minimum interval.
1986
1697
       Specifying zero would mean "wait forever".  */
2006
1717
{
2007
1718
#ifdef ITIMER_REAL
2008
1719
  struct itimerval disable;
2009
 
  memset (&disable, 0, sizeof (disable));
 
1720
  xzero (disable);
2010
1721
  setitimer (ITIMER_REAL, &disable, NULL);
2011
1722
#else  /* not ITIMER_REAL */
2012
1723
  alarm (0);
2032
1743
     * It works with both SYSV and BSD signals because it doesn't
2033
1744
       depend on the default setting of SA_RESTART.
2034
1745
 
2035
 
     * It doesn't special handler setup beyond a simple call to
2036
 
       signal().  (It does use sigsetjmp/siglongjmp, but they're
 
1746
     * It doesn't require special handler setup beyond a simple call
 
1747
       to signal().  (It does use sigsetjmp/siglongjmp, but they're
2037
1748
       optional.)
2038
1749
 
2039
1750
   The only downside is that, if FUN allocates internal resources that
2085
1796
}
2086
1797
#endif /* not WINDOWS */
2087
1798
#endif /* not USE_SIGNAL_TIMEOUT */
 
1799
 
 
1800
#ifndef WINDOWS
 
1801
 
 
1802
/* Sleep the specified amount of seconds.  On machines without
 
1803
   nanosleep(), this may sleep shorter if interrupted by signals.  */
 
1804
 
 
1805
void
 
1806
xsleep (double seconds)
 
1807
{
 
1808
#ifdef HAVE_NANOSLEEP
 
1809
  /* nanosleep is the preferred interface because it offers high
 
1810
     accuracy and, more importantly, because it allows us to reliably
 
1811
     restart receiving a signal such as SIGWINCH.  (There was an
 
1812
     actual Debian bug report about --limit-rate malfunctioning while
 
1813
     the terminal was being resized.)  */
 
1814
  struct timespec sleep, remaining;
 
1815
  sleep.tv_sec = (long) seconds;
 
1816
  sleep.tv_nsec = 1000000000 * (seconds - (long) seconds);
 
1817
  while (nanosleep (&sleep, &remaining) < 0 && errno == EINTR)
 
1818
    /* If nanosleep has been interrupted by a signal, adjust the
 
1819
       sleeping period and return to sleep.  */
 
1820
    sleep = remaining;
 
1821
#else  /* not HAVE_NANOSLEEP */
 
1822
#ifdef HAVE_USLEEP
 
1823
  /* If usleep is available, use it in preference to select.  */
 
1824
  if (seconds >= 1)
 
1825
    {
 
1826
      /* On some systems, usleep cannot handle values larger than
 
1827
         1,000,000.  If the period is larger than that, use sleep
 
1828
         first, then add usleep for subsecond accuracy.  */
 
1829
      sleep (seconds);
 
1830
      seconds -= (long) seconds;
 
1831
    }
 
1832
  usleep (seconds * 1000000);
 
1833
#else  /* not HAVE_USLEEP */
 
1834
#ifdef HAVE_SELECT
 
1835
  /* Note that, although Windows supports select, this sleeping
 
1836
     strategy doesn't work there because Winsock's select doesn't
 
1837
     implement timeout when it is passed NULL pointers for all fd
 
1838
     sets.  (But it does work under Cygwin, which implements its own
 
1839
     select.)  */
 
1840
  struct timeval sleep;
 
1841
  sleep.tv_sec = (long) seconds;
 
1842
  sleep.tv_usec = 1000000 * (seconds - (long) seconds);
 
1843
  select (0, NULL, NULL, NULL, &sleep);
 
1844
  /* If select returns -1 and errno is EINTR, it means we were
 
1845
     interrupted by a signal.  But without knowing how long we've
 
1846
     actually slept, we can't return to sleep.  Using gettimeofday to
 
1847
     track sleeps is slow and unreliable due to clock skew.  */
 
1848
#else  /* not HAVE_SELECT */
 
1849
  sleep (seconds);
 
1850
#endif /* not HAVE_SELECT */
 
1851
#endif /* not HAVE_USLEEP */
 
1852
#endif /* not HAVE_NANOSLEEP */
 
1853
}
 
1854
 
 
1855
#endif /* not WINDOWS */
 
1856
 
 
1857
/* Encode the string STR of length LENGTH to base64 format and place it
 
1858
   to B64STORE.  The output will be \0-terminated, and must point to a
 
1859
   writable buffer of at least 1+BASE64_LENGTH(length) bytes.  It
 
1860
   returns the length of the resulting base64 data, not counting the
 
1861
   terminating zero.
 
1862
 
 
1863
   This implementation will not emit newlines after 76 characters of
 
1864
   base64 data.  */
 
1865
 
 
1866
int
 
1867
base64_encode (const char *str, int length, char *b64store)
 
1868
{
 
1869
  /* Conversion table.  */
 
1870
  static char tbl[64] = {
 
1871
    'A','B','C','D','E','F','G','H',
 
1872
    'I','J','K','L','M','N','O','P',
 
1873
    'Q','R','S','T','U','V','W','X',
 
1874
    'Y','Z','a','b','c','d','e','f',
 
1875
    'g','h','i','j','k','l','m','n',
 
1876
    'o','p','q','r','s','t','u','v',
 
1877
    'w','x','y','z','0','1','2','3',
 
1878
    '4','5','6','7','8','9','+','/'
 
1879
  };
 
1880
  int i;
 
1881
  const unsigned char *s = (const unsigned char *) str;
 
1882
  char *p = b64store;
 
1883
 
 
1884
  /* Transform the 3x8 bits to 4x6 bits, as required by base64.  */
 
1885
  for (i = 0; i < length; i += 3)
 
1886
    {
 
1887
      *p++ = tbl[s[0] >> 2];
 
1888
      *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
 
1889
      *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
 
1890
      *p++ = tbl[s[2] & 0x3f];
 
1891
      s += 3;
 
1892
    }
 
1893
 
 
1894
  /* Pad the result if necessary...  */
 
1895
  if (i == length + 1)
 
1896
    *(p - 1) = '=';
 
1897
  else if (i == length + 2)
 
1898
    *(p - 1) = *(p - 2) = '=';
 
1899
 
 
1900
  /* ...and zero-terminate it.  */
 
1901
  *p = '\0';
 
1902
 
 
1903
  return p - b64store;
 
1904
}
 
1905
 
 
1906
#define IS_ASCII(c) (((c) & 0x80) == 0)
 
1907
#define IS_BASE64(c) ((IS_ASCII (c) && base64_char_to_value[c] >= 0) || c == '=')
 
1908
 
 
1909
/* Get next character from the string, except that non-base64
 
1910
   characters are ignored, as mandated by rfc2045.  */
 
1911
#define NEXT_BASE64_CHAR(c, p) do {                     \
 
1912
  c = *p++;                                             \
 
1913
} while (c != '\0' && !IS_BASE64 (c))
 
1914
 
 
1915
/* Decode data from BASE64 (assumed to be encoded as base64) into
 
1916
   memory pointed to by TO.  TO should be large enough to accomodate
 
1917
   the decoded data, which is guaranteed to be less than
 
1918
   strlen(base64).
 
1919
 
 
1920
   Since TO is assumed to contain binary data, it is not
 
1921
   NUL-terminated.  The function returns the length of the data
 
1922
   written to TO.  -1 is returned in case of error caused by malformed
 
1923
   base64 input.  */
 
1924
 
 
1925
int
 
1926
base64_decode (const char *base64, char *to)
 
1927
{
 
1928
  /* Table of base64 values for first 128 characters.  Note that this
 
1929
     assumes ASCII (but so does Wget in other places).  */
 
1930
  static short base64_char_to_value[128] =
 
1931
    {
 
1932
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  /*   0-  9 */
 
1933
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  /*  10- 19 */
 
1934
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  /*  20- 29 */
 
1935
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  /*  30- 39 */
 
1936
      -1,  -1,  -1,  62,  -1,  -1,  -1,  63,  52,  53,  /*  40- 49 */
 
1937
      54,  55,  56,  57,  58,  59,  60,  61,  -1,  -1,  /*  50- 59 */
 
1938
      -1,  -1,  -1,  -1,  -1,  0,   1,   2,   3,   4,   /*  60- 69 */
 
1939
      5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  /*  70- 79 */
 
1940
      15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  /*  80- 89 */
 
1941
      25,  -1,  -1,  -1,  -1,  -1,  -1,  26,  27,  28,  /*  90- 99 */
 
1942
      29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  /* 100-109 */
 
1943
      39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  /* 110-119 */
 
1944
      49,  50,  51,  -1,  -1,  -1,  -1,  -1             /* 120-127 */
 
1945
    };
 
1946
 
 
1947
  const char *p = base64;
 
1948
  char *q = to;
 
1949
 
 
1950
  while (1)
 
1951
    {
 
1952
      unsigned char c;
 
1953
      unsigned long value;
 
1954
 
 
1955
      /* Process first byte of a quadruplet.  */
 
1956
      NEXT_BASE64_CHAR (c, p);
 
1957
      if (!c)
 
1958
        break;
 
1959
      if (c == '=')
 
1960
        return -1;              /* illegal '=' while decoding base64 */
 
1961
      value = base64_char_to_value[c] << 18;
 
1962
 
 
1963
      /* Process scond byte of a quadruplet.  */
 
1964
      NEXT_BASE64_CHAR (c, p);
 
1965
      if (!c)
 
1966
        return -1;              /* premature EOF while decoding base64 */
 
1967
      if (c == '=')
 
1968
        return -1;              /* illegal `=' while decoding base64 */
 
1969
      value |= base64_char_to_value[c] << 12;
 
1970
      *q++ = value >> 16;
 
1971
 
 
1972
      /* Process third byte of a quadruplet.  */
 
1973
      NEXT_BASE64_CHAR (c, p);
 
1974
      if (!c)
 
1975
        return -1;              /* premature EOF while decoding base64 */
 
1976
 
 
1977
      if (c == '=')
 
1978
        {
 
1979
          NEXT_BASE64_CHAR (c, p);
 
1980
          if (!c)
 
1981
            return -1;          /* premature EOF while decoding base64 */
 
1982
          if (c != '=')
 
1983
            return -1;          /* padding `=' expected but not found */
 
1984
          continue;
 
1985
        }
 
1986
 
 
1987
      value |= base64_char_to_value[c] << 6;
 
1988
      *q++ = 0xff & value >> 8;
 
1989
 
 
1990
      /* Process fourth byte of a quadruplet.  */
 
1991
      NEXT_BASE64_CHAR (c, p);
 
1992
      if (!c)
 
1993
        return -1;              /* premature EOF while decoding base64 */
 
1994
      if (c == '=')
 
1995
        continue;
 
1996
 
 
1997
      value |= base64_char_to_value[c];
 
1998
      *q++ = 0xff & value;
 
1999
    }
 
2000
 
 
2001
  return q - to;
 
2002
}
 
2003
 
 
2004
#undef IS_ASCII
 
2005
#undef IS_BASE64
 
2006
#undef NEXT_BASE64_CHAR
 
2007
 
 
2008
/* Simple merge sort for use by stable_sort.  Implementation courtesy
 
2009
   Zeljko Vrba with additional debugging by Nenad Barbutov.  */
 
2010
 
 
2011
static void
 
2012
mergesort_internal (void *base, void *temp, size_t size, size_t from, size_t to,
 
2013
                    int (*cmpfun) PARAMS ((const void *, const void *)))
 
2014
{
 
2015
#define ELT(array, pos) ((char *)(array) + (pos) * size)
 
2016
  if (from < to)
 
2017
    {
 
2018
      size_t i, j, k;
 
2019
      size_t mid = (to + from) / 2;
 
2020
      mergesort_internal (base, temp, size, from, mid, cmpfun);
 
2021
      mergesort_internal (base, temp, size, mid + 1, to, cmpfun);
 
2022
      i = from;
 
2023
      j = mid + 1;
 
2024
      for (k = from; (i <= mid) && (j <= to); k++)
 
2025
        if (cmpfun (ELT (base, i), ELT (base, j)) <= 0)
 
2026
          memcpy (ELT (temp, k), ELT (base, i++), size);
 
2027
        else
 
2028
          memcpy (ELT (temp, k), ELT (base, j++), size);
 
2029
      while (i <= mid)
 
2030
        memcpy (ELT (temp, k++), ELT (base, i++), size);
 
2031
      while (j <= to)
 
2032
        memcpy (ELT (temp, k++), ELT (base, j++), size);
 
2033
      for (k = from; k <= to; k++)
 
2034
        memcpy (ELT (base, k), ELT (temp, k), size);
 
2035
    }
 
2036
#undef ELT
 
2037
}
 
2038
 
 
2039
/* Stable sort with interface exactly like standard library's qsort.
 
2040
   Uses mergesort internally, allocating temporary storage with
 
2041
   alloca.  */
 
2042
 
 
2043
void
 
2044
stable_sort (void *base, size_t nmemb, size_t size,
 
2045
             int (*cmpfun) PARAMS ((const void *, const void *)))
 
2046
{
 
2047
  if (size > 1)
 
2048
    {
 
2049
      void *temp = alloca (nmemb * size * sizeof (void *));
 
2050
      mergesort_internal (base, temp, size, 0, nmemb - 1, cmpfun);
 
2051
    }
 
2052
}