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>
76 lines
1.4 KiB
C
76 lines
1.4 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
|
|
#ifndef SHADOW_INCLUDE_LIB_STRTCPY_H_
|
|
#define SHADOW_INCLUDE_LIB_STRTCPY_H_
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
|
|
#include "sizeof.h"
|
|
|
|
|
|
/*
|
|
* SYNOPSIS
|
|
* int STRTCPY(char dst[restrict], const char *restrict src);
|
|
*
|
|
* ARGUMENTS
|
|
* dst Destination buffer where to copy a string.
|
|
* src Source string to be copied into dst.
|
|
*
|
|
* DESCRIPTION
|
|
* This macro copies the string pointed to by src, into a string
|
|
* 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
|
|
* NITEMS().
|
|
*
|
|
* RETURN VALUE
|
|
* -1 If this call truncated the resulting string.
|
|
*
|
|
* strlen(dst)
|
|
* On success.
|
|
*
|
|
* ERRORS
|
|
* This function doesn't set errno.
|
|
*/
|
|
|
|
|
|
#define STRTCPY(dst, src) strtcpy(dst, src, NITEMS(dst))
|
|
|
|
|
|
inline ssize_t strtcpy(char *restrict dst, const char *restrict src,
|
|
size_t dsize);
|
|
|
|
|
|
inline ssize_t
|
|
strtcpy(char *restrict dst, const char *restrict src, size_t dsize)
|
|
{
|
|
bool trunc;
|
|
char *p;
|
|
size_t dlen, slen;
|
|
|
|
if (dsize == 0)
|
|
return -1;
|
|
|
|
slen = strnlen(src, dsize);
|
|
trunc = (slen == dsize);
|
|
dlen = slen - trunc;
|
|
|
|
p = mempcpy(dst, src, dlen);
|
|
*p = '\0';
|
|
|
|
return trunc ? -1 : slen;
|
|
}
|
|
|
|
|
|
#endif // include guard
|