diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2022-09-10 15:13:28 -0500 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2022-09-10 15:39:38 -0500 |
commit | 2bf63fcba72c4f4bc54a4caf53d7923c1f9f174f (patch) | |
tree | a466361955cef6e34765fbec4734d91d23029222 /tests | |
parent | 9ae062098da4383a6c9b39e245c1a2888a645352 (diff) | |
download | paxutils-2bf63fcba72c4f4bc54a4caf53d7923c1f9f174f.tar.gz |
Modernize integer handling
It’s now safe to assume C99 sprintf %jd, etc.
* am/system.m4 (PU_SYSTEM): Do not require gl_INTTOSTR.
* gnulib.modules, paxlib.modules:
Remove inttostr. Add strtoimax, strtoumax.
* lib/system.h (UINTMAX_STRSIZE_BOUND): Remove.
* paxlib/error.c (chown_error_details, read_error_details)
(read_warn_details, read_fatal_details, seek_error_details)
(seek_warn_details, write_error_details):
* paxlib/rtape.c (rmt_read, rmt_write, rmt_lseek, rmt_ioctl):
* rmt/rmt.c (rmt_reply):
* tests/genfile.c (verify_file, print_stat):
Use C99 printf formats instead of doing it by hand.
* paxlib/paxlib.h, rmt/rmt.c, tests/genfile.c:
Do not include inttostr.h.
* paxlib/paxlib.h (STRINGIFY_BIGINT): Remove.
* tests/argcv.c (xtonum): Use strtol for signed int, not strtoul.
* tests/genfile.c (checkpoint, verify_file, parse_opt)
(exec_checkpoint, process_checkpoint, exec_command):
Prefer uintmax_t to size_t for counters unrelated to object size.
(PRINT_INT): New macro.
(print_stat): Prefer uintmax_t to unsigned for modes.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/argcv.c | 23 | ||||
-rw-r--r-- | tests/genfile.c | 72 |
2 files changed, 54 insertions, 41 deletions
diff --git a/tests/argcv.c b/tests/argcv.c index dfde2ff..a53047c 100644 --- a/tests/argcv.c +++ b/tests/argcv.c @@ -115,7 +115,7 @@ int argcv_escape_char (int c) { char *p; - + for (p = escape_transtab + sizeof(escape_transtab) - 2; p > escape_transtab; p -= 2) { @@ -124,19 +124,18 @@ argcv_escape_char (int c) } return -1; } - + static int xtonum (const char *src, int base, size_t cnt) { - int val; char *p; char tmp[4]; /* At most three characters + zero */ - + /* Notice: No use to check `cnt'. It should be either 2 or 3 */ memcpy (tmp, src, cnt); tmp[cnt] = 0; - val = strtoul (tmp, &p, base); + long int val = strtol (tmp, &p, base); return (*p == 0) ? val : -1; } @@ -171,7 +170,7 @@ static void unescape_copy (char *dst, const char *src, size_t n) { int c; - + while (n > 0) { n--; @@ -204,7 +203,7 @@ unescape_copy (char *dst, const char *src, size_t n) } } break; - + case '0': ++src; --n; @@ -229,7 +228,7 @@ unescape_copy (char *dst, const char *src, size_t n) } } break; - + default: *dst++ = argcv_unescape_char (*src++); n--; @@ -254,7 +253,7 @@ escape_copy (char *dst, const char *src) *dst++ = '"'; } else if (*src != '\t' && isprint ((unsigned char) *src)) - *dst++ = *src; + *dst++ = *src; else { int c = argcv_escape_char (*src); @@ -353,11 +352,11 @@ argcv_string (int argc, char **argv, char **pstring) int toklen; toklen = escaped_length (argv[i], "e); - + len += toklen + 2; if (quote) len += 2; - + buffer = realloc (buffer, len); if (buffer == NULL) return 1; @@ -388,7 +387,7 @@ main(int xargc, char **xargv) int i, argc; char **argv; char *s; - + argcv_get (xargv[1] ? xargv[1]:command, "=", "#", &argc, &argv); printf ("%d args:\n", argc); for (i = 0; i < argc; i++) diff --git a/tests/genfile.c b/tests/genfile.c index 284d762..4c7c745 100644 --- a/tests/genfile.c +++ b/tests/genfile.c @@ -26,7 +26,6 @@ #include <argp.h> #include <argcv.h> #include <parse-datetime.h> -#include <inttostr.h> #include <fcntl.h> #include <sys/stat.h> #include <c-ctype.h> @@ -71,7 +70,7 @@ static off_t seek_offset = 0; static enum pattern pattern = DEFAULT_PATTERN; /* Next checkpoint number */ -size_t checkpoint; +uintmax_t checkpoint; enum genfile_mode { @@ -268,8 +267,11 @@ verify_file (char *file_name) error (0, errno, _("stat(%s) failed"), file_name); if (st.st_size != file_length + seek_offset) - error (EXIT_FAILURE, 0, _("requested file length %lu, actual %lu"), - (unsigned long)st.st_size, (unsigned long)file_length); + { + intmax_t requested = st.st_size, actual = file_length; + error (EXIT_FAILURE, 0, _("requested file length %jd, actual %jd"), + requested, actual); + } if (!quiet && mode == mode_sparse && !ST_IS_SPARSE (st)) error (EXIT_UNAVAILABLE, 0, _("created file is not sparse")); @@ -279,7 +281,7 @@ verify_file (char *file_name) struct action { struct action *next; - size_t checkpoint; + uintmax_t checkpoint; int action; char *name; off_t size; @@ -335,7 +337,7 @@ parse_opt (int key, char *arg, struct argp_state *state) case 'q': quiet = 1; break; - + case 's': mode = mode_sparse; break; @@ -363,7 +365,7 @@ parse_opt (int key, char *arg, struct argp_state *state) { char *p; - checkpoint = strtoul (arg, &p, 0); + checkpoint = strtoumax (arg, &p, 0); if (*p) argp_error (state, _("Error parsing number near `%s'"), p); } @@ -536,7 +538,7 @@ make_fragment (int fd, char *offstr, char *mapstr) for (i = 0; i < block_size; i++) buffer[i] = i & 255; break; - + case ZEROS_PATTERN: memset (buffer, 0, block_size); break; @@ -544,7 +546,7 @@ make_fragment (int fd, char *offstr, char *mapstr) if (lseek (fd, displ, SEEK_CUR) == -1) error (EXIT_FAILURE, errno, "lseek"); - + for (; n; n--) { if (write (fd, buffer, block_size) != block_size) @@ -590,7 +592,7 @@ generate_sparse_file (int argc, char **argv) while (n > 0 && c_isspace (buf[n-1])) buf[--n] = 0; - + n = strcspn (buf, " \t"); buf[n++] = 0; while (buf[n] && c_isblank (buf[n])) @@ -615,6 +617,18 @@ generate_sparse_file (int argc, char **argv) /* Status Mode */ +#define PRINT_INT(expr) \ + if (EXPR_SIGNED (expr)) \ + { \ + intmax_t printval = expr; \ + printf ("%jd", printval); \ + } \ + else \ + { \ + uintmax_t printval = expr; \ + printf ("%ju", printval); \ + } + void print_time (time_t t) { @@ -628,7 +642,6 @@ print_stat (const char *name) { char *fmt, *p; struct stat st; - char buf[UINTMAX_STRSIZE_BOUND]; if (stat (name, &st)) { @@ -644,18 +657,18 @@ print_stat (const char *name) if (strcmp (p, "name") == 0) printf ("%s", name); else if (strcmp (p, "dev") == 0) - printf ("%lu", (unsigned long) st.st_dev); + PRINT_INT (st.st_dev); else if (strcmp (p, "ino") == 0) - printf ("%lu", (unsigned long) st.st_ino); + PRINT_INT (st.st_ino); else if (strncmp (p, "mode", 4) == 0) { - unsigned val = st.st_mode; + uintmax_t val = st.st_mode; if (ispunct ((unsigned char) p[4])) { char *q; - val &= strtoul (p + 5, &q, 8); + val &= strtoumax (p + 5, &q, 8); if (*q) { printf ("\n"); @@ -667,30 +680,30 @@ print_stat (const char *name) printf ("\n"); error (EXIT_USAGE, 0, _("Unknown field `%s'"), p); } - printf ("%0o", val); + printf ("%0jo", val); } else if (strcmp (p, "nlink") == 0) - printf ("%lu", (unsigned long) st.st_nlink); + PRINT_INT (st.st_nlink); else if (strcmp (p, "uid") == 0) - printf ("%ld", (long unsigned) st.st_uid); + PRINT_INT (st.st_uid); else if (strcmp (p, "gid") == 0) - printf ("%lu", (unsigned long) st.st_gid); + PRINT_INT (st.st_gid); else if (strcmp (p, "size") == 0) - printf ("%s", umaxtostr (st.st_size, buf)); + PRINT_INT (st.st_size); else if (strcmp (p, "blksize") == 0) - printf ("%s", umaxtostr (st.st_blksize, buf)); + PRINT_INT (st.st_blksize); else if (strcmp (p, "blocks") == 0) - printf ("%s", umaxtostr (st.st_blocks, buf)); + PRINT_INT (st.st_blocks); else if (strcmp (p, "atime") == 0) - printf ("%lu", (unsigned long) st.st_atime); + PRINT_INT (st.st_atime); else if (strcmp (p, "atimeH") == 0) print_time (st.st_atime); else if (strcmp (p, "mtime") == 0) - printf ("%lu", (unsigned long) st.st_mtime); + PRINT_INT (st.st_mtime); else if (strcmp (p, "mtimeH") == 0) print_time (st.st_mtime); else if (strcmp (p, "ctime") == 0) - printf ("%lu", (unsigned long) st.st_ctime); + PRINT_INT (st.st_ctime); else if (strcmp (p, "ctimeH") == 0) print_time (st.st_ctime); else if (strcmp (p, "sparse") == 0) @@ -715,7 +728,7 @@ void exec_checkpoint (struct action *p) { if (verbose) - printf ("processing checkpoint %lu\n", (unsigned long) p->checkpoint); + printf ("processing checkpoint %ju\n", p->checkpoint); switch (p->action) { case OPT_TOUCH: @@ -788,7 +801,7 @@ exec_checkpoint (struct action *p) } void -process_checkpoint (size_t n) +process_checkpoint (uintmax_t n) { struct action *p, *prev = NULL; @@ -830,7 +843,7 @@ exec_command (int argc, char **argv) char **xargv; int i; char checkpoint_option[80]; - + /* Insert --checkpoint option. FIXME: This assumes that argv does not use traditional tar options (without dash). @@ -897,7 +910,8 @@ exec_command (int argc, char **argv) && memcmp (p, CHECKPOINT_TEXT, sizeof CHECKPOINT_TEXT - 1) == 0) { char *end; - size_t n = strtoul (p + sizeof CHECKPOINT_TEXT - 1, &end, 10); + uintmax_t n = strtoumax (p + sizeof CHECKPOINT_TEXT - 1, + &end, 10); if (!(*end && !isspace ((unsigned char) *end))) { process_checkpoint (n); |