New upstream version 4.17.0~rc1

This commit is contained in:
Chris Hofstaedtler
2024-12-06 19:17:25 +01:00
parent 9f68246a01
commit f78a468368
858 changed files with 9076 additions and 13407 deletions

View File

@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include <stddef.h>
#include "string/memset/memzero.h"
extern inline void *memzero(void *ptr, size_t size);
extern inline char *strzero(char *s);

View File

@@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: 2022-2023, Christian Göttsche <cgzones@googlemail.com>
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_MEMSET_MEMZERO_H_
#define SHADOW_INCLUDE_LIB_STRING_MEMSET_MEMZERO_H_
#include <config.h>
#include <stddef.h>
#include <string.h>
#include <strings.h>
#include "sizeof.h"
#define MEMZERO(arr) memzero(arr, SIZEOF_ARRAY(arr))
inline void *memzero(void *ptr, size_t size);
inline char *strzero(char *s);
inline void *
memzero(void *ptr, size_t size)
{
#if defined(HAVE_MEMSET_EXPLICIT)
memset_explicit(ptr, 0, size);
#elif defined(HAVE_EXPLICIT_BZERO)
explicit_bzero(ptr, size);
#else
bzero(ptr, size);
__asm__ __volatile__ ("" : : "r"(ptr) : "memory");
#endif
return ptr;
}
inline char *
strzero(char *s)
{
return memzero(s, strlen(s));
}
#endif // include guard

View File

@@ -1,24 +0,0 @@
/*
* 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);
extern inline int snprintf_(char *restrict s, size_t size,
const char *restrict fmt, ...);
extern inline int vsnprintf_(char *restrict s, size_t size,
const char *restrict fmt, va_list ap);

View File

@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/sprintf/snprintf.h"
#include <stdarg.h>
#include <stddef.h>
extern inline int snprintf_(char *restrict s, size_t size,
const char *restrict fmt, ...);
extern inline int vsnprintf_(char *restrict s, size_t size,
const char *restrict fmt, va_list ap);

View File

@@ -1,11 +1,9 @@
/*
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_SPRINTF_H_
#define SHADOW_INCLUDE_LIB_SPRINTF_H_
#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_SNPRINTF_H_
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_SNPRINTF_H_
#include <config.h>
@@ -15,19 +13,15 @@
#include <stdio.h>
#include "attr.h"
#include "defines.h"
#include "sizeof.h"
#define SNPRINTF(s, fmt, ...) \
snprintf_(s, NITEMS(s), fmt __VA_OPT__(,) __VA_ARGS__)
( \
snprintf_(s, NITEMS(s), fmt __VA_OPT__(,) __VA_ARGS__) \
)
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);
format_attr(printf, 3, 4)
inline int snprintf_(char *restrict s, size_t size, const char *restrict fmt,
...);
@@ -36,35 +30,6 @@ inline int vsnprintf_(char *restrict s, size_t size, 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;
}
inline int
snprintf_(char *restrict s, size_t size, const char *restrict fmt, ...)
{

View File

@@ -1,25 +1,17 @@
/*
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2022-2024, 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 "string/sprintf/stpeprintf.h"
#include <stdarg.h>
#if !defined(HAVE_STPEPRINTF)
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
#endif

View File

@@ -1,32 +1,27 @@
/*
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STPEPRINTF_H_
#define SHADOW_INCLUDE_LIB_STPEPRINTF_H_
#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_STPEPRINTF_H_
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_STPEPRINTF_H_
#include <config.h>
#if !defined(HAVE_STPEPRINTF)
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include "attr.h"
#include "defines.h"
#if !defined(HAVE_STPEPRINTF)
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);
#endif
/*
@@ -79,6 +74,7 @@ inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
*/
#if !defined(HAVE_STPEPRINTF)
inline char *
stpeprintf(char *dst, char *end, const char *restrict fmt, ...)
{
@@ -91,8 +87,10 @@ stpeprintf(char *dst, char *end, const char *restrict fmt, ...)
return p;
}
#endif
#if !defined(HAVE_STPEPRINTF)
inline char *
vstpeprintf(char *dst, char *end, const char *restrict fmt, va_list ap)
{
@@ -114,7 +112,7 @@ vstpeprintf(char *dst, char *end, const char *restrict fmt, va_list ap)
return dst + len;
}
#endif
#endif // !HAVE_STPEPRINTF
#endif // include guard

