lib/string/: Move string-related files to string/ subdir
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
committed by
Serge Hallyn
parent
d1ad64b40f
commit
dbb37b1b31
19
lib/string/sprintf.c
Normal file
19
lib/string/sprintf.c
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#ident "$Id$"
|
||||
|
||||
#include "string/sprintf.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
extern inline int xasprintf(char **restrict s, const char *restrict fmt, ...);
|
||||
extern inline int xvasprintf(char **restrict s, const char *restrict fmt,
|
||||
va_list ap);
|
||||
56
lib/string/sprintf.h
Normal file
56
lib/string/sprintf.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_SPRINTF_H_
|
||||
#define SHADOW_INCLUDE_LIB_SPRINTF_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "attr.h"
|
||||
#include "defines.h"
|
||||
|
||||
|
||||
format_attr(printf, 2, 3)
|
||||
inline int xasprintf(char **restrict s, const char *restrict fmt, ...);
|
||||
format_attr(printf, 2, 0)
|
||||
inline int xvasprintf(char **restrict s, const char *restrict fmt, va_list ap);
|
||||
|
||||
|
||||
inline int
|
||||
xasprintf(char **restrict s, const char *restrict fmt, ...)
|
||||
{
|
||||
int len;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
len = xvasprintf(s, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
inline int
|
||||
xvasprintf(char **restrict s, const char *restrict fmt, va_list ap)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = vasprintf(s, fmt, ap);
|
||||
if (len == -1) {
|
||||
perror("asprintf");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
20
lib/string/stpecpy.c
Normal file
20
lib/string/stpecpy.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#if !defined(HAVE_STPECPY)
|
||||
|
||||
#ident "$Id$"
|
||||
|
||||
#include "string/stpecpy.h"
|
||||
|
||||
|
||||
extern inline char *stpecpy(char *dst, char *end, const char *restrict src);
|
||||
|
||||
|
||||
#endif // !HAVE_STPECPY
|
||||
90
lib/string/stpecpy.h
Normal file
90
lib/string/stpecpy.h
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STPECPY_H_
|
||||
#define SHADOW_INCLUDE_LIB_STPECPY_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#if !defined(HAVE_STPECPY)
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
inline char *stpecpy(char *dst, char *end, const char *restrict src);
|
||||
|
||||
|
||||
/*
|
||||
* SYNOPSIS
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
inline char *
|
||||
stpecpy(char *dst, char *end, const char *restrict src)
|
||||
{
|
||||
bool trunc;
|
||||
char *p;
|
||||
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;
|
||||
|
||||
p = mempcpy(dst, src, dlen);
|
||||
*p = '\0';
|
||||
|
||||
return p + trunc;
|
||||
}
|
||||
|
||||
|
||||
#endif // !HAVE_STPECPY
|
||||
#endif // include guard
|
||||
25
lib/string/stpeprintf.c
Normal file
25
lib/string/stpeprintf.c
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#if !defined(HAVE_STPEPRINTF)
|
||||
|
||||
#ident "$Id$"
|
||||
|
||||
#include "string/stpeprintf.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
extern inline char *stpeprintf(char *dst, char *end, const char *restrict fmt,
|
||||
...);
|
||||
extern inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
|
||||
va_list ap);
|
||||
|
||||
|
||||
#endif // !HAVE_STPEPRINTF
|
||||
120
lib/string/stpeprintf.h
Normal file
120
lib/string/stpeprintf.h
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STPEPRINTF_H_
|
||||
#define SHADOW_INCLUDE_LIB_STPEPRINTF_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#if !defined(HAVE_STPEPRINTF)
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "attr.h"
|
||||
#include "defines.h"
|
||||
|
||||
|
||||
format_attr(printf, 3, 4)
|
||||
inline char *stpeprintf(char *dst, char *end, const char *restrict fmt, ...);
|
||||
|
||||
format_attr(printf, 3, 0)
|
||||
inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
|
||||
va_list ap);
|
||||
|
||||
|
||||
/*
|
||||
* SYNOPSIS
|
||||
* [[gnu::format(printf, 3, 4)]]
|
||||
* char *_Nullable stpeprintf(char *_Nullable dst, char end[0],
|
||||
* const char *restrict fmt, ...);
|
||||
*
|
||||
* [[gnu::format(printf, 3, 0)]]
|
||||
* char *_Nullable vstpeprintf(char *_Nullable dst, char end[0],
|
||||
* const char *restrict fmt, va_list ap);
|
||||
*
|
||||
*
|
||||
* ARGUMENTS
|
||||
* dst Destination buffer where to write 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)`.
|
||||
*
|
||||
* fmt Format string
|
||||
*
|
||||
* ...
|
||||
* ap Variadic argument list
|
||||
*
|
||||
* DESCRIPTION
|
||||
* These functions are very similar to [v]snprintf(3).
|
||||
*
|
||||
* The destination buffer is limited by a pointer to its end --one
|
||||
* after its last element-- instead of a size. This allows
|
||||
* chaining calls to it safely, unlike [v]snprintf(3), which is
|
||||
* difficult to chain without invoking Undefined Behavior.
|
||||
*
|
||||
* RETURN VALUE
|
||||
* dst + strlen(dst)
|
||||
* • On success, these functions return 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 this function failed (see ERRORS).
|
||||
* • If `dst == NULL` (a previous chained call to these
|
||||
* functions failed).
|
||||
*
|
||||
* ERRORS
|
||||
* These functions may fail for the same reasons as vsnprintf(3).
|
||||
*/
|
||||
|
||||
|
||||
inline char *
|
||||
stpeprintf(char *dst, char *end, const char *restrict fmt, ...)
|
||||
{
|
||||
char *p;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
p = vstpeprintf(dst, end, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
inline char *
|
||||
vstpeprintf(char *dst, char *end, const char *restrict fmt, va_list ap)
|
||||
{
|
||||
int len;
|
||||
ptrdiff_t size;
|
||||
|
||||
if (dst == end)
|
||||
return end;
|
||||
if (dst == NULL)
|
||||
return NULL;
|
||||
|
||||
size = end - dst;
|
||||
len = vsnprintf(dst, size, fmt, ap);
|
||||
|
||||
if (len == -1)
|
||||
return NULL;
|
||||
if (len >= size)
|
||||
return end;
|
||||
|
||||
return dst + len;
|
||||
}
|
||||
|
||||
|
||||
#endif // !HAVE_STPEPRINTF
|
||||
#endif // include guard
|
||||
21
lib/string/strncpy.h
Normal file
21
lib/string/strncpy.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRNCPY_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRNCPY_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "sizeof.h"
|
||||
|
||||
|
||||
#define STRNCPY(dst, src) strncpy(dst, src, NITEMS(dst))
|
||||
|
||||
|
||||
#endif // include guard
|
||||
18
lib/string/strtcpy.c
Normal file
18
lib/string/strtcpy.c
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ident "$Id$"
|
||||
|
||||
#include "string/strtcpy.h"
|
||||
|
||||
|
||||
extern inline ssize_t strtcpy(char *restrict dst, const char *restrict src,
|
||||
size_t dsize);
|
||||
75
lib/string/strtcpy.h
Normal file
75
lib/string/strtcpy.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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
|
||||
17
lib/string/zustr2stp.c
Normal file
17
lib/string/zustr2stp.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ident "$Id$"
|
||||
|
||||
#include "string/zustr2stp.h"
|
||||
|
||||
|
||||
extern inline char *zustr2stp(char *restrict dst, const char *restrict src,
|
||||
size_t sz);
|
||||
84
lib/string/zustr2stp.h
Normal file
84
lib/string/zustr2stp.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "must_be.h"
|
||||
#include "sizeof.h"
|
||||
|
||||
|
||||
#define ZUSTR2STP(dst, src) \
|
||||
({ \
|
||||
static_assert(!is_array(dst) || sizeof(dst) > SIZEOF_ARRAY(src), ""); \
|
||||
\
|
||||
zustr2stp(dst, 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);
|
||||
*
|
||||
* 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[NITEMS(src) + 1];
|
||||
*
|
||||
* zustr2stp(dst, src, NITEMS(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
|
||||
Reference in New Issue
Block a user