* lib/nproc.c, lib/regex_internal.h, lib/set-permissions.c: * lib/strftime.c: Include <stdcountof.h>. * lib/nproc.c (ARRAY_SIZE): * lib/sig2str.c (NUNNAME_ENTRIES): Remove. All uses replaced with countof. * lib/regcomp.c (regerror): * lib/set-permissions.c (set_acls_from_mode): * lib/strftime.c (__strftime_internal): Use countof instead of doing it by hand. * modules/acl-permissions, modules/c-nstrftime, modules/fprintftime: * modules/nproc, modules/regex, modules/nstrftime: * modules/nstrftime-limited, modules/sig2str: Depend on stdcountof-h. --- ChangeLog | 16 ++++++++++++++++ lib/nproc.c | 7 +++---- lib/regcomp.c | 2 +- lib/regex_internal.h | 1 + lib/set-permissions.c | 8 ++++---- lib/sig2str.c | 7 +++---- lib/strftime.c | 7 ++++--- modules/acl-permissions | 1 + modules/c-nstrftime | 1 + modules/fprintftime | 1 + modules/nproc | 1 + modules/nstrftime | 1 + modules/nstrftime-limited | 1 + modules/regex | 1 + modules/sig2str | 1 + 15 files changed, 40 insertions(+), 16 deletions(-)
diff --git a/ChangeLog b/ChangeLog index 09eade9f34..7dfa750e25 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,21 @@ 2026-03-20 Paul Eggert <[email protected]> + countof-h: use countof, part 3 + * lib/nproc.c, lib/regex_internal.h, lib/set-permissions.c: + * lib/strftime.c: + Include <stdcountof.h>. + * lib/nproc.c (ARRAY_SIZE): + * lib/sig2str.c (NUNNAME_ENTRIES): + Remove. All uses replaced with countof. + * lib/regcomp.c (regerror): + * lib/set-permissions.c (set_acls_from_mode): + * lib/strftime.c (__strftime_internal): + Use countof instead of doing it by hand. + * modules/acl-permissions, modules/c-nstrftime, modules/fprintftime: + * modules/nproc, modules/regex, modules/nstrftime: + * modules/nstrftime-limited, modules/sig2str: + Depend on stdcountof-h. + boot-time: remove SIZEOF * lib/boot-time-aux.h (SIZEOF): Remove; no longer used, now that the code uses countof. diff --git a/lib/nproc.c b/lib/nproc.c index b0c9514115..ef86975fb1 100644 --- a/lib/nproc.c +++ b/lib/nproc.c @@ -25,6 +25,7 @@ #if HAVE_SETMNTENT # include <mntent.h> #endif +#include <stdcountof.h> #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -61,8 +62,6 @@ #include "minmax.h" -#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) - #define NPROC_MINIMUM 1 /* Return the number of processors available to the current process, based @@ -335,9 +334,9 @@ num_processors_available (enum nproc_query query) # endif { CTL_HW, HW_NCPU } }; - for (int i = 0; i < ARRAY_SIZE (mib); i++) + for (int i = 0; i < countof (mib); i++) { - if (sysctl (mib[i], ARRAY_SIZE (mib[i]), &nprocs, &len, NULL, 0) == 0 + if (sysctl (mib[i], countof (mib[i]), &nprocs, &len, NULL, 0) == 0 && len == sizeof (nprocs) && 0 < nprocs) return nprocs; diff --git a/lib/regcomp.c b/lib/regcomp.c index aa2f680088..3580ea4aa0 100644 --- a/lib/regcomp.c +++ b/lib/regcomp.c @@ -499,7 +499,7 @@ regerror (int errcode, const regex_t *__restrict preg, char *__restrict errbuf, { const char *msg; size_t msg_size; - int nerrcodes = sizeof __re_error_msgid_idx / sizeof __re_error_msgid_idx[0]; + int nerrcodes = countof (__re_error_msgid_idx); if (__glibc_unlikely (errcode < 0 || errcode >= nerrcodes)) /* Only error codes returned by the rest of the code should be passed diff --git a/lib/regex_internal.h b/lib/regex_internal.h index 11b745ef28..fb6c1f373d 100644 --- a/lib/regex_internal.h +++ b/lib/regex_internal.h @@ -30,6 +30,7 @@ #include <wchar.h> #include <wctype.h> #include <stdckdint.h> +#include <stdcountof.h> #include <stdint.h> #ifndef _LIBC diff --git a/lib/set-permissions.c b/lib/set-permissions.c index 8a0eadf5c4..f6a1315e0d 100644 --- a/lib/set-permissions.c +++ b/lib/set-permissions.c @@ -21,6 +21,8 @@ #include "acl.h" +#include <stdcountof.h> + #include "acl-internal.h" #include "minmax.h" @@ -251,11 +253,9 @@ set_acls_from_mode (const char *name, int desc, mode_t mode, bool *must_chmod) int ret; if (desc != -1) - ret = facl (desc, SETACL, - sizeof (entries) / sizeof (aclent_t), entries); + ret = facl (desc, SETACL, countof (entries), entries); else - ret = acl (name, SETACL, - sizeof (entries) / sizeof (aclent_t), entries); + ret = acl (name, SETACL, countof (entries), entries); if (ret < 0) { if (errno == ENOSYS || errno == EOPNOTSUPP) diff --git a/lib/sig2str.c b/lib/sig2str.c index da54234ac4..3141ff8806 100644 --- a/lib/sig2str.c +++ b/lib/sig2str.c @@ -23,6 +23,7 @@ #include <signal.h> #include <limits.h> +#include <stdcountof.h> #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -260,8 +261,6 @@ static struct numname { int num; char const name[8]; } numname_table[] = { 0, "EXIT" } }; -#define NUMNAME_ENTRIES (sizeof numname_table / sizeof numname_table[0]) - /* ISDIGIT differs from isdigit, as follows: - Its arg may be any int or unsigned int; it need not be an unsigned char or EOF. @@ -286,7 +285,7 @@ str2signum (char const *signame) } else { - for (unsigned int i = 0; i < NUMNAME_ENTRIES; i++) + for (int i = 0; i < countof (numname_table); i++) if (streq (numname_table[i].name, signame)) return numname_table[i].num; @@ -331,7 +330,7 @@ str2sig (char const *signame, int *signum) int sig2str (int signum, char *signame) { - for (unsigned int i = 0; i < NUMNAME_ENTRIES; i++) + for (int i = 0; i < countof (numname_table); i++) if (numname_table[i].num == signum) { strcpy (signame, numname_table[i].name); diff --git a/lib/strftime.c b/lib/strftime.c index af96da95e4..2872355b78 100644 --- a/lib/strftime.c +++ b/lib/strftime.c @@ -109,6 +109,7 @@ #include <limits.h> #include <locale.h> #include <stdckdint.h> +#include <stdcountof.h> #include <stddef.h> #include <stdint.h> #include <stdlib.h> @@ -1882,7 +1883,7 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize) #endif } - bufp = buf + sizeof (buf) / sizeof (buf[0]); + bufp = buf + countof (buf); if (negative_number) u_number_value = - u_number_value; @@ -1913,7 +1914,7 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize) CHAR_T sign_char = (negative_number ? L_('-') : always_output_a_sign ? L_('+') : 0); - int number_bytes = buf + sizeof buf / sizeof buf[0] - bufp; + int number_bytes = buf + countof (buf) - bufp; int number_digits = number_bytes; #if SUPPORT_NON_GREG_CALENDARS_IN_STRFTIME if (digits_base >= 0x100) @@ -2098,7 +2099,7 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize) /* Generate string value for T using time_t arithmetic; this works even if sizeof (long) < sizeof (time_t). */ - bufp = buf + sizeof (buf) / sizeof (buf[0]); + bufp = buf + countof (buf); negative_number = t < 0; do diff --git a/modules/acl-permissions b/modules/acl-permissions index 59c88016e2..94df4764f2 100644 --- a/modules/acl-permissions +++ b/modules/acl-permissions @@ -19,6 +19,7 @@ stat streq bool sys_stat-h +stdcountof-h stdint-h configure.ac: diff --git a/modules/c-nstrftime b/modules/c-nstrftime index 0498ce669a..9bab0fce3f 100644 --- a/modules/c-nstrftime +++ b/modules/c-nstrftime @@ -19,6 +19,7 @@ libc-config locale-h bool stdckdint-h +stdcountof-h streq strnul time_rz diff --git a/modules/fprintftime b/modules/fprintftime index d132388cc4..e013aecb1a 100644 --- a/modules/fprintftime +++ b/modules/fprintftime @@ -10,6 +10,7 @@ Depends-on: stdio-h sys_types-h nstrftime +stdcountof-h streq strnul time_rz diff --git a/modules/nproc b/modules/nproc index 4b62a61f9a..0a648d6ae5 100644 --- a/modules/nproc +++ b/modules/nproc @@ -10,6 +10,7 @@ Depends-on: c-ctype extensions minmax +stdcountof-h streq unistd-h diff --git a/modules/nstrftime b/modules/nstrftime index 0212d56ccc..012ce7b818 100644 --- a/modules/nstrftime +++ b/modules/nstrftime @@ -25,6 +25,7 @@ localcharset localename-unsafe [case "$host_os" in darwin*) false ;; *) true ;; esac] bool stdckdint-h +stdcountof-h stdint-h streq strnul diff --git a/modules/nstrftime-limited b/modules/nstrftime-limited index 99993f2c06..897cf27d4c 100644 --- a/modules/nstrftime-limited +++ b/modules/nstrftime-limited @@ -26,6 +26,7 @@ localcharset localename-unsafe-limited [case "$host_os" in darwin*) false ;; *) true ;; esac] bool stdckdint-h +stdcountof-h streq strnul time_rz diff --git a/modules/regex b/modules/regex index d043dc1e7a..a1176b8e90 100644 --- a/modules/regex +++ b/modules/regex @@ -39,6 +39,7 @@ mbsinit [test $ac_use_included_regex = yes] nl_langinfo [test $ac_use_included_regex = yes] bool [test $ac_use_included_regex = yes] stdckdint-h [test $ac_use_included_regex = yes] +stdcountof-h [test $ac_use_included_regex = yes] stdint-h [test $ac_use_included_regex = yes] strncpy [test $ac_use_included_regex = yes] verify [test $ac_use_included_regex = yes] diff --git a/modules/sig2str b/modules/sig2str index fd9bbcc0f2..0dba4bbf74 100644 --- a/modules/sig2str +++ b/modules/sig2str @@ -7,6 +7,7 @@ lib/sig2str.c m4/sig2str.m4 Depends-on: +stdcountof-h signal-h streq -- 2.53.0
