102
108
extern int errno;
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
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. */
116
/* Croak the fatal memory error and bail out with non-zero exit
119
memfatal (const char *what)
121
/* Make sure we don't try to store part of the log line, and thus
123
log_set_save_context (0);
124
logprintf (LOG_ALWAYS, _("%s: %s: Not enough memory.\n"), exec_name, what);
128
/* These functions end with _real because they need to be
129
distinguished from the debugging functions, and from the macros.
132
If memory debugging is not turned on, wget.h defines these:
134
#define xmalloc xmalloc_real
135
#define xrealloc xrealloc_real
136
#define xstrdup xstrdup_real
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
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__)
150
Each of the *_debug function does its magic and calls the real one. */
153
# define STATIC_IF_DEBUG static
155
# define STATIC_IF_DEBUG
158
STATIC_IF_DEBUG void *
159
xmalloc_real (size_t size)
161
void *ptr = malloc (size);
167
STATIC_IF_DEBUG void *
168
xrealloc_real (void *ptr, size_t newsize)
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
176
newptr = realloc (ptr, newsize);
178
newptr = malloc (newsize);
180
memfatal ("realloc");
184
STATIC_IF_DEBUG char *
185
xstrdup_real (const char *s)
191
copy = malloc (l + 1);
194
memcpy (copy, s, l + 1);
195
#else /* HAVE_STRDUP */
199
#endif /* HAVE_STRDUP */
206
/* Crude home-grown routines for debugging some malloc-related
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.
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
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. */
224
static int malloc_count, free_count;
230
} malloc_debug[100000];
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. */
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.) */
244
register_ptr (void *ptr, const char *file, int line)
247
for (i = 0; i < countof (malloc_debug); i++)
248
if (malloc_debug[i].ptr == NULL)
250
malloc_debug[i].ptr = ptr;
251
malloc_debug[i].file = file;
252
malloc_debug[i].line = line;
258
/* Unregister PTR from malloc_debug. Abort if PTR is not present in
259
malloc_debug. (This catches calling free() with a bogus pointer.) */
262
unregister_ptr (void *ptr)
265
for (i = 0; i < countof (malloc_debug); i++)
266
if (malloc_debug[i].ptr == ptr)
268
malloc_debug[i].ptr = NULL;
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. */
280
print_malloc_debug_stats (void)
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);
292
xmalloc_debug (size_t size, const char *source_file, int source_line)
294
void *ptr = xmalloc_real (size);
296
register_ptr (ptr, source_file, source_line);
301
xfree_debug (void *ptr, const char *source_file, int source_line)
303
assert (ptr != NULL);
305
unregister_ptr (ptr);
310
xrealloc_debug (void *ptr, size_t newsize, const char *source_file, int source_line)
312
void *newptr = xrealloc_real (ptr, newsize);
316
register_ptr (newptr, source_file, source_line);
318
else if (newptr != ptr)
320
unregister_ptr (ptr);
321
register_ptr (newptr, source_file, source_line);
327
xstrdup_debug (const char *s, const char *source_file, int source_line)
329
char *copy = xstrdup_real (s);
331
register_ptr (copy, source_file, source_line);
335
#endif /* DEBUG_MALLOC */
337
111
/* Utility function: like xstrdup(), but also lowercases S. */
171
#ifdef WGET_USE_STDARG
172
# define VA_START(args, arg1) va_start (args, arg1)
174
# define VA_START(args, ignored) va_start (args)
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. */
182
aprintf (const char *fmt, ...)
184
/* This function is implemented using vsnprintf, which we provide
185
for the systems that don't have it. Therefore, it should be 100%
189
char *str = xmalloc (size);
196
/* See log_vprintf_internal for explanation why it's OK to rely
197
on the return value of vsnprintf. */
199
VA_START (args, fmt);
200
n = vsnprintf (str, size, fmt, args);
203
/* If the printing worked, return the string. */
204
if (n > -1 && n < size)
207
/* Else try again with a larger buffer. */
208
if (n > -1) /* C99 */
209
size = n + 1; /* precisely what is needed */
211
size <<= 1; /* twice the old size */
212
str = xrealloc (str, size);
216
/* Concatenate the NULL-terminated list of string arguments into
217
freshly allocated space. */
220
concat_strings (const char *str0, ...)
223
int saved_lengths[5]; /* inspired by Apache's apr_pstrcat */
226
const char *next_str;
227
int total_length = 0;
230
/* Calculate the length of and allocate the resulting string. */
233
VA_START (args, str0);
234
for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
236
int len = strlen (next_str);
237
if (argcount < countof (saved_lengths))
238
saved_lengths[argcount++] = len;
242
p = ret = xmalloc (total_length + 1);
244
/* Copy the strings into the allocated space. */
247
VA_START (args, str0);
248
for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
251
if (argcount < countof (saved_lengths))
252
len = saved_lengths[argcount++];
254
len = strlen (next_str);
255
memcpy (p, next_str, len);
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.
413
If TM is non-NULL, the current time-in-seconds will be stored
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. */
422
270
time_str (time_t *tm)
424
272
static char output[15];
426
time_t secs = time (tm);
274
time_t secs = tm ? *tm : time (NULL);
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
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). */
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(). */
1170
slist_append (slist *l, const char *s)
1172
slist *newel = (slist *)xmalloc (sizeof (slist));
1175
newel->string = xstrdup (s);
1180
/* Find the last element. */
1187
/* Prepend S to the list. Unlike slist_append(), this is O(1). */
1190
slist_prepend (slist *l, const char *s)
1192
slist *newel = (slist *)xmalloc (sizeof (slist));
1193
newel->string = xstrdup (s);
1198
/* Destructively reverse L. */
1201
slist_nreverse (slist *l)
1206
slist *next = l->next;
1214
/* Is there a specific entry in the list? */
1216
slist_contains (slist *l, const char *s)
1218
for (; l; l = l->next)
1219
if (!strcmp (l->string, s))
1224
/* Free the whole slist. */
1226
slist_free (slist *l)
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. */
1093
vec_append (char **vec, const char *str)
1095
int cnt; /* count of vector elements, including
1096
the one we're about to append */
1099
for (cnt = 0; vec[cnt]; cnt++)
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);
1237
1113
/* Sometimes it's useful to create "sets" of strings, i.e. special
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. */
1233
with_thousand_seps (wgint l)
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);
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.
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. */
1359
large_int_to_string (char *buffer, LARGE_INT number)
1251
large_int_to_string (char *buffer, int bufsize, LARGE_INT number)
1361
snprintf (buffer, 24, LARGE_INT_FMT, number);
1253
snprintf (buffer, bufsize, LARGE_INT_FMT, number);
1364
/* The same as legible(), but works on LARGE_INT. */
1256
/* The same as with_thousand_seps, but works on LARGE_INT. */
1367
legible_large_int (LARGE_INT l)
1259
with_thousand_seps_large (LARGE_INT l)
1369
1261
char inbuf[48];
1370
large_int_to_string (inbuf, l);
1371
return legible_1 (inbuf);
1374
/* Count the digits in a (long) integer. */
1262
large_int_to_string (inbuf, sizeof (inbuf), l);
1263
return add_thousand_seps (inbuf);
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.
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."
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. */
1284
human_readable (wgint n)
1286
/* These suffixes are compatible with those of GNU `ls -lh'. */
1287
static char powers[] =
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 */
1299
/* If the quantity is smaller than 1K, just print it. */
1302
snprintf (buf, sizeof (buf), "%d", (int) n);
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++)
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
1314
if ((n >> 10) < 1024 || i == countof (powers) - 1)
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
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]);
1328
return NULL; /* unreached */
1331
/* Count the digits in the provided number. Used to allocate space
1332
when printing numbers. */
1376
numdigit (long number)
1335
numdigit (wgint number)
1379
1338
if (number < 0)
1384
while ((number /= 10) > 0)
1339
++cnt; /* accomodate '-' */
1340
while ((number /= 10) != 0)
1389
/* A half-assed implementation of INT_MAX on machines that don't
1390
bother to define one. */
1392
# define INT_MAX ((int) ~((unsigned)1 << 8 * sizeof (int) - 1))
1345
#define PR(mask) *p++ = n / (mask) + '0'
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. */
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)
1363
/* DIGITS_<11-20> are only used on machines with 64-bit wgints. */
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)
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))
1382
# if SIZEOF_LONG_LONG >= SIZEOF_WGINT
1383
# define SPRINTF_WGINT(buf, n) sprintf (buf, "%lld", (long long) (n))
1386
# define SPRINTF_WGINT(buf, n) sprintf (buf, "%I64", (__int64) (n))
1388
# define SPRINTF_WGINT(buf, n) sprintf (buf, "%j", (intmax_t) (n))
1395
#define ONE_DIGIT(figure) *p++ = n / (figure) + '0'
1396
#define ONE_DIGIT_ADVANCE(figure) (ONE_DIGIT (figure), n %= (figure))
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)
1409
/* DIGITS_<11-20> are only used on machines with 64-bit longs. */
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)
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. */
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.
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.
1429
1405
Return the pointer to the location where the terminating zero was
1430
1406
printed. (Equivalent to calling buffer+strlen(buffer) after the
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_*.) */
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);
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);
1493
#endif /* (SIZEOF_LONG == 4) || (SIZEOF_LONG == 8) */
1476
#endif /* (SIZEOF_WGINT == 4) || (SIZEOF_WGINT == 8) */
1499
#undef ONE_DIGIT_ADVANCE
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
1521
/* Support for timers. */
1523
#undef TIMER_WINDOWS
1524
#undef TIMER_GETTIMEOFDAY
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.
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.) */
1538
# define TIMER_WINDOWS
1539
#else /* not WINDOWS */
1540
# ifdef HAVE_GETTIMEOFDAY
1541
# define TIMER_GETTIMEOFDAY
1545
#endif /* not WINDOWS */
1547
#ifdef TIMER_GETTIMEOFDAY
1548
typedef struct timeval wget_sys_time;
1552
typedef time_t wget_sys_time;
1555
#ifdef TIMER_WINDOWS
1556
typedef ULARGE_INTEGER wget_sys_time;
1560
/* The starting point in time which, subtracted from the current
1561
time, yields elapsed time. */
1562
wget_sys_time start;
1564
/* The most recent elapsed time, calculated by wtimer_elapsed().
1565
Measured in milliseconds. */
1566
double elapsed_last;
1568
/* Approximately, the time elapsed between the true start of the
1569
measurement and the time represented by START. */
1570
double elapsed_pre_start;
1573
/* Allocate a timer. It is not legal to do anything with a freshly
1574
allocated timer, except call wtimer_reset() or wtimer_delete(). */
1577
wtimer_allocate (void)
1579
struct wget_timer *wt =
1580
(struct wget_timer *)xmalloc (sizeof (struct wget_timer));
1584
/* Allocate a new timer and reset it. Return the new timer. */
1589
struct wget_timer *wt = wtimer_allocate ();
1594
/* Free the resources associated with the timer. Its further use is
1598
wtimer_delete (struct wget_timer *wt)
1603
/* Store system time to WST. */
1606
wtimer_sys_set (wget_sys_time *wst)
1608
#ifdef TIMER_GETTIMEOFDAY
1609
gettimeofday (wst, NULL);
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
1626
GetSystemTime (&st);
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;
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. */
1642
wtimer_reset (struct wget_timer *wt)
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;
1651
wtimer_sys_diff (wget_sys_time *wst1, wget_sys_time *wst2)
1653
#ifdef TIMER_GETTIMEOFDAY
1654
return ((double)(wst1->tv_sec - wst2->tv_sec) * 1000
1655
+ (double)(wst1->tv_usec - wst2->tv_usec) / 1000);
1659
return 1000 * (*wst1 - *wst2);
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
1666
return (double)(signed __int64)(wst1->QuadPart - wst2->QuadPart) / 10000;
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
1676
wtimer_elapsed (struct wget_timer *wt)
1681
wtimer_sys_set (&now);
1682
elapsed = wt->elapsed_pre_start + wtimer_sys_diff (&now, &wt->start);
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.
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
1697
if (elapsed < wt->elapsed_last)
1700
wt->elapsed_pre_start = wt->elapsed_last;
1701
elapsed = wt->elapsed_last;
1704
wt->elapsed_last = elapsed;
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. */
1713
wtimer_granularity (void)
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?) */
1727
#ifdef TIMER_WINDOWS
1728
/* According to MSDN, GetSystemTime returns a broken-down time
1729
structure the smallest member of which are milliseconds. */
1734
/* This should probably be at a better place, but it doesn't really
1735
fit into html-parse.c. */
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:
1747
No other entities are recognized or replaced. */
1505
/* Print NUMBER to a statically allocated string and return a pointer
1506
to the printed representation.
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
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
1523
What you should use instead is printf("%s", number_to_static_string
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.) */
1749
html_quote_string (const char *s)
1537
number_to_static_string (wgint number)
1755
/* Pass through the string, and count the new size. */
1756
for (i = 0; *s; s++, i++)
1759
i += 4; /* `amp;' */
1760
else if (*s == '<' || *s == '>')
1761
i += 3; /* `lt;' and `gt;' */
1762
else if (*s == '\"')
1763
i += 5; /* `quot;' */
1767
res = (char *)xmalloc (i + 1);
1769
for (p = res; *s; s++)
1782
*p++ = (*s == '<' ? 'l' : 'g');
1539
static char ring[RING_SIZE][24];
1541
char *buf = ring[ringpos];
1542
number_to_string (buf, number);
1543
ringpos = (ringpos + 1) % RING_SIZE;
1809
1547
/* Determine the width of the terminal we're running on. If that's
1810
1548
not possible, return 0. */
2086
1797
#endif /* not WINDOWS */
2087
1798
#endif /* not USE_SIGNAL_TIMEOUT */
1802
/* Sleep the specified amount of seconds. On machines without
1803
nanosleep(), this may sleep shorter if interrupted by signals. */
1806
xsleep (double seconds)
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. */
1821
#else /* not HAVE_NANOSLEEP */
1823
/* If usleep is available, use it in preference to select. */
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. */
1830
seconds -= (long) seconds;
1832
usleep (seconds * 1000000);
1833
#else /* not HAVE_USLEEP */
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
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 */
1850
#endif /* not HAVE_SELECT */
1851
#endif /* not HAVE_USLEEP */
1852
#endif /* not HAVE_NANOSLEEP */
1855
#endif /* not WINDOWS */
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
1863
This implementation will not emit newlines after 76 characters of
1867
base64_encode (const char *str, int length, char *b64store)
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','+','/'
1881
const unsigned char *s = (const unsigned char *) str;
1884
/* Transform the 3x8 bits to 4x6 bits, as required by base64. */
1885
for (i = 0; i < length; i += 3)
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];
1894
/* Pad the result if necessary... */
1895
if (i == length + 1)
1897
else if (i == length + 2)
1898
*(p - 1) = *(p - 2) = '=';
1900
/* ...and zero-terminate it. */
1903
return p - b64store;
1906
#define IS_ASCII(c) (((c) & 0x80) == 0)
1907
#define IS_BASE64(c) ((IS_ASCII (c) && base64_char_to_value[c] >= 0) || c == '=')
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 { \
1913
} while (c != '\0' && !IS_BASE64 (c))
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
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
1926
base64_decode (const char *base64, char *to)
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] =
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 */
1947
const char *p = base64;
1953
unsigned long value;
1955
/* Process first byte of a quadruplet. */
1956
NEXT_BASE64_CHAR (c, p);
1960
return -1; /* illegal '=' while decoding base64 */
1961
value = base64_char_to_value[c] << 18;
1963
/* Process scond byte of a quadruplet. */
1964
NEXT_BASE64_CHAR (c, p);
1966
return -1; /* premature EOF while decoding base64 */
1968
return -1; /* illegal `=' while decoding base64 */
1969
value |= base64_char_to_value[c] << 12;
1972
/* Process third byte of a quadruplet. */
1973
NEXT_BASE64_CHAR (c, p);
1975
return -1; /* premature EOF while decoding base64 */
1979
NEXT_BASE64_CHAR (c, p);
1981
return -1; /* premature EOF while decoding base64 */
1983
return -1; /* padding `=' expected but not found */
1987
value |= base64_char_to_value[c] << 6;
1988
*q++ = 0xff & value >> 8;
1990
/* Process fourth byte of a quadruplet. */
1991
NEXT_BASE64_CHAR (c, p);
1993
return -1; /* premature EOF while decoding base64 */
1997
value |= base64_char_to_value[c];
1998
*q++ = 0xff & value;
2006
#undef NEXT_BASE64_CHAR
2008
/* Simple merge sort for use by stable_sort. Implementation courtesy
2009
Zeljko Vrba with additional debugging by Nenad Barbutov. */
2012
mergesort_internal (void *base, void *temp, size_t size, size_t from, size_t to,
2013
int (*cmpfun) PARAMS ((const void *, const void *)))
2015
#define ELT(array, pos) ((char *)(array) + (pos) * size)
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);
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);
2028
memcpy (ELT (temp, k), ELT (base, j++), size);
2030
memcpy (ELT (temp, k++), ELT (base, i++), size);
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);
2039
/* Stable sort with interface exactly like standard library's qsort.
2040
Uses mergesort internally, allocating temporary storage with
2044
stable_sort (void *base, size_t nmemb, size_t size,
2045
int (*cmpfun) PARAMS ((const void *, const void *)))
2049
void *temp = alloca (nmemb * size * sizeof (void *));
2050
mergesort_internal (base, temp, size, 0, nmemb - 1, cmpfun);