From: Alejandro Colomar Date: Tue, 10 Feb 2026 13:46:48 +0000 (+0100) Subject: man/: Use _Countof() instead of a custom NITEMS() X-Git-Tag: man-pages-6.17~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4903873c146c50b3660d4d6aaab50fb58fb9fec4;p=thirdparty%2Fman-pages.git man/: Use _Countof() instead of a custom NITEMS() Signed-off-by: Alejandro Colomar --- diff --git a/man/man2/bpf.2 b/man/man2/bpf.2 index 76d6d6c13..3d6a9248a 100644 --- a/man/man2/bpf.2 +++ b/man/man2/bpf.2 @@ -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"); \& diff --git a/man/man2/seccomp.2 b/man/man2/seccomp.2 index 18ae140d7..9a0b865be 100644 --- a/man/man2/seccomp.2 +++ b/man/man2/seccomp.2 @@ -1118,7 +1118,6 @@ cecilia #include \& #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, }; \& diff --git a/man/man2/seccomp_unotify.2 b/man/man2/seccomp_unotify.2 index 06203a3a0..89f5f2cd7 100644 --- a/man/man2/seccomp_unotify.2 +++ b/man/man2/seccomp_unotify.2 @@ -1429,8 +1429,6 @@ T: terminating #include #include \& -#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, }; \& diff --git a/man/man2/sysctl.2 b/man/man2/sysctl.2 index cdb6dc19e..76c5b87e2 100644 --- a/man/man2/sysctl.2 +++ b/man/man2/sysctl.2 @@ -120,8 +120,6 @@ It is not yet possible to change operating system by writing to \& #include \& -#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; \& diff --git a/man/man3/bsearch.3 b/man/man3/bsearch.3 index 9e511b3d7..40150f4ac 100644 --- a/man/man3/bsearch.3 +++ b/man/man3/bsearch.3 @@ -85,8 +85,6 @@ then retrieves desired elements using #include #include \& -#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]); diff --git a/man/man3/fread.3 b/man/man3/fread.3 index 57b8a9165..9ad56580c 100644 --- a/man/man3/fread.3 +++ b/man/man3/fread.3 @@ -109,8 +109,6 @@ Class: 0x02 #include #include \& -#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); } diff --git a/man/man3/regex.3 b/man/man3/regex.3 index 0369ede19..f6dc532f2 100644 --- a/man/man3/regex.3 +++ b/man/man3/regex.3 @@ -363,8 +363,6 @@ Always reference them by name. #include #include \& -#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(®ex, s, NITEMS(pmatch), pmatch, 0)) + if (regexec(®ex, s, _Countof(pmatch), pmatch, 0)) break; \& off = pmatch[0].rm_so + (s \- str); diff --git a/man/man3/strncat.3 b/man/man3/strncat.3 index 1185f2b36..4066ae4b1 100644 --- a/man/man3/strncat.3 +++ b/man/man3/strncat.3 @@ -94,17 +94,15 @@ Shlemiel the painter #include #include \& -#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 diff --git a/man/man7/string_copying.7 b/man/man7/string_copying.7 index 61e7bb857..1a0e760a8 100644 --- a/man/man7/string_copying.7 +++ b/man/man7/string_copying.7 @@ -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);