]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Prefer countof to sizeof / sizeof
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 15 Nov 2025 23:49:23 +0000 (15:49 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 15 Nov 2025 23:50:05 +0000 (15:50 -0800)
C2y plans to introduce a new countof operator that will be
convenient for GNU tar, so start using it now via Gnulib.
* gnulib.modules: Add stdcountof-h.
* lib/wordsplit.c, src/buffer.c, src/suffix.c, src/tar.c:
Include stdcountof.h, and prefer countof (X) to sizeof X / sizeof *X.

gnulib.modules
lib/wordsplit.c
src/buffer.c
src/suffix.c
src/tar.c

index ede48729bd604a81d3fb0dc1a34fd53158da8abe..93c88934e37cf5736e52d97aabdcba5267ef6d4c 100644 (file)
@@ -102,6 +102,7 @@ selinux-at
 setenv
 stat-time
 std-gnu23
+stdcountof-h
 stddef-h
 stdint-h
 stpcpy
index 22057209e1b5805586745178a0ad096d5aca0734..a777d16a2aa638b39e556b29a1dd74020d2d295b 100644 (file)
@@ -26,6 +26,7 @@
 #include <pwd.h>
 #include <stdarg.h>
 #include <stdckdint.h>
+#include <stdcountof.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -2590,14 +2591,13 @@ static char const *const wordsplit_errstr[] = {
   N_("unbalanced parenthesis"),
   N_("globbing error")
 };
-enum { wordsplit_nerrs = sizeof wordsplit_errstr / sizeof *wordsplit_errstr };
 
 const char *
 wordsplit_strerror (struct wordsplit const *ws)
 {
   if (ws->ws_errno == WRDSE_USERERR)
     return ws->ws_usererr;
-  if (ws->ws_errno < wordsplit_nerrs)
+  if (ws->ws_errno < countof (wordsplit_errstr))
     return wordsplit_errstr[ws->ws_errno];
   return N_("unknown error");
 }
index d9ffaa7dbf02355e44278f326f4943a87f30471c..3fde3feb2131be17155c9cf0665bccd1e529ea82 100644 (file)
@@ -33,6 +33,8 @@
 #include "common.h"
 #include <rmt.h>
 
+#include <stdcountof.h>
+
 /* Work around GCC bug 117236.  */
 # if 13 <= __GNUC__
 #  pragma GCC diagnostic ignored "-Wnull-dereference"
@@ -328,7 +330,6 @@ static struct zip_magic const magic[] = {
   { ct_xz,       6, "\xFD" "7zXZ" },
   { ct_zstd,     4, "\x28\xB5\x2F\xFD" },
 };
-enum { n_zip_magic = sizeof magic / sizeof *magic };
 
 static struct zip_program zip_program[] = {
   { ct_compress, COMPRESS_PROGRAM, "-Z" },
@@ -343,14 +344,13 @@ static struct zip_program zip_program[] = {
   { ct_xz,       XZ_PROGRAM,       "-J" },
   { ct_zstd,     ZSTD_PROGRAM,     "--zstd" },
 };
-enum { n_zip_programs = sizeof zip_program / sizeof *zip_program };
 
 static struct zip_program const *
 find_zip_program (enum compress_type type, int *pstate)
 {
   int i;
 
-  for (i = *pstate; i < n_zip_programs; i++)
+  for (i = *pstate; i < countof (zip_program); i++)
     {
       if (zip_program[i].type == type)
        {
@@ -367,7 +367,7 @@ first_decompress_program (int *pstate)
 {
   struct zip_program const *zp;
 
-  *pstate = n_zip_programs;
+  *pstate = countof (zip_program);
 
   if (use_compress_program_option)
     return use_compress_program_option;
@@ -427,7 +427,7 @@ check_compressed_archive (bool *pshort)
     /* Probably a valid header */
     return ct_tar;
 
-  for (p = magic + 2; p < magic + n_zip_magic; p++)
+  for (p = magic + 2; p < magic + countof (magic); p++)
     if (memeq (record_start->buffer, p->magic, p->length))
       return p->type;
 
index b1118f15032b536155f3c8f4f9558cef15d28eed..2fb22da145cb338a8b8106d55d3fa7eb1a03b668 100644 (file)
@@ -19,6 +19,8 @@
 #include <system.h>
 #include "common.h"
 
+#include <stdcountof.h>
+
 struct compression_suffix
 {
   char suffix[sizeof "tbz2"];  /* "tbz2" is tied for longest.  */
@@ -72,8 +74,7 @@ find_compression_suffix (char const *name, idx_t *ret_len)
       suf++;
 
       for (struct compression_suffix const *p = compression_suffixes;
-          p < (compression_suffixes
-               + sizeof compression_suffixes / sizeof *compression_suffixes);
+          p < compression_suffixes + countof (compression_suffixes);
           p++)
        if (streq (p->suffix, suf))
          return p;
index b777298134537991fc6e78191fa29aff9d680142..7dcdf68ac9c629611f703b7c70a103a11c99cbfc 100644 (file)
--- a/src/tar.c
+++ b/src/tar.c
@@ -136,6 +136,8 @@ bool delay_directory_restore_option;
 #include <priv-set.h>
 #include <savedir.h>
 
+#include <stdcountof.h>
+
 /* Local declarations.  */
 
 #ifndef DEFAULT_ARCHIVE_FORMAT
@@ -1106,12 +1108,11 @@ decode_signal (const char *name)
     { "INT", SIGINT },
     { "QUIT", SIGQUIT }
   };
-  enum { nsigtab = sizeof sigtab / sizeof *sigtab };
   char const *s = name;
 
   if (strncmp (s, "SIG", 3) == 0)
     s += 3;
-  for (struct sigtab const *p = sigtab; p < sigtab + nsigtab; p++)
+  for (struct sigtab const *p = sigtab; p < sigtab + countof (sigtab); p++)
     if (streq (p->name, s))
       return p->signo;
   paxfatal (0, _("Unknown signal name: %s"), name);