View File

@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/sprintf/xasprintf.h"
#include <stdarg.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);

View File

@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
#include <config.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "attr.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

View File

@@ -1,20 +0,0 @@
/*
* 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

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/strchr/stpspn.h"

View File

@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STPSPN_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STPSPN_H_
#include <config.h>
#include <string.h>
#include "attr.h"
// Similar to strspn(3), but return a pointer instead of an offset.
// Similar to strchrnul(3), but search for any bytes not in 'accept'.
#define stpspn(s, accept) \
({ \
__auto_type s_ = s; \
\
s_ + strspn(s_, accept); \
})
#endif // include guard

View File

@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/strchr/strchrcnt.h"
#include <stddef.h>
extern inline size_t strchrcnt(const char *s, char c);

View File

@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRCNT_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRCNT_H_
#include <config.h>
#include <stddef.h>
#include "attr.h"
#include "string/strcmp/streq.h"
ATTR_STRING(1)
inline size_t strchrcnt(const char *s, char c);
inline size_t
strchrcnt(const char *s, char c)
{
size_t n = 0;
for (; !streq(s, ""); s++) {
if (*s == c)
n++;
}
return n;
}
#endif // include guard

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/strchr/strnul.h"

View File

@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRNUL_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRNUL_H_
#include <config.h>
#include <string.h>
#include "attr.h"
// Similar to strlen(3), but return a pointer instead of an offset.
#define strnul(s) \
({ \
__auto_type s_ = s; \
\
s_ + strlen(s_); \
})
#endif // include guard

View File

@@ -0,0 +1,10 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/strchr/strrspn.h"
extern inline char *strrspn(char *restrict s, const char *restrict accept);

View File

@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRRSPN_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRRSPN_H_
#include <config.h>
#include <string.h>
#include "attr.h"
#include "string/strchr/strnul.h"
ATTR_STRING(2)
inline char *strrspn(char *restrict s, const char *restrict accept);
// Available in Oracle Solaris: strrspn(3GEN).
// <https://docs.oracle.com/cd/E36784_01/html/E36877/strrspn-3gen.html>
inline char *
strrspn(char *restrict s, const char *restrict accept)
{
char *p;
p = strnul(s);
while (p > s) {
p--;
if (strchr(accept, *p) == NULL)
return p + 1;
}
return s;
}
#endif // include guard

12
lib/string/strcmp/streq.c Normal file
View File

@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include <stdbool.h>
#include "string/strcmp/streq.h"
extern inline bool streq(const char *s1, const char *s2);

30
lib/string/strcmp/streq.h Normal file
View File

@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STREQ_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STREQ_H_
#include <config.h>
#include <stdbool.h>
#include <string.h>
#include "attr.h"
ATTR_STRING(1)
ATTR_STRING(2)
inline bool streq(const char *s1, const char *s2);
/* Return true if s1 and s2 compare equal. */
inline bool
streq(const char *s1, const char *s2)
{
return strcmp(s1, s2) == 0;
}
#endif // include guard

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

View File

@@ -1,18 +1,13 @@
/*
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STPECPY_H_
#define SHADOW_INCLUDE_LIB_STPECPY_H_
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STPECPY_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STPECPY_H_
#include <config.h>
#if !defined(HAVE_STPECPY)
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
@@ -20,8 +15,10 @@
#include "attr.h"
#if !defined(HAVE_STPECPY)
ATTR_STRING(3)
inline char *stpecpy(char *dst, char *end, const char *restrict src);
#endif
/*
@@ -66,6 +63,7 @@ inline char *stpecpy(char *dst, char *end, const char *restrict src);
*/
#if !defined(HAVE_STPECPY)
inline char *
stpecpy(char *dst, char *end, const char *restrict src)
{
@@ -84,7 +82,7 @@ stpecpy(char *dst, char *end, const char *restrict src)
return stpcpy(mempcpy(dst, src, dlen), "") + trunc;
}
#endif
#endif // !HAVE_STPECPY
#endif // include guard

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/strncat.h"

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_STRNCAT_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STRNCAT_H_
#include <config.h>
#include <string.h>
#include "sizeof.h"
#define STRNCAT(dst, src) strncat(dst, src, NITEMS(src))
#endif // include guard

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"

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

