Prefer C99 formats like %jd to doing it by hand

It’s now safe to assume support for C99 formats like %jd, so remove
some of the longwinded formatting code put in only to be portable to
pre-C99 platforms.
* gnulib.modules: Add intprops.
* src/buffer.c (format_total_stats, try_new_volume)
(write_volume_label):
* src/checkpoint.c (format_checkpoint_string):
* src/compare.c (verify_volume):
* src/create.c (to_chars_subst, dump_regular_file):
* src/incremen.c (read_num):
* src/list.c (read_and, from_header, simple_print_header)
(print_for_mkdir):
* src/sparse.c (sparse_dump_region):
* src/system.c (dec_to_env, sys_exec_info_script)
(sys_exec_checkpoint_script):
* src/xheader.c (out_of_range_header):
Prefer C99 formats like %jd and %ju to STRINGIFY_BIGINT.
* src/common.h: Sort includes.
Include intprops.h, verify.h.  All other includes of verify.h
removed.
(intmax, uintmax): New functions and macros.
(STRINGIFY_BIGINT): Remove; no longer used.
(TIMESPEC_STRSIZE_BOUND): Make it 1 byte bigger, for negatives.
* src/create.c (MAX_VAL_WITH_DIGITS, to_base256):
Use *_WIDTH macros rather than assuming no padding bits.
Prefer UINTMAX_MAX to (uintmax_t) -1.
* src/list.c (tartime): Use strftime result rather
than running strlen later.
* src/misc.c (timetostr): New function.  Prefer it when
printing time_t values.
This commit is contained in:
Paul Eggert
2024-08-04 01:41:43 -07:00
parent 6c91bd82e1
commit c26111742a
13 changed files with 171 additions and 234 deletions
+19 -40
View File
@@ -125,9 +125,9 @@ cachedir_file_p (int fd)
/* The maximum uintmax_t value that can be represented with DIGITS digits,
assuming that each digit is BITS_PER_DIGIT wide. */
#define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
((digits) * (bits_per_digit) < UINTMAX_WIDTH \
? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
: (uintmax_t) -1)
: UINTMAX_MAX)
/* The maximum uintmax_t value that can be represented with octal
digits and a trailing NUL in BUFFER. */
@@ -185,7 +185,7 @@ to_base256 (bool negative, uintmax_t value, char *where, size_t size)
{
uintmax_t v = value;
uintmax_t propagated_sign_bits =
((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
((uintmax_t) - negative << (UINTMAX_WIDTH - LG_256));
size_t i = size;
do
@@ -218,31 +218,12 @@ to_chars_subst (bool negative, bool gnu_format, uintmax_t value, size_t valsize,
uintmax_t maxval = (gnu_format
? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
: MAX_VAL_WITH_DIGITS (size - 1, LG_8));
char valbuf[UINTMAX_STRSIZE_BOUND + 1];
char maxbuf[UINTMAX_STRSIZE_BOUND];
char minbuf[UINTMAX_STRSIZE_BOUND + 1];
char const *minval_string;
char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
char const *value_string;
if (gnu_format)
{
uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
char *p = STRINGIFY_BIGINT (m, minbuf + 1);
*--p = '-';
minval_string = p;
}
else
minval_string = "0";
intmax_t minval = (!gnu_format ? 0
: ckd_sub (&minval, -1, maxval) ? INTMAX_MIN
: minval);
char const *valuesign = &"-"[!negative];
if (negative)
{
char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
*--p = '-';
value_string = p;
}
else
value_string = STRINGIFY_BIGINT (value, valbuf);
value = -value;
if (substitute)
{
@@ -256,18 +237,15 @@ to_chars_subst (bool negative, bool gnu_format, uintmax_t value, size_t valsize,
Apart from this they are completely identical. */
uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
char subbuf[UINTMAX_STRSIZE_BOUND + 1];
char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
if (negsub)
*--sub_string = '-';
WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
value_string, type, minval_string, maxval_string,
sub_string));
char const *ssign = &"-"[!negsub];
WARN ((0, 0, _("value %s%ju out of %s range %jd..%ju;"
" substituting %s%ju"),
valuesign, value, type, minval, maxval, ssign, s));
return to_chars (negsub, s, valsize, 0, where, size, type);
}
else
ERROR ((0, 0, _("value %s out of %s range %s..%s"),
value_string, type, minval_string, maxval_string));
ERROR ((0, 0, _("value %s%ju out of %s range %jd..%ju"),
valuesign, value, type, minval, maxval));
return false;
}
@@ -1097,15 +1075,16 @@ dump_regular_file (int fd, struct tar_stat_info *st)
if (count != bufsize)
{
char buf[UINTMAX_STRSIZE_BOUND];
memset (blk->buffer + count, 0, bufsize - count);
WARNOPT (WARN_FILE_SHRANK,
(0, 0,
ngettext ("%s: File shrank by %s byte; padding with zeros",
"%s: File shrank by %s bytes; padding with zeros",
ngettext (("%s: File shrank by %jd byte;"
" padding with zeros"),
("%s: File shrank by %jd bytes;"
" padding with zeros"),
size_left),
quotearg_colon (st->orig_file_name),
STRINGIFY_BIGINT (size_left, buf)));
intmax (size_left)));
if (! ignore_failed_read_option)
set_exit_status (TAREXIT_DIFFERS);
pad_archive (size_left - (bufsize - count));