]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
man/: Use _Countof() instead of a custom NITEMS()
authorAlejandro Colomar <alx@kernel.org>
Tue, 10 Feb 2026 13:46:48 +0000 (14:46 +0100)
committerAlejandro Colomar <alx@kernel.org>
Tue, 10 Feb 2026 13:46:48 +0000 (14:46 +0100)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
man/man2/bpf.2
man/man2/seccomp.2
man/man2/seccomp_unotify.2
man/man2/sysctl.2
man/man3/bsearch.3
man/man3/fread.3
man/man3/regex.3
man/man3/strncat.3
man/man7/string_copying.7

index 76d6d6c138eac742f32a69878c8b32dcf6c6b98a..3d6a9248af9f64bd6f67b03e9f1a840b042eba2c 100644 (file)
@@ -1208,8 +1208,6 @@ riscv
 .SH EXAMPLES
 .\" SRC BEGIN (bpf.c)
 .EX
-#define NITEMS(a)  (sizeof(a) / sizeof(*(a)))
-\&
 /* bpf+sockets example:
  * 1.  create array map of 256 elements
  * 2.  load program that counts number of packets received
@@ -1254,7 +1252,7 @@ main(int argc, char *argv[])
     };
 \&
     prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, prog,
-                            NITEMS(prog), "GPL");
+                            _Countof(prog), "GPL");
 \&
     sock = open_raw_sock("lo");
 \&
index 18ae140d7d54f6c97159b34187b94b019cf4e96e..9a0b865be78a75ea2a9d607670c9d4600f57403c 100644 (file)
@@ -1118,7 +1118,6 @@ cecilia
 #include <unistd.h>
 \&
 #define X32_SYSCALL_BIT 0x40000000
-#define NITEMS(arr)  (sizeof(arr) / sizeof((arr)[0]))
 \&
 static int
 install_filter(int syscall_nr, unsigned int t_arch, int f_errno)
@@ -1169,7 +1168,7 @@ install_filter(int syscall_nr, unsigned int t_arch, int f_errno)
     };
 \&
     struct sock_fprog prog = {
-        .len = NITEMS(filter),
+        .len = _Countof(filter),
         .filter = filter,
     };
 \&
index 06203a3a06f6be3c7809929c0740c531c672957a..89f5f2cd7b3ad3fb51c6027c2f82cb78502d8e00 100644 (file)
@@ -1429,8 +1429,6 @@ T: terminating
 #include <sys/un.h>
 #include <unistd.h>
 \&
-#define NITEMS(arr)  (sizeof(arr) / sizeof((arr)[0]))
-\&
 /* Send the file descriptor \[aq]fd\[aq] over the connected UNIX domain socket
    \[aq]sockfd\[aq].  Returns 0 on success, or \-1 on error.  */
 \&
@@ -1610,7 +1608,7 @@ installNotifyFilter(void)
     };
 \&
     struct sock_fprog prog = {
-        .len = NITEMS(filter),
+        .len = _Countof(filter),
         .filter = filter,
     };
 \&
index cdb6dc19e2f481ae2b550a6a09e55492b71d709a..76c5b87e2e77b193e56b58e279e222789b656c79 100644 (file)
@@ -120,8 +120,6 @@ It is not yet possible to change operating system by writing to
 \&
 #include <linux/sysctl.h>
 \&
-#define NITEMS(arr)  (sizeof(arr) / sizeof((arr)[0]))
-\&
 int _sysctl(struct __sysctl_args *args);
 \&
 #define OSNAMESZ 100
@@ -136,7 +134,7 @@ main(void)
 \&
     memset(&args, 0, sizeof(args));
     args.name = name;
-    args.nlen = NITEMS(name);
+    args.nlen = _Countof(name);
     args.oldval = osname;
     args.oldlenp = &osnamelth;
 \&
index 9e511b3d79675a53158bb595dc81b28d7f155b5f..40150f4ac1c1b2546dbc4243d8141d629b8595a3 100644 (file)
@@ -85,8 +85,6 @@ then retrieves desired elements using
 #include <stdlib.h>
 #include <string.h>
 \&
