diff --git a/lib/Makefile.am b/lib/Makefile.am index 3f749aa1..79e00085 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -157,7 +157,9 @@ libshadow_la_SOURCES = \ xgetgrnam.c \ xgetgrgid.c \ xgetspnam.c \ - yesno.c + yesno.c \ + zustr2stp.c \ + zustr2stp.h if WITH_TCB libshadow_la_SOURCES += tcbfuncs.c tcbfuncs.h diff --git a/lib/zustr2stp.c b/lib/zustr2stp.c new file mode 100644 index 00000000..8dfe8705 --- /dev/null +++ b/lib/zustr2stp.c @@ -0,0 +1,17 @@ +/* + * SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar + * SPDX-License-Identifier: BSD-3-Clause + */ + + +#include + +#include + +#ident "$Id$" + +#include "zustr2stp.h" + + +extern inline char *zustr2stp(char *restrict dst, const char *restrict src, + size_t sz); diff --git a/lib/zustr2stp.h b/lib/zustr2stp.h new file mode 100644 index 00000000..987a7ba8 --- /dev/null +++ b/lib/zustr2stp.h @@ -0,0 +1,74 @@ +/* + * SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar + * SPDX-License-Identifier: BSD-3-Clause + */ + + +#ifndef SHADOW_INCLUDE_LIBMISC_ZUSTR2STP_H_ +#define SHADOW_INCLUDE_LIBMISC_ZUSTR2STP_H_ + + +#include + +#include +#include + +#include "mempcpy.h" + + +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); + * + * 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. + * + * DESCRIPTION + * This function 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`. + * + * EXAMPLES + * char src[13] = "Hello, world!" // No '\0' in this buffer! + * char dst[SIZEOF_ARRAY(src) + 1]; + * + * zustr2stp(dst, src, SIZEOF_ARRAY(src)); + * puts(dst); + */ + + +inline char * +zustr2stp(char *restrict dst, const char *restrict src, size_t sz) +{ + char *p; + + p = mempcpy(dst, src, strnlen(src, sz)); + *p = '\0'; + + return p; +} + + +#endif // include guard