]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
authorAlejandro Colomar <alx@kernel.org>
Wed, 21 May 2025 11:33:37 +0000 (13:33 +0200)
committerSerge Hallyn <serge@hallyn.com>
Tue, 3 Jun 2025 14:34:55 +0000 (09:34 -0500)
countof() is the name blessed by the C Committee for ISO C2y.
Use it if available, and define it if not.

countof() will be provided by GCC 16 and Clang 21.

This is mostly a scripted change:

$ grep -rl NITEMS | xargs sed -i s/NITEMS/countof/;

Apart from the scripted changes, I've adjusted white-space alignment,
and of course the definition at "lib/sizeof.h".

Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#subsubsection.0.6.5.4.5>
Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#section.0.7.21>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
19 files changed:
lib/adds.h
lib/audit_help.c
lib/fs/readlink/readlinknul.h
lib/must_be.h
lib/sizeof.h
lib/string/sprintf/snprintf.h
lib/string/sprintf/stpeprintf.h
lib/string/strcpy/stpecpy.h
lib/string/strcpy/strncat.h
lib/string/strcpy/strncpy.h
lib/string/strcpy/strtcpy.h
lib/string/strdup/strndupa.h
lib/string/strdup/xstrndup.h
lib/string/strftime.h
lib/time/day_to_str.h
lib/utmp.c
src/login_nopam.c
tests/unit/test_snprintf.c
tests/unit/test_strtcpy.c

index 5e4fa2749e6d5e79caa56573e9565179dadbaf92..b4cd8c258d4233e867ae0216f445ac5d2c55556a 100644 (file)
@@ -20,7 +20,7 @@
 ({                                                                            \
        long  addend_[] = {a, b, __VA_ARGS__};                                \
                                                                               \
-       addslN(NITEMS(addend_), addend_);                                     \
+       addslN(countof(addend_), addend_);                                    \
 })
 
 
