Prefer countof to sizeof / sizeof

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.
This commit is contained in:
Paul Eggert
2025-11-15 15:50:05 -08:00
parent 2e243986c7
commit cdc541ad52
5 changed files with 14 additions and 11 deletions
+5 -5
View 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;
+3 -2
View 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;
+3 -2
View File
@@ -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);