lib/string/strcpy/, lib/, src/, tests/: Move all copying APIs to a subdirectory

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2024-06-27 11:48:16 +02:00
committed by Serge Hallyn
parent 22272347b6
commit 40ab806066
29 changed files with 88 additions and 95 deletions
+12
View File
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/strcpy/stpecpy.h"
#if !defined(HAVE_STPECPY)
extern inline char *stpecpy(char *dst, char *end, const char *restrict src);
#endif
+88
View File
@@ -0,0 +1,88 @@
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STPECPY_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STPECPY_H_
#include <config.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "attr.h"
#if !defined(HAVE_STPECPY)
ATTR_STRING(3)
inline char *stpecpy(char *dst, char *end, const char *restrict src);
#endif
/*
* SYNOPSIS
* [[gnu::null_terminated_string_arg(3)]]
* char *_Nullable stpecpy(char *_Nullable dst, char end[0],
* const char *restrict src);
*
* ARGUMENTS
* dst Destination buffer where to copy a string.
*
* end Pointer to one after the last element of the buffer
* pointed to by `dst`. Usually, it should be calculated
* as `dst + NITEMS(dst)`.
*
* src Source string to be copied into dst.
*
* DESCRIPTION
* This function copies the string pointed to by src, into a string
* at the buffer pointed to by dst. If the destination buffer,
* limited by a pointer to its end --one after its last element--,
* isn't large enough to hold the copy, the resulting string is
* truncated.
*
* This function can be chained with calls to [v]stpeprintf().
*
* RETURN VALUE
* dst + strlen(dst)
* • On success, this function returns a pointer to the
* terminating NUL byte.
*
* end
* • If this call truncated the resulting string.
* • If `dst == end` (a previous chained call to these
* functions truncated).
* NULL
* • If `dst == NULL` (a previous chained call to
* [v]stpeprintf() failed).
*
* ERRORS
* This function doesn't set errno.
*/
#if !defined(HAVE_STPECPY)
inline char *
stpecpy(char *dst, char *end, const char *restrict src)
{
bool trunc;
size_t dsize, dlen, slen;
if (dst == end)
return end;
if (dst == NULL)
return NULL;
dsize = end - dst;
slen = strnlen(src, dsize);
trunc = (slen == dsize);
dlen = slen - trunc;
return stpcpy(mempcpy(dst, src, dlen), "") + trunc;
}
#endif
#endif // include guard
+7
View File
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/strcpy/strncpy.h"
+19
View File
@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STRNCPY_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STRNCPY_H_
#include <config.h>
#include <string.h>
#include "sizeof.h"
#define STRNCPY(dst, src) strncpy(dst, src, NITEMS(dst))
#endif // include guard
+14
View File
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include <stddef.h>
#include <sys/types.h>
#include "string/strcpy/strtcpy.h"
extern inline ssize_t strtcpy(char *restrict dst, const char *restrict src,
size_t dsize);
+77
View File
@@ -0,0 +1,77 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STRTCPY_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STRTCPY_H_
#include <config.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include "attr.h"
#include "sizeof.h"
/*
* SYNOPSIS
* [[gnu::null_terminated_string_arg(2)]]
* 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))
ATTR_STRING(2)
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;
size_t dlen, slen;
if (dsize == 0)
return -1;
slen = strnlen(src, dsize);
trunc = (slen == dsize);
dlen = slen - trunc;
stpcpy(mempcpy(dst, src, dlen), "");
if (trunc)
return -1;
return slen;
}
#endif // include guard
+7
View File
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/strcpy/zustr2stp.h"
+58
View File
@@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_ZUSTR2STP_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_ZUSTR2STP_H_
#include <config.h>
#include <assert.h>
#include <string.h>
#include "must_be.h"
#include "sizeof.h"
/*
* SYNOPSIS
* char *ZUSTR2STP(char *restrict dst, const char src[restrict]);
*
* ARGUMENTS
* dst Destination buffer.
* src Source null-padded character sequence.
*
* DESCRIPTION
* This macro copies at most NITEMS(src) non-null bytes from the
* array pointed to by src, followed by a null character, to the
* buffer pointed to by dst.
*
* RETURN VALUE
* dst + strlen(dst)
* This function returns a pointer to the terminating NUL
* byte.
*
* 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 `NITEMS(src) + 1`.
*
* EXAMPLES
* char hostname[NITEMS(utmp->ut_host) + 1];
*
* len = ZUSTR2STP(hostname, utmp->ut_host) - hostname;
* puts(hostname);
*/
#define ZUSTR2STP(dst, src) \
({ \
static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
\
stpcpy(mempcpy(dst, src, strnlen(src, NITEMS(src))), ""); \
})
#endif // include guard