From feb5293eced0bd3a03b76d136253bd2bd5b3bc43 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 3 Dec 2023 21:43:07 +0100 Subject: [PATCH] lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP() The function should never be used; it's always used via its wrapper macro. To simplify, and reduce chances of confusion, remove the function, and implement the macro directly in terms of stpcpy(mempcpy(strnlen())). Update the documentation, and improve the example, which was rather confusing. Cc: "Serge E. Hallyn" Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 1 - lib/string/zustr2stp.c | 17 ----------------- lib/string/zustr2stp.h | 38 +++++++++----------------------------- 3 files changed, 9 insertions(+), 47 deletions(-) delete mode 100644 lib/string/zustr2stp.c diff --git a/lib/Makefile.am b/lib/Makefile.am index cc79326b38..abda2de663 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -145,7 +145,6 @@ libshadow_la_SOURCES = \ string/strncpy.h \ string/strtcpy.c \ string/strtcpy.h \ - string/zustr2stp.c \ string/zustr2stp.h \ strtoday.c \ sub.c \ diff --git a/lib/string/zustr2stp.c b/lib/string/zustr2stp.c deleted file mode 100644 index e27ced3fb6..0000000000 --- a/lib/string/zustr2stp.c +++ /dev/null @@ -1,17 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar - * SPDX-License-Identifier: BSD-3-Clause - */ - - -#include - -#include - -#ident "$Id$" - -#include "string/zustr2stp.h" - - -extern inline char *zustr2stp(char *restrict dst, const char *restrict src, - size_t sz); diff --git a/lib/string/zustr2stp.h b/lib/string/zustr2stp.h index 5ed4249506..4f29e02978 100644 --- a/lib/string/zustr2stp.h +++ b/lib/string/zustr2stp.h @@ -11,7 +11,6 @@ #include #include -#include #include #include "must_be.h" @@ -22,28 +21,20 @@ ({ \ static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \ \ - zustr2stp(dst, src, NITEMS(src)); \ + stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), ""); \ }) -inline char *zustr2stp(char *restrict dst, const char *restrict src, size_t sz); - - /* * SYNOPSIS - * char *zustr2stp(char *restrict dst, - * const char src[restrict .sz], size_t sz); + * char *ZUSTR2STP(char *restrict dst, const char src[restrict]); * * ARGUMENTS - * dst Destination buffer where to copy a string. - * - * src Source null-padded character sequence to be copied into - * dst. - * - * sz Size of the *source* buffer. + * dst Destination buffer. + * src Source null-padded character sequence. * * DESCRIPTION - * This function copies the null-padded character sequence pointed + * This macro copies the null-padded character sequence pointed * to by src, into a string at the buffer pointed to by dst. * * RETURN VALUE @@ -51,29 +42,18 @@ inline char *zustr2stp(char *restrict dst, const char *restrict src, size_t sz); * This function returns a pointer to the terminating NUL * byte. * - * ERRORS - * This function doesn't set errno. - * * CAVEATS * This function doesn't know the size of the destination buffer. * It assumes it will always be large enough. Since the size of * the source buffer is known to the caller, it should make sure to - * allocate a destination buffer of at least `sz + 1`. + * allocate a destination buffer of at least `NITEMS(src) + 1`. * * EXAMPLES - * char src[13] = "Hello, world!" // No '\0' in this buffer! - * char dst[NITEMS(src) + 1]; + * char hostname[NITEMS(utmp->ut_host) + 1]; * - * zustr2stp(dst, src, NITEMS(src)); - * puts(dst); + * len = ZUSTR2STP(hostname, utmp->ut_host) - hostname; + * puts(hostname); */ -inline char * -zustr2stp(char *restrict dst, const char *restrict src, size_t sz) -{ - return stpcpy(mempcpy(dst, src, strnlen(src, sz)), ""); -} - - #endif // include guard