From 3c6093cf1358c194aa77f6d099687c5d766732d4 Mon Sep 17 00:00:00 2001 From: hniksic Date: Thu, 5 Apr 2001 19:51:41 -0700 Subject: [PATCH] [svn] Remove VERY_LONG_FORMAT. Instead, define function very_long_to_string for printing VERY_LONG_TYPE numbers. Published in . --- src/ChangeLog | 7 +++++++ src/sysdep.h | 34 +++++++++++++++++++++++----------- src/utils.c | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 076826cf..2edb1eb3 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2001-04-06 Hrvoje Niksic + + * sysdep.h: Don't define VERY_LONG_FORMAT. + + * utils.c (very_long_to_string): New function. + (legible_very_long): Use it; don't use VERY_LONG_FORMAT. + 2001-04-04 Christian Fraenkel * url.c (parse_uname): Would run past the end of the string if the diff --git a/src/sysdep.h b/src/sysdep.h index 32ff0364..5ecef669 100644 --- a/src/sysdep.h +++ b/src/sysdep.h @@ -5,8 +5,8 @@ This file is part of Wget. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -111,18 +111,30 @@ do { \ DEBUGP (("Closing fd %d\n", x)); \ } while (0) -/* Define a "very long" type useful for storing large non-negative - integers, e.g. the total number of bytes downloaded. This needs to - be an integral type at least 64 bits wide. On the machines where - `long' is 64-bit, we use long. Otherwise, we check whether `long - long' is available and if yes, use that. Otherwise, we give up and - just use `long'. */ +/* Define a large ("very long") type useful for storing large + non-negative quantities that exceed sizes of normal download, such + as the *total* number of bytes downloaded. To fit today's needs, + this needs to be an integral type at least 64 bits wide. On the + machines where `long' is 64-bit, we use long. Otherwise, we check + whether `long long' is available and if yes, use that. If long + long is unavailable, we give up and just use `long'. + + This check could be smarter and moved to configure, which could + check for a bunch of non-standard types such as uint64_t. But I + don't see the need for it -- the current test will work on all + modern architectures, and if it fails, nothing bad happens, we just + end up with long. + + Note: you cannot use VERY_LONG_TYPE along with printf(). When you + need to print it, use very_long_to_string(). */ + #if (SIZEOF_LONG >= 8) || !defined(HAVE_LONG_LONG) +/* either long is "big enough", or long long is unavailable which + leaves long as the only choice. */ # define VERY_LONG_TYPE unsigned long -# define VERY_LONG_FORMAT "%lu" -#else /* long is smaller than 8 bytes, but long long is available. */ +#else /* use long long */ +/* long is smaller than 8 bytes, but long long is available. */ # define VERY_LONG_TYPE unsigned long long -# define VERY_LONG_FORMAT "%llu" #endif /* use long long */ /* Defined in cmpt.c: */ diff --git a/src/utils.c b/src/utils.c index 9075e3ad..1988e607 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1296,13 +1296,53 @@ legible (long l) return legible_1 (inbuf); } +/* Write a string representation of NUMBER into the provided buffer. + We cannot use sprintf() because we cannot be sure whether the + platform supports printing of what we chose for VERY_LONG_TYPE. + + Example: Gcc supports `long long' under many platforms, but on many + of those the native libc knows nothing of it and therefore cannot + print it. + + How long BUFFER needs to be depends on the platform and the content + of NUMBER. For 64-bit VERY_LONG_TYPE (the most common case), 24 + bytes are sufficient. Using more might be a good idea. + + This function does not go through the hoops that long_to_string + goes to because it doesn't need to be fast. (It's called perhaps + once in a Wget run.) */ + +static void +very_long_to_string (char *buffer, VERY_LONG_TYPE number) +{ + int i = 0; + int j; + + /* Print the number backwards... */ + do + { + buffer[i++] = '0' + number % 10; + number /= 10; + } + while (number); + + /* ...and reverse the order of the digits. */ + for (j = 0; j < i / 2; j++) + { + char c = buffer[j]; + buffer[j] = buffer[i - 1 - j]; + buffer[i - 1 - j] = c; + } + buffer[i] = '\0'; +} + /* The same as legible(), but works on VERY_LONG_TYPE. See sysdep.h. */ char * legible_very_long (VERY_LONG_TYPE l) { char inbuf[128]; /* Print the number into the buffer. */ - sprintf (inbuf, VERY_LONG_FORMAT, l); + very_long_to_string (inbuf, l); return legible_1 (inbuf); }