-#define NITEMS(arr)  (sizeof((arr)) / sizeof((arr)[0]))
-\&
 struct mi {
     int         nr;
     const char  *name;
@@ -110,13 +108,13 @@ compmi(const void *m1, const void *m2)
 int
 main(int argc, char *argv[])
 {
-    qsort(months, NITEMS(months), sizeof(months[0]), compmi);
+    qsort(months, _Countof(months), sizeof(months[0]), compmi);
     for (size_t i = 1; i < argc; i++) {
         struct mi key;
         struct mi *res;
 \&
         key.name = argv[i];
-        res = bsearch(&key, months, NITEMS(months),
+        res = bsearch(&key, months, _Countof(months),
                       sizeof(months[0]), compmi);
         if (res == NULL)
             printf("\[aq]%s\[aq]: unknown month\[rs]n", argv[i]);
index 57b8a9165870cfb28ed113c791e8ca46f7debbe4..9ad56580c9c259f63e7660a9f211949974e10a4d 100644 (file)
@@ -109,8 +109,6 @@ Class: 0x02
 #include <stdio.h>
 #include <stdlib.h>
 \&
-#define NITEMS(arr)  (sizeof(arr) / sizeof((arr)[0]))
-\&
 int
 main(void)
 {
@@ -124,8 +122,8 @@ main(void)
         return EXIT_FAILURE;
     }
 \&
-    ret = fread(buffer, sizeof(*buffer), NITEMS(buffer), fp);
-    if (ret != NITEMS(buffer)) {
+    ret = fread(buffer, sizeof(*buffer), _Countof(buffer), fp);
+    if (ret != _Countof(buffer)) {
         fprintf(stderr, "fread() failed: %zu\[rs]n", ret);
         exit(EXIT_FAILURE);
     }
index 0369ede19445c6ff818ec30ea2da44b473d2b118..f6dc532f22540c96506a221f610d2878a258c03e 100644 (file)
@@ -363,8 +363,6 @@ Always reference them by name.
 #include <stdlib.h>
 #include <regex.h>
 \&
-#define NITEMS(arr)  (sizeof((arr)) / sizeof((arr)[0]))
-\&
 static const char *const str =
         "1) John Driverhacker;\[rs]n2) John Doe;\[rs]n3) John Foo;\[rs]n";
 static const char *const re = "John.*o";
@@ -383,7 +381,7 @@ int main(void)
     printf("Matches:\[rs]n");
 \&
     for (unsigned int i = 0; ; i++) {
-        if (regexec(&regex, s, NITEMS(pmatch), pmatch, 0))
+        if (regexec(&regex, s, _Countof(pmatch), pmatch, 0))
             break;
 \&
         off = pmatch[0].rm_so + (s \- str);
index 1185f2b36b4e8e0fa6789c607fb0cdbaab4ea694..4066ae4b17d3c82d00eae01d39786b60acdad453 100644 (file)
@@ -94,17 +94,15 @@ Shlemiel the painter
 #include <string.h>
 #include <utmp.h>
 \&
-#define NITEMS(arr)     (sizeof((arr)) / sizeof((arr)[0]))
-\&
 void print_ut_user(struct utmp *ut);
 \&
 void
 print_ut_user(struct utmp *ut)
 {
-       char  buf[NITEMS(ut\->ut_user) + 1];
+       char  buf[_Countof(ut\->ut_user) + 1];
 \&
        strcpy(buf, "");
-       strncat(buf, ut\->ut_user, NITEMS(ut\->ut_user));
+       strncat(buf, ut\->ut_user, _Countof(ut\->ut_user));
        puts(buf);
 }
 .EE
index 61e7bb8577359701562a5ff65b2fc48bd8ad5a82..1a0e760a8a34e7b1a3b419888c0820328536ae59 100644 (file)
@@ -53,10 +53,10 @@ strncat
 .BI "               size_t " dsize );
 .P
 // Chain-copy a null-padded character sequence into a character sequence.
-.I mempcpy(dst, src, strnlen(src, NITEMS(src)));
+.I mempcpy(dst, src, strnlen(src, _Countof(src)));
 .P
 // Chain-copy a null-padded character sequence into a string.
-.I stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), \[dq]\[dq]);
+.I stpcpy(mempcpy(dst, src, strnlen(src, _Countof(src))), \[dq]\[dq]);
 .P
 // Catenate a null-padded character sequence into a string.
 .BR "char *strncat(" "size_t ssize;"
@@ -259,7 +259,7 @@ or
 .P
 To read a null-padded character sequence,
 use
-.IR "strnlen(src,\ NITEMS(src))" ,
+.IR "strnlen(src,\ _Countof(src))" ,
 and then you can treat it as a length-bounded character sequence;
 or use
 .BR strncat (3)
@@ -468,7 +468,7 @@ Do not confuse this function with
 .BR strncpy (3);
 they are not related at all.
 .IP
-.I \%stpcpy(mempcpy(dst,\ src,\ strnlen(src,\ NITEMS(src))),\ \[dq]\[dq])
+.I \%stpcpy(mempcpy(dst,\ src,\ strnlen(src,\ _Countof(src))),\ \[dq]\[dq])
 is a faster alternative to this function.
 .\" ----- DESCRIPTION :: Functions :: strndup(3) ----------------------/
 .TP
@@ -638,13 +638,13 @@ puts(buf);
 .TP
 .BR stpecpy ()
 .EX
-end = buf + NITEMS(buf);
+end = buf + _Countof(buf);
 p = buf;
 p = stpecpy(p, end, "Hello ");
 p = stpecpy(p, end, "world");
 p = stpecpy(p, end, "!");
 if (p == NULL) {
-    len = NITEMS(buf) \- 1;
+    len = _Countof(buf) \- 1;
     goto toolong;
 }
 len = p \- buf;
@@ -654,7 +654,7 @@ puts(buf);
 .TP
 .BR strtcpy ()
 .EX
-len = strtcpy(buf, "Hello world!", NITEMS(buf));
+len = strtcpy(buf, "Hello world!", _Countof(buf));
 if (len == \-1)
     goto toolong;
 puts(buf);
@@ -665,12 +665,12 @@ puts(buf);
 .TQ
 .BR strlcat (3bsd)
 .EX
-if (strlcpy(buf, "Hello ", NITEMS(buf)) >= NITEMS(buf))
+if (strlcpy(buf, "Hello ", _Countof(buf)) >= _Countof(buf))
     goto toolong;
-if (strlcat(buf, "world", NITEMS(buf)) >= NITEMS(buf))
+if (strlcat(buf, "world", _Countof(buf)) >= _Countof(buf))
     goto toolong;
-len = strlcat(buf, "!", NITEMS(buf));
-if (len >= NITEMS(buf))
+len = strlcat(buf, "!", _Countof(buf));
+if (len >= _Countof(buf))
     goto toolong;
 puts(buf);
 .EE
@@ -678,8 +678,8 @@ puts(buf);
 .TP
 .BR stpncpy (3)
 .EX
-p = stpncpy(u->ut_user, "alx", NITEMS(u->ut_user));
-if (NITEMS(u->ut_user) < strlen("alx"))
+p = stpncpy(u->ut_user, "alx", _Countof(u->ut_user));
+if (_Countof(u->ut_user) < strlen("alx"))
     goto toolong;
 len = p \- u->ut_user;
 fwrite(u->ut_user, 1, len, stdout);
@@ -688,29 +688,29 @@ fwrite(u->ut_user, 1, len, stdout);
 .TP
 .BR strncpy (3)
 .EX
-strncpy(u->ut_user, "alx", NITEMS(u->ut_user));
-if (NITEMS(u->ut_user) < strlen("alx"))
+strncpy(u->ut_user, "alx", _Countof(u->ut_user));
+if (_Countof(u->ut_user) < strlen("alx"))
     goto toolong;
-len = strnlen(u->ut_user, NITEMS(u->ut_user));
+len = strnlen(u->ut_user, _Countof(u->ut_user));
 fwrite(u->ut_user, 1, len, stdout);
 .EE
-.\" ----- EXAMPLES :: mempcpy(dst, src, strnlen(src, NITEMS(src))) ----/
+.\" ----- EXAMPLES :: mempcpy(dst, src, strnlen(src, _Countof(src))) ----/
 .TP
-.I mempcpy(dst, src, strnlen(src, NITEMS(src)))
+.I mempcpy(dst, src, strnlen(src, _Countof(src)))
 .EX
-char  buf[NITEMS(u->ut_user)];
+char  buf[_Countof(u->ut_user)];
 p = buf;
-p = mempcpy(p, u->ut_user, strnlen(u->ut_user, NITEMS(u->ut_user)));
+p = mempcpy(p, u->ut_user, strnlen(u->ut_user, _Countof(u->ut_user)));
 len = p \- buf;
 fwrite(buf, 1, len, stdout);
 .EE
-.\" ----- EXAMPLES :: stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), "")
+.\" ----- EXAMPLES :: stpcpy(mempcpy(dst, src, strnlen(src, _Countof(src))), "")
 .TP
-.I stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), \[dq]\[dq])
+.I stpcpy(mempcpy(dst, src, strnlen(src, _Countof(src))), \[dq]\[dq])
 .EX
-char  buf[NITEMS(u->ut_user) + 1];
+char  buf[_Countof(u->ut_user) + 1];
 p = buf;
-p = mempcpy(p, u->ut_user, strnlen(u->ut_user, NITEMS(u->ut_user)));
+p = mempcpy(p, u->ut_user, strnlen(u->ut_user, _Countof(u->ut_user)));
 p = stpcpy(p, "");
 len = p \- buf;
 puts(buf);
@@ -719,9 +719,9 @@ puts(buf);
 .TP
 .BR strncat (3)
 .EX
-char  buf[NITEMS(u->ut_user) + 1];
+char  buf[_Countof(u->ut_user) + 1];
 strcpy(buf, "");
-strncat(buf, u->ut_user, NITEMS(u->ut_user));
+strncat(buf, u->ut_user, _Countof(u->ut_user));
 len = strlen(buf);
 puts(buf);
 .EE
@@ -729,7 +729,7 @@ puts(buf);
 .TP
 .BR strndup (3)
 .EX
-buf = strndup(u->ut_user, NITEMS(u->ut_user));
+buf = strndup(u->ut_user, _Countof(u->ut_user));
 len = strlen(buf);
 puts(buf);
 free(buf);