For arrays of char, both NITEMS() and SIZEOF_ARRAY() return the same
value. However, NITEMS() is more appropriate. Think of wide-character
equivalents of the same code; with NITEMS(), they would continue to be
valid, while with SIZEOF_ARRAY(), they would be wrong.
In the implementation of ZUSTR2STP(), we want SIZEOF_ARRAY() within the
static assert, because we're just comparing the sizes of the source and
destination buffers, and we don't care if we compare sizes or numbers of
elements, and using sizes is just simpler. But we want NITEMS() in the
zustr2stp() call, where we want to copy a specific number of characters.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
* 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
- * SIZEOF_ARRAY().
+ * NITEMS().
*
* RETURN VALUE
* -1 If this call truncated the resulting string.
*/
-#define STRTCPY(dst, src) strtcpy(dst, src, SIZEOF_ARRAY(dst))
+#define STRTCPY(dst, src) strtcpy(dst, src, NITEMS(dst))
inline ssize_t strtcpy(char *restrict dst, const char *restrict src,
#ifdef HAVE_STRUCT_UTMP_UT_HOST
} else if ( (NULL != ut)
&& ('\0' != ut->ut_host[0])) {
- hostname = XMALLOC(SIZEOF_ARRAY(ut->ut_host) + 1, char);
+ hostname = XMALLOC(NITEMS(ut->ut_host) + 1, char);
ZUSTR2STP(hostname, ut->ut_host);
#endif /* HAVE_STRUCT_UTMP_UT_HOST */
}
({ \
static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
\
- zustr2stp(dst, src, SIZEOF_ARRAY(src)); \
+ zustr2stp(dst, src, NITEMS(src)); \
})
*
* EXAMPLES
* char src[13] = "Hello, world!" // No '\0' in this buffer!
- * char dst[SIZEOF_ARRAY(src) + 1];
+ * char dst[NITEMS(src) + 1];
*
- * zustr2stp(dst, src, SIZEOF_ARRAY(src));
+ * zustr2stp(dst, src, NITEMS(src));
* puts(dst);
*/