983a844633
It's a wrapper around zustr2stp() that calls SIZEOF_ARRAY() internally. The function call is usually --in our code base, always-- called with an array as the second argument. For such an argument, one should call SIZEOF_ARRAY(). To avoid mistakes, and simplify usage, let's add this macro that does it internally. BTW, this macro doesn't have any issues of double evaluation, because sizeof() doesn't evaluate its argument (unless it's a VLA, but then the static_assert(3) within SIZEOF_ARRAY() makes sure VLAs are not allowed). Cc: Christian Göttsche <cgzones@googlemail.com> Cc: Serge Hallyn <serge@hallyn.com> Cc: Iker Pedrosa <ipedrosa@redhat.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
79 lines
1.7 KiB
C
79 lines
1.7 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <alx@kernel.org>
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
|
|
#ifndef SHADOW_INCLUDE_LIBMISC_ZUSTR2STP_H_
|
|
#define SHADOW_INCLUDE_LIBMISC_ZUSTR2STP_H_
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
|
|
#include "mempcpy.h"
|
|
#include "sizeof.h"
|
|
|
|
|
|
#define ZUSTR2STP(dst, src) zustr2stp(dst, src, SIZEOF_ARRAY(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);
|
|
*
|
|
* 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
|