Skip to content

Commit

Permalink
lib/string/zustr2stp.[ch]: Remove zustr2stp(); keep ZUSTR2STP()
Browse files Browse the repository at this point in the history
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" <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Dec 4, 2023
1 parent 93a5c47 commit 535e3f6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 47 deletions.
1 change: 0 additions & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
17 changes: 0 additions & 17 deletions lib/string/zustr2stp.c

This file was deleted.

38 changes: 9 additions & 29 deletions lib/string/zustr2stp.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <config.h>

#include <assert.h>
#include <stddef.h>
#include <string.h>

#include "must_be.h"
Expand All @@ -22,58 +21,39 @@
({ \
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
* dst + strlen(dst)
* 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

0 comments on commit 535e3f6

Please sign in to comment.