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 | |
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.
-rw-r--r-- | am/system.m4 | 1 | ||||
-rw-r--r-- | gnulib.modules | 3 | ||||
-rw-r--r-- | lib/system.h | 2 | ||||
-rw-r--r-- | paxlib.modules | 3 | ||||
-rw-r--r-- | paxlib/error.c | 51 | ||||
-rw-r--r-- | paxlib/paxlib.h | 5 | ||||
-rw-r--r-- | paxlib/rtape.c | 34 | ||||
-rw-r--r-- | rmt/rmt.c | 10 | ||||
-rw-r--r-- | tests/argcv.c | 23 | ||||
-rw-r--r-- | tests/genfile.c | 72 |
10 files changed, 92 insertions, 112 deletions
diff --git a/am/system.m4 b/am/system.m4 index 2dbac5c..29e9728 100644 --- a/am/system.m4 +++ b/am/system.m4 @@ -19,7 +19,6 @@ AC_DEFUN([PU_SYSTEM],[ AC_REQUIRE([AC_STRUCT_ST_BLOCKS]) AC_CHECK_FUNCS_ONCE(lstat mkfifo setlocale) - AC_REQUIRE([gl_INTTOSTR]) AC_REQUIRE([gl_STDINT_H]) AC_SEARCH_LIBS(gethostbyname, nsl) diff --git a/gnulib.modules b/gnulib.modules index 1a2cf89..476e798 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -9,7 +9,6 @@ full-write gettext-h getopt-gnu gitlog-to-changelog -inttostr obstack parse-datetime quote @@ -17,7 +16,9 @@ quotearg safe-read stdbool strerror +strtoimax strtol +strtoumax utimensat version-etc-fsf xalloc diff --git a/lib/system.h b/lib/system.h index fcc87a1..bf035ff 100644 --- a/lib/system.h +++ b/lib/system.h @@ -423,8 +423,6 @@ char *getenv (); #include <inttypes.h> #include <intprops.h> - -#define UINTMAX_STRSIZE_BOUND INT_BUFSIZE_BOUND (uintmax_t) /* Prototypes for external functions. */ diff --git a/paxlib.modules b/paxlib.modules index 7928250..4b13595 100644 --- a/paxlib.modules +++ b/paxlib.modules @@ -13,14 +13,15 @@ getopt-gnu gettext-h hash iconv -inttostr progname quote quotearg safe-read savedir stdbool +strtoimax strtol +strtoumax unlocked-io verify xalloc diff --git a/paxlib/error.c b/paxlib/error.c index 134cef3..f363d84 100644 --- a/paxlib/error.c +++ b/paxlib/error.c @@ -99,9 +99,10 @@ chmod_error_details (char const *name, mode_t mode) void chown_error_details (char const *name, uid_t uid, gid_t gid) { + uintmax_t u = uid, g = gid; int e = errno; - ERROR ((0, e, _("%s: Cannot change ownership to uid %lu, gid %lu"), - quotearg_colon (name), (unsigned long) uid, (unsigned long) gid)); + ERROR ((0, e, _("%s: Cannot change ownership to uid %ju, gid %ju"), + quotearg_colon (name), u, g)); } void @@ -175,27 +176,25 @@ read_error (char const *name) void read_error_details (char const *name, off_t offset, size_t size) { - char buf[UINTMAX_STRSIZE_BOUND]; + intmax_t off = offset; int e = errno; ERROR ((0, e, - ngettext ("%s: Read error at byte %s, while reading %lu byte", - "%s: Read error at byte %s, while reading %lu bytes", + ngettext ("%s: Read error at byte %jd, while reading %zu byte", + "%s: Read error at byte %jd, while reading %zu bytes", size), - quotearg_colon (name), STRINGIFY_BIGINT (offset, buf), - (unsigned long) size)); + quotearg_colon (name), off, size)); } void read_warn_details (char const *name, off_t offset, size_t size) { - char buf[UINTMAX_STRSIZE_BOUND]; + intmax_t off = offset; int e = errno; WARN ((0, e, - ngettext ("%s: Warning: Read error at byte %s, while reading %lu byte", - "%s: Warning: Read error at byte %s, while reading %lu bytes", + ngettext ("%s: Warning: Read error at byte %jd, while reading %zu byte", + "%s: Warning: Read error at byte %jd, while reading %zu bytes", size), - quotearg_colon (name), STRINGIFY_BIGINT (offset, buf), - (unsigned long) size)); + quotearg_colon (name), off, size)); } void @@ -207,14 +206,13 @@ read_fatal (char const *name) void read_fatal_details (char const *name, off_t offset, size_t size) { - char buf[UINTMAX_STRSIZE_BOUND]; + intmax_t off = offset; int e = errno; FATAL_ERROR ((0, e, - ngettext ("%s: Read error at byte %s, while reading %lu byte", - "%s: Read error at byte %s, while reading %lu bytes", + ngettext ("%s: Read error at byte %jd, while reading %zu byte", + "%s: Read error at byte %jd, while reading %zu bytes", size), - quotearg_colon (name), STRINGIFY_BIGINT (offset, buf), - (unsigned long) size)); + quotearg_colon (name), off, size)); } void @@ -256,11 +254,9 @@ seek_error (char const *name) void seek_error_details (char const *name, off_t offset) { - char buf[UINTMAX_STRSIZE_BOUND]; + intmax_t off = offset; int e = errno; - ERROR ((0, e, _("%s: Cannot seek to %s"), - quotearg_colon (name), - STRINGIFY_BIGINT (offset, buf))); + ERROR ((0, e, _("%s: Cannot seek to %jd"), quotearg_colon (name), off)); } void @@ -272,11 +268,10 @@ seek_warn (char const *name) void seek_warn_details (char const *name, off_t offset) { - char buf[UINTMAX_STRSIZE_BOUND]; + int off = offset; int e = errno; - WARN ((0, e, _("%s: Warning: Cannot seek to %s"), - quotearg_colon (name), - STRINGIFY_BIGINT (offset, buf))); + WARN ((0, e, _("%s: Warning: Cannot seek to %jd"), + quotearg_colon (name), off)); } void @@ -348,10 +343,10 @@ write_error_details (char const *name, size_t status, size_t size) write_error (name); else ERROR ((0, 0, - ngettext ("%s: Wrote only %lu of %lu byte", - "%s: Wrote only %lu of %lu bytes", + ngettext ("%s: Wrote only %zu of %zu byte", + "%s: Wrote only %zu of %zu bytes", size), - name, (unsigned long int) status, (unsigned long int) size)); + name, status, size)); } void diff --git a/paxlib/paxlib.h b/paxlib/paxlib.h index c6e8382..5e82d8f 100644 --- a/paxlib/paxlib.h +++ b/paxlib/paxlib.h @@ -21,8 +21,6 @@ #ifndef _paxlib_h_ #define _paxlib_h_ -#include <inttostr.h> - /* Error reporting functions and definitions */ /* Exit status for paxutils app. Let's try to keep this list as simple as @@ -121,9 +119,6 @@ void write_error_details (char const *, size_t, size_t); _Noreturn void pax_exit (void); _Noreturn void fatal_exit (void); - -#define STRINGIFY_BIGINT(i, b) umaxtostr (i, b) - /* Name-related functions */ bool removed_prefixes_p (void); diff --git a/paxlib/rtape.c b/paxlib/rtape.c index 2cd0c36..70d69ac 100644 --- a/paxlib/rtape.c +++ b/paxlib/rtape.c @@ -598,7 +598,7 @@ rmt_read (int handle, char *buffer, size_t length) size_t rlen; size_t counter; - sprintf (command_buffer, "R%lu\n", (unsigned long) length); + sprintf (command_buffer, "R%zu\n", length); if (do_command (handle, command_buffer) == -1 || (status = get_status (handle)) == SAFE_READ_ERROR) return SAFE_READ_ERROR; @@ -625,7 +625,7 @@ rmt_write (int handle, char *buffer, size_t length) void (*pipe_handler) (); size_t written; - sprintf (command_buffer, "W%lu\n", (unsigned long) length); + sprintf (command_buffer, "W%zu\n", length); if (do_command (handle, command_buffer) == -1) return 0; @@ -654,16 +654,7 @@ off_t rmt_lseek (int handle, off_t offset, int whence) { char command_buffer[COMMAND_BUFFER_SIZE]; - char operand_buffer[UINTMAX_STRSIZE_BOUND]; - uintmax_t u = offset < 0 ? - (uintmax_t) offset : (uintmax_t) offset; - char *p = operand_buffer + sizeof operand_buffer; - - *--p = 0; - do - *--p = '0' + (int) (u % 10); - while ((u /= 10) != 0); - if (offset < 0) - *--p = '-'; + intmax_t off = offset; switch (whence) { @@ -683,7 +674,7 @@ rmt_lseek (int handle, off_t offset, int whence) abort (); } - sprintf (command_buffer, "L%s\n%d\n", p, whence); + sprintf (command_buffer, "L%jd\n%d\n", off, whence); if (do_command (handle, command_buffer) == -1) return -1; @@ -705,24 +696,13 @@ rmt_ioctl (int handle, unsigned long int operation, char *argument) #ifdef MTIOCTOP case MTIOCTOP: { + struct mtop *arg = (struct mtop *) argument; char command_buffer[COMMAND_BUFFER_SIZE]; - char operand_buffer[UINTMAX_STRSIZE_BOUND]; - uintmax_t u = (((struct mtop *) argument)->mt_count < 0 - ? - (uintmax_t) ((struct mtop *) argument)->mt_count - : (uintmax_t) ((struct mtop *) argument)->mt_count); - char *p = operand_buffer + sizeof operand_buffer; - - *--p = 0; - do - *--p = '0' + (int) (u % 10); - while ((u /= 10) != 0); - if (((struct mtop *) argument)->mt_count < 0) - *--p = '-'; + intmax_t count = arg->mt_count; /* MTIOCTOP is the easy one. Nothing is transferred in binary. */ - sprintf (command_buffer, "I%d\n%s\n", - ((struct mtop *) argument)->mt_op, p); + sprintf (command_buffer, "I%d\n%jd\n", arg->mt_op, count); if (do_command (handle, command_buffer) == -1) return -1; @@ -22,7 +22,6 @@ #include <getopt.h> #include <full-write.h> #include <configmake.h> -#include <inttostr.h> #include <error.h> #include <progname.h> #include <c-ctype.h> @@ -111,8 +110,7 @@ rmt_write (const char *fmt, ...) static void rmt_reply (uintmax_t code) { - char buf[UINTMAX_STRSIZE_BOUND]; - rmt_write ("A%s\n", umaxtostr (code, buf)); + rmt_write ("A%ju\n", code); } static void @@ -247,7 +245,7 @@ decode_open_flag (const char *mstr, int *pmode) while (mstr) { int v; - + mstr = skip_ws (mstr); if (*mstr == 0) break; @@ -260,7 +258,7 @@ decode_open_flag (const char *mstr, int *pmode) } mode |= v; - + if (*p && c_isblank (*p)) p = skip_ws (p); if (*p == 0) @@ -310,7 +308,7 @@ decode_open_flag (const char *mstr, int *pmode) In addition, a compined form is also allowed, i.e. a decimal mode followed by its symbolic representation. In this case the symbolic representation is given preference. - + Reply ----- A0\n on success, E0\n<msg>\n on error. 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); |