index 8b24025528c77b182157ec63f32ea008f74d8c83..710069b8a06a543ea019cb72de884d3eee221152 100644 (file)
@@ -98,7 +98,7 @@ audit_logger_with_group(int type, const char *op, const char *name,
 {
        int len;
        char enc_group[GROUP_NAME_MAX_LENGTH * 2 + 1];
-       char buf[NITEMS(enc_group) + 100];
+       char buf[countof(enc_group) + 100];
 
        if (audit_fd < 0)
                return;
index ed2f34d135c4436af184f253479c20d2d987fd97..0791df57d86d456a44c0a917140083011ec70c65 100644 (file)
@@ -17,7 +17,7 @@
 #include "sizeof.h"
 
 
-#define READLINKNUL(link, buf)  readlinknul(link, buf, NITEMS(buf))
+#define READLINKNUL(link, buf)  readlinknul(link, buf, countof(buf))
 
 
 ATTR_STRING(1)
index 6ae6c7998f2e962b6e0081c1997691ba11d50b37..2c866e7baef43b5f7a1bf4ce60a13c73f9af5a63 100644 (file)
  * EXAMPLES
  *     #define must_be_array(a)  must_be(is_array(a))
  *
- *     #define NITEMS(a)  (sizeof(a) / sizeof(*(a)) + must_be_array(a))
+ *     #define countof(a)  (sizeof(a) / sizeof(*(a)) + must_be_array(a))
  *
  *     int foo[42];
- *     int bar[NITEMS(foo)];
+ *     int bar[countof(foo)];
  */
 
 
index 6051b7777d2478ad2689f9c02e587628a9a5a791..fc1a2dd3e0b036e127554661ef1b922faed0818c 100644 (file)
@@ -1,7 +1,5 @@
-/*
- * SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <alx@kernel.org>
- * SPDX-License-Identifier: BSD-3-Clause
- */
+// SPDX-FileCopyrightText: 2022-2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
 
 
 #ifndef SHADOW_INCLUDE_LIBMISC_SIZEOF_H_
@@ -11,6 +9,9 @@
 #include <config.h>
 
 #include <limits.h>
+#if __has_include(<stdcountof.h>)
+# include <stdcountof.h>
+#endif
 #include <sys/types.h>
 
 #include "must_be.h"
 #define memberof(T, member)  ((T){}.member)
 #define WIDTHOF(x)           (sizeof(x) * CHAR_BIT)
 #define SIZEOF_ARRAY(a)      (sizeof(a) + must_be_array(a))
-#define NITEMS(a)            (SIZEOF_ARRAY((a)) / sizeof((a)[0]))
-#define STRLEN(s)            (NITEMS("" s "") - 1)
+
+#if !defined(countof)
+# define countof(a)          (SIZEOF_ARRAY((a)) / sizeof((a)[0]))
+#endif
+
+#define STRLEN(s)            (countof("" s "") - 1)
 
 
 #endif  // include guard
index d181c661ac7b4e1e00073c95c2db72b1e626c574..4395c71f4e6f3dd993c481ebd5f49cb138b95d7c 100644 (file)
@@ -18,7 +18,7 @@
 
 #define SNPRINTF(s, fmt, ...)                                                 \
 (                                                                             \
-       snprintf_(s, NITEMS(s), fmt __VA_OPT__(,) __VA_ARGS__)                \
+       snprintf_(s, countof(s), fmt __VA_OPT__(,) __VA_ARGS__)               \
 )
 
 
index ccd844bc25b1cbf0e56a36208eafa37d54b3720d..c34e4efa84df0131f22c9b3df10d83bb2ac2b441 100644 (file)
@@ -40,7 +40,7 @@ inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
  *
  *     end     Pointer to one after the last element of the buffer
  *             pointed to by `dst`.  Usually, it should be calculated
- *             as `dst + NITEMS(dst)`.
+ *             as `dst + countof(dst)`.
  *
  *     fmt     Format string
  *
index e9debe4ec191b1ab3380ea5cfe691f3718a2c1e2..cc7dc0a2da7b1f8b338d39d3ff93702d44bf18be 100644 (file)
@@ -32,7 +32,7 @@ inline char *stpecpy(char *dst, char *end, const char *restrict src);
  *
  *     end     Pointer to one after the last element of the buffer
  *             pointed to by `dst`.  Usually, it should be calculated
- *             as `dst + NITEMS(dst)`.
+ *             as `dst + countof(dst)`.
  *
  *     src     Source string to be copied into dst.
  *
index 6bb9907bba770c649ef8cbfec6bca70bb7f1ea2e..c88d89285f9f50d2ea9966f12754910a57d3ce4e 100644 (file)
@@ -13,7 +13,7 @@
 #include "sizeof.h"
 
 
-#define STRNCAT(dst, src)  strncat(dst, src, NITEMS(src))
+#define STRNCAT(dst, src)  strncat(dst, src, countof(src))
 
 
 #endif  // include guard
index fcb8720e2ad33971f08644b7f133ae2bba346bc1..391d0d3f0f857e06cd504f26e09de530719ced59 100644 (file)
@@ -13,7 +13,7 @@
 #include "sizeof.h"
 
 
-#define STRNCPY(dst, src)  strncpy(dst, src, NITEMS(dst))
+#define STRNCPY(dst, src)  strncpy(dst, src, countof(dst))
 
 
 #endif  // include guard
index 4d35e1b81189183e29326a68d0e4c6e80ca0112b..aa89f99c01ae0b43d1b24865e9269c52b68c92b5 100644 (file)
@@ -31,7 +31,7 @@
  *     at the buffer pointed to by dst.  If the destination buffer,
  *     isn't large enough to hold the copy, the resulting string is
  *     truncated.  The size of the buffer is calculated internally via
- *     NITEMS().
+ *     countof().
  *
  * RETURN VALUE
  *     -1      If this call truncated the resulting string.
@@ -44,7 +44,7 @@
  */
 
 
-#define STRTCPY(dst, src)  strtcpy(dst, src, NITEMS(dst))
+#define STRTCPY(dst, src)  strtcpy(dst, src, countof(dst))
 
 
 ATTR_STRING(2)
index 80b8c3dcdbdebfbc3756fb2efa22e2a3e0e8bb59..844c76bae17eb60e5afaaa1ff7797857bec4f5c7 100644 (file)
@@ -20,7 +20,7 @@
 #endif
 
 
-#define STRNDUPA(s)  strndupa(s, NITEMS(s))
+#define STRNDUPA(s)  strndupa(s, countof(s))
 
 
 #endif  // include guard
index 88263c30099a11dbe602fd14e9316d689b5517c3..8d93eb3f88a4a4ae01f5a2dfa7c43f63bdccf161 100644 (file)
@@ -18,7 +18,7 @@
 // Similar to strndup(3), but ensure that 's' is an array, and exit on ENOMEM.
 #define XSTRNDUP(s)                                                           \
 (                                                                             \
-       STRNCAT(strcpy(XMALLOC(strnlen(s, NITEMS(s)) + 1, char), ""), s)      \
+       STRNCAT(strcpy(XMALLOC(strnlen(s, countof(s)) + 1, char), ""), s)     \
 )
 
 
index 2c86e89d15ed925ba16e64fb008eaa968a7966b0..8e71237dd59606659b2cd6f3e65624746803f6b6 100644 (file)
@@ -14,7 +14,7 @@
 
 
 // string format time
-#define STRFTIME(dst, fmt, tm)  strftime(dst, NITEMS(dst), fmt, tm)
+#define STRFTIME(dst, fmt, tm)  strftime(dst, countof(dst), fmt, tm)
 
 
 #endif  // include guard
index fe3308d8cc5aad75a1ac93debc95231585cc4538..3feb089e237c4d66af76c9ae046effa0129f7553 100644 (file)
@@ -16,7 +16,7 @@
 #include "string/strcpy/strtcpy.h"
 
 
-#define DAY_TO_STR(str, day)   day_to_str(NITEMS(str), str, day)
+#define DAY_TO_STR(str, day)   day_to_str(countof(str), str, day)
 
 
 inline void day_to_str(size_t size, char buf[size], long day);
index 09fc2c9ebcb349dec89ce6771bf9d6d4088d1e53..ff886354ac052cbcd89c2f3fb8beae6255ffe8e5 100644 (file)
@@ -36,7 +36,7 @@
 #ident "$Id$"
 
 
-#define UTX_LINESIZE  NITEMS(memberof(struct utmpx, ut_line))
+#define UTX_LINESIZE  countof(memberof(struct utmpx, ut_line))
 
 
 /*
index b7a7bd54f8c8002180d6de6d431bd7850ec37c8e..856da11d5226ce2fa1b707bd7f338053ea078b82 100644 (file)
@@ -275,7 +275,7 @@ static const char *resolve_hostname (const char *string)
 
        addr_str = host;
        gai_err = getnameinfo(addrs[0].ai_addr, addrs[0].ai_addrlen,
-                             host, NITEMS(host), NULL, 0, NI_NUMERICHOST);
+                             host, countof(host), NULL, 0, NI_NUMERICHOST);
        if (gai_err != 0) {
                SYSLOG ((LOG_ERR, "getnameinfo(%s): %s", string, gai_strerror(gai_err)));
                addr_str = string;
index bdef8ac9ab51042f5c2c7dc81c57bce3b840fe41..b5144a61b399f09d51a41d40f813977de61c8e7d 100644 (file)
@@ -36,7 +36,7 @@ main(void)
 static void
 test_SNPRINTF_trunc(void **state)
 {
-       char  buf[NITEMS("foo")];
+       char  buf[countof("foo")];
 
        // Test that we're not returning SIZE_MAX
        assert_true(SNPRINTF(buf, "f%su", "oo") < 0);
@@ -50,7 +50,7 @@ test_SNPRINTF_trunc(void **state)
 static void
 test_SNPRINTF_ok(void **state)
 {
-       char  buf[NITEMS("foo")];
+       char  buf[countof("foo")];
 
        assert_true(SNPRINTF(buf, "%s", "foo") == strlen("foo"));
        assert_true(strcmp(buf, "foo") == 0);
index c27d7c9dbe5005363d96e9ae90a3184f13bdfd62..edf8fc9f3d4311b83e5bcee3ffd4e5cc1ee63061 100644 (file)
@@ -37,7 +37,7 @@ main(void)
 static void
 test_STRTCPY_trunc(void **state)
 {
-       char  buf[NITEMS("foo")];
+       char  buf[countof("foo")];
 
        // Test that we're not returning SIZE_MAX
        assert_true(STRTCPY(buf, "fooo") < 0);
@@ -51,7 +51,7 @@ test_STRTCPY_trunc(void **state)
 static void
 test_STRTCPY_ok(void **state)
 {
-       char  buf[NITEMS("foo")];
+       char  buf[countof("foo")];
 
        assert_int_equal(STRTCPY(buf, "foo"), strlen("foo"));
        assert_string_equal(buf, "foo");