View File

@@ -1,7 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <alx@kernel.org>
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
@@ -9,9 +7,7 @@
#include <stddef.h>
#include <sys/types.h>
#ident "$Id$"
#include "string/strtcpy.h"
#include "string/strcpy/strtcpy.h"
extern inline ssize_t strtcpy(char *restrict dst, const char *restrict src,

View File

@@ -1,11 +1,9 @@
/*
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRTCPY_H_
#define SHADOW_INCLUDE_LIB_STRTCPY_H_
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STRTCPY_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STRTCPY_H_
#include <config.h>
@@ -16,7 +14,6 @@
#include <sys/types.h>
#include "attr.h"
#include "defines.h"
#include "sizeof.h"

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/strdup/strndupa.h"

View File

@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_STRNDUPA_H_
#define SHADOW_INCLUDE_LIB_STRING_STRDUP_STRNDUPA_H_
#include <config.h>
#include <alloca.h>
#include <string.h>
#include "sizeof.h"
#include "string/strcpy/strncat.h"
// Similar to strndupa(3), but ensure that 's' is an array.
#define STRNDUPA(s) \
( \
STRNCAT(strcpy(alloca(strnlen(s, NITEMS(s)) + 1), ""), s) \
)
#endif // include guard

View File

@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh
// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
// SPDX-FileCopyrightText: 2003-2006, Tomasz Kłoczko
// SPDX-FileCopyrightText: 2008 , Nicolas François
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/strdup/xstrdup.h"
extern inline char *xstrdup(const char *str);

View File

@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh
// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
// SPDX-FileCopyrightText: 2003-2006, Tomasz Kłoczko
// SPDX-FileCopyrightText: 2008 , Nicolas François
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRDUP_H_
#define SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRDUP_H_
#include <config.h>
#include <string.h>
#include "alloc/x/xmalloc.h"
#include "attr.h"
ATTR_MALLOC(free)
inline char *xstrdup(const char *str);
inline char *
xstrdup(const char *str)
{
return strcpy(XMALLOC(strlen(str) + 1, char), str);
}
#endif // include guard

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/strdup/xstrndup.h"

View File

@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRNDUP_H_
#define SHADOW_INCLUDE_LIB_STRING_STRDUP_XSTRNDUP_H_
#include <config.h>
#include <string.h>
#include "alloc/x/xmalloc.h"
#include "sizeof.h"
#include "string/strcpy/strncat.h"
// Similar to strndup(3), but ensure that 's' is an array, and exit on ENOMEM.
#define XSTRNDUP(s) \
( \
STRNCAT(strcpy(XMALLOC(strnlen(s, NITEMS(s)) + 1, char), ""), s) \
)
#endif // include guard

View File

@@ -13,7 +13,7 @@
#include "sizeof.h"
#define STRFTIME(dst, fmt, ...) strftime(dst, NITEMS(dst), fmt, __VA_ARGS__)
#define STRFTIME(dst, fmt, tm) strftime(dst, NITEMS(dst), fmt, tm)
#endif // include guard

View File

@@ -1,21 +0,0 @@
/*
* 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

View File

@@ -0,0 +1,10 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/strtok/stpsep.h"
extern inline char *stpsep(char *s, const char *delim);

View File

@@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_STPSEP_H_
#define SHADOW_INCLUDE_LIB_STRING_STRTOK_STPSEP_H_
#include <config.h>
#include <string.h>
#include "attr.h"
ATTR_STRING(1) ATTR_STRING(2)
inline char *stpsep(char *s, const char *delim);
// Similar to strsep(3),
// but return the next token, and don't update the input pointer.
// Similar to strtok(3),
// but don't store a state, and don't skip empty fields.
inline char *
stpsep(char *s, const char *delim)
{
strsep(&s, delim);
return s;
}
#endif // include guard

View File

@@ -1,58 +0,0 @@
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_ZUSTR2STP_H_
#define SHADOW_INCLUDE_LIB_STRING_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