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:49:23 -08:00
parent 2e243986c7
commit cdc541ad52
5 changed files with 14 additions and 11 deletions

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;