New upstream version 4.18.0
This commit is contained in:
12
lib/string/ctype/strchrisascii/strchriscntrl.c
Normal file
12
lib/string/ctype/strchrisascii/strchriscntrl.c
Normal file
@@ -0,0 +1,12 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/ctype/strchrisascii/strchriscntrl.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
extern inline bool strchriscntrl(const char *s);
|
||||
36
lib/string/ctype/strchrisascii/strchriscntrl.h
Normal file
36
lib/string/ctype/strchrisascii/strchriscntrl.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRCHRISASCII_STRCHRISCNTRL_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRCHRISASCII_STRCHRISCNTRL_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "string/strcmp/streq.h"
|
||||
|
||||
|
||||
inline bool strchriscntrl(const char *s);
|
||||
|
||||
|
||||
// string character is [:cntrl:]
|
||||
// Return true if any iscntrl(3) character is found in the string.
|
||||
inline bool
|
||||
strchriscntrl(const char *s)
|
||||
{
|
||||
for (; !streq(s, ""); s++) {
|
||||
unsigned char c = *s;
|
||||
|
||||
if (iscntrl(c))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
12
lib/string/ctype/strisascii/strisprint.c
Normal file
12
lib/string/ctype/strisascii/strisprint.c
Normal file
@@ -0,0 +1,12 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/ctype/strisascii/strisprint.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
extern inline bool strisprint(const char *s);
|
||||
39
lib/string/ctype/strisascii/strisprint.h
Normal file
39
lib/string/ctype/strisascii/strisprint.h
Normal file
@@ -0,0 +1,39 @@
|
||||
// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISASCII_STRISPRINT_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISASCII_STRISPRINT_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "string/strcmp/streq.h"
|
||||
|
||||
|
||||
inline bool strisprint(const char *s);
|
||||
|
||||
|
||||
// string is [:print:]
|
||||
// Like isprint(3), but check all characters in the string.
|
||||
inline bool
|
||||
strisprint(const char *s)
|
||||
{
|
||||
if (streq(s, ""))
|
||||
return false;
|
||||
|
||||
for (; !streq(s, ""); s++) {
|
||||
unsigned char c = *s;
|
||||
|
||||
if (!isprint(c))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
10
lib/string/ctype/strtoascii/strtolower.c
Normal file
10
lib/string/ctype/strtoascii/strtolower.c
Normal file
@@ -0,0 +1,10 @@
|
||||
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/ctype/strtoascii/strtolower.h"
|
||||
|
||||
|
||||
extern inline char *strtolower(char *str);
|
||||
34
lib/string/ctype/strtoascii/strtolower.h
Normal file
34
lib/string/ctype/strtoascii/strtolower.h
Normal file
@@ -0,0 +1,34 @@
|
||||
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRTOASCII_STRTOLOWER_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRTOASCII_STRTOLOWER_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#include "string/strcmp/streq.h"
|
||||
|
||||
|
||||
inline char *strtolower(char *str);
|
||||
|
||||
|
||||
// string convert-to lower-case
|
||||
// Like tolower(3), but convert all characters in the string.
|
||||
// Returns the input pointer.
|
||||
inline char *
|
||||
strtolower(char *str)
|
||||
{
|
||||
for (char *s = str; !streq(s, ""); s++) {
|
||||
unsigned char c = *s;
|
||||
|
||||
*s = tolower(c);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
13
lib/string/sprintf/aprintf.c
Normal file
13
lib/string/sprintf/aprintf.c
Normal file
@@ -0,0 +1,13 @@
|
||||
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/sprintf/aprintf.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
extern inline char *aprintf(const char *restrict fmt, ...);
|
||||
extern inline char *vaprintf(const char *restrict fmt, va_list ap);
|
||||
57
lib/string/sprintf/aprintf.h
Normal file
57
lib/string/sprintf/aprintf.h
Normal file
@@ -0,0 +1,57 @@
|
||||
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_APRINTF_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_APRINTF_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "attr.h"
|
||||
|
||||
|
||||
ATTR_MALLOC(free)
|
||||
format_attr(printf, 1, 2)
|
||||
inline char *aprintf(const char *restrict fmt, ...);
|
||||
|
||||
ATTR_MALLOC(free)
|
||||
format_attr(printf, 1, 0)
|
||||
inline char *vaprintf(const char *restrict fmt, va_list ap);
|
||||
|
||||
|
||||
// allocate print formatted
|
||||
// Like asprintf(3), but simpler; omit the length.
|
||||
inline char *
|
||||
aprintf(const char *restrict fmt, ...)
|
||||
{
|
||||
char *p;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
p = vaprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
// Like vasprintf(3), but simpler; omit the length.
|
||||
inline char *
|
||||
vaprintf(const char *restrict fmt, va_list ap)
|
||||
{
|
||||
char *p;
|
||||
|
||||
if (vasprintf(&p, fmt, ap) == -1)
|
||||
return NULL;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
#define SNPRINTF(s, fmt, ...) \
|
||||
( \
|
||||
snprintf_(s, NITEMS(s), fmt __VA_OPT__(,) __VA_ARGS__) \
|
||||
snprintf_(s, countof(s), fmt __VA_OPT__(,) __VA_ARGS__) \
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
|
||||
*
|
||||
* end Pointer to one after the last element of the buffer
|
||||
* pointed to by `dst`. Usually, it should be calculated
|
||||
* as `dst + NITEMS(dst)`.
|
||||
* as `dst + countof(dst)`.
|
||||
*
|
||||
* fmt Format string
|
||||
*
|
||||
|
||||
13
lib/string/sprintf/xaprintf.c
Normal file
13
lib/string/sprintf/xaprintf.c
Normal file
@@ -0,0 +1,13 @@
|
||||
// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/sprintf/xaprintf.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
extern inline char *xaprintf(const char *restrict fmt, ...);
|
||||
extern inline char *xvaprintf(const char *restrict fmt, va_list ap);
|
||||
59
lib/string/sprintf/xaprintf.h
Normal file
59
lib/string/sprintf/xaprintf.h
Normal file
@@ -0,0 +1,59 @@
|
||||
// SPDX-FileCopyrightText: 2023-2025, 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"
|
||||
#include "string/sprintf/aprintf.h"
|
||||
|
||||
|
||||
ATTR_MALLOC(free)
|
||||
format_attr(printf, 1, 2)
|
||||
inline char *xaprintf(const char *restrict fmt, ...);
|
||||
|
||||
ATTR_MALLOC(free)
|
||||
format_attr(printf, 1, 0)
|
||||
inline char *xvaprintf(const char *restrict fmt, va_list ap);
|
||||
|
||||
|
||||
// exit-on-error allocate print formatted
|
||||
inline char *
|
||||
xaprintf(const char *restrict fmt, ...)
|
||||
{
|
||||
char *p;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
p = xvaprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
inline char *
|
||||
xvaprintf(const char *restrict fmt, va_list ap)
|
||||
{
|
||||
char *p;
|
||||
|
||||
p = vaprintf(fmt, ap);
|
||||
if (p == NULL) {
|
||||
perror("vaprintf");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
@@ -1,14 +0,0 @@
|
||||
// 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);
|
||||
@@ -1,54 +0,0 @@
|
||||
// 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
|
||||
10
lib/string/strcmp/strcaseprefix.c
Normal file
10
lib/string/strcmp/strcaseprefix.c
Normal file
@@ -0,0 +1,10 @@
|
||||
// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/strcmp/strcaseprefix.h"
|
||||
|
||||
|
||||
extern inline const char *strcaseprefix_(const char *s, const char *prefix);
|
||||
49
lib/string/strcmp/strcaseprefix.h
Normal file
49
lib/string/strcmp/strcaseprefix.h
Normal file
@@ -0,0 +1,49 @@
|
||||
// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STRCASEPREFIX_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STRCASEPREFIX_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "attr.h"
|
||||
#include "cast.h"
|
||||
|
||||
|
||||
// string case-insensitive prefix
|
||||
#define strcaseprefix(s, prefix) \
|
||||
({ \
|
||||
const char *p_; \
|
||||
\
|
||||
p_ = strcaseprefix_(s, prefix); \
|
||||
\
|
||||
_Generic(s, \
|
||||
const char *: p_, \
|
||||
const void *: p_, \
|
||||
char *: const_cast(char *, p_), \
|
||||
void *: const_cast(char *, p_) \
|
||||
); \
|
||||
})
|
||||
|
||||
|
||||
ATTR_STRING(1) ATTR_STRING(2)
|
||||
inline const char *strcaseprefix_(const char *s, const char *prefix);
|
||||
|
||||
|
||||
// strprefix_(), but case-insensitive.
|
||||
inline const char *
|
||||
strcaseprefix_(const char *s, const char *prefix)
|
||||
{
|
||||
if (strncasecmp(s, prefix, strlen(prefix)) != 0)
|
||||
return NULL;
|
||||
|
||||
return s + strlen(prefix);
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
10
lib/string/strcmp/strprefix.c
Normal file
10
lib/string/strcmp/strprefix.c
Normal file
@@ -0,0 +1,10 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/strcmp/strprefix.h"
|
||||
|
||||
|
||||
extern inline const char *strprefix_(const char *s, const char *prefix);
|
||||
53
lib/string/strcmp/strprefix.h
Normal file
53
lib/string/strcmp/strprefix.h
Normal file
@@ -0,0 +1,53 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STRPREFIX_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STRPREFIX_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "attr.h"
|
||||
#include "cast.h"
|
||||
|
||||
|
||||
// string prefix
|
||||
#define strprefix(s, prefix) \
|
||||
({ \
|
||||
const char *p_; \
|
||||
\
|
||||
p_ = strprefix_(s, prefix); \
|
||||
\
|
||||
_Generic(s, \
|
||||
const char *: p_, \
|
||||
const void *: p_, \
|
||||
char *: const_cast(char *, p_), \
|
||||
void *: const_cast(char *, p_) \
|
||||
); \
|
||||
})
|
||||
|
||||
|
||||
ATTR_STRING(1)
|
||||
ATTR_STRING(2)
|
||||
inline const char *strprefix_(const char *s, const char *prefix);
|
||||
|
||||
|
||||
/*
|
||||
* Return NULL if s does not start with prefix.
|
||||
* Return `s + strlen(prefix)` if s starts with prefix.
|
||||
*/
|
||||
inline const char *
|
||||
strprefix_(const char *s, const char *prefix)
|
||||
{
|
||||
if (strncmp(s, prefix, strlen(prefix)) != 0)
|
||||
return NULL;
|
||||
|
||||
return s + strlen(prefix);
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
@@ -32,7 +32,7 @@ inline char *stpecpy(char *dst, char *end, const char *restrict src);
|
||||
*
|
||||
* end Pointer to one after the last element of the buffer
|
||||
* pointed to by `dst`. Usually, it should be calculated
|
||||
* as `dst + NITEMS(dst)`.
|
||||
* as `dst + countof(dst)`.
|
||||
*
|
||||
* src Source string to be copied into dst.
|
||||
*
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "sizeof.h"
|
||||
|
||||
|
||||
#define STRNCAT(dst, src) strncat(dst, src, NITEMS(src))
|
||||
#define STRNCAT(dst, src) strncat(dst, src, countof(src))
|
||||
|
||||
|
||||
#endif // include guard
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "sizeof.h"
|
||||
|
||||
|
||||
#define STRNCPY(dst, src) strncpy(dst, src, NITEMS(dst))
|
||||
#define STRNCPY(dst, src) strncpy(dst, src, countof(dst))
|
||||
|
||||
|
||||
#endif // include guard
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
* 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().
|
||||
* countof().
|
||||
*
|
||||
* RETURN VALUE
|
||||
* -1 If this call truncated the resulting string.
|
||||
@@ -44,7 +44,7 @@
|
||||
*/
|
||||
|
||||
|
||||
#define STRTCPY(dst, src) strtcpy(dst, src, NITEMS(dst))
|
||||
#define STRTCPY(dst, src) strtcpy(dst, src, countof(dst))
|
||||
|
||||
|
||||
ATTR_STRING(2)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
@@ -12,14 +12,15 @@
|
||||
#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) \
|
||||
)
|
||||
// string n-bounded-read duplicate using-alloca(3)
|
||||
#ifndef strndupa
|
||||
# define strndupa(s, n) strncat(strcpy(alloca(n + 1), ""), s, n)
|
||||
#endif
|
||||
|
||||
|
||||
#define STRNDUPA(s) strndupa(s, countof(s))
|
||||
|
||||
|
||||
#endif // include guard
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
// 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) \
|
||||
STRNCAT(strcpy(XMALLOC(strnlen(s, countof(s)) + 1, char), ""), s) \
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
|
||||
// string format time
|
||||
#define STRFTIME(dst, fmt, tm) strftime(dst, NITEMS(dst), fmt, tm)
|
||||
#define STRFTIME(dst, fmt, tm) strftime(dst, countof(dst), fmt, tm)
|
||||
|
||||
|
||||
#endif // include guard
|
||||
|
||||
13
lib/string/strtok/astrsep2ls.c
Normal file
13
lib/string/strtok/astrsep2ls.c
Normal file
@@ -0,0 +1,13 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/strtok/astrsep2ls.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
extern inline char **astrsep2ls(char *restrict s, const char *restrict delim,
|
||||
size_t *restrict np);
|
||||
52
lib/string/strtok/astrsep2ls.h
Normal file
52
lib/string/strtok/astrsep2ls.h
Normal file
@@ -0,0 +1,52 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_ASTRSEP2LS_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_STRTOK_ASTRSEP2LS_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "alloc/malloc.h"
|
||||
#include "attr.h"
|
||||
#include "string/strchr/strchrscnt.h"
|
||||
#include "string/strtok/strsep2ls.h"
|
||||
|
||||
|
||||
ATTR_ACCESS(read_write, 1) ATTR_ACCESS(write_only, 3)
|
||||
ATTR_STRING(1) ATTR_STRING(2)
|
||||
inline char **astrsep2ls(char *restrict s, const char *restrict delim,
|
||||
size_t *restrict np);
|
||||
|
||||
|
||||
// allocate string separate to list-of-strings
|
||||
// Like strsep2ls(), but allocate the list array.
|
||||
inline char **
|
||||
astrsep2ls(char *s, const char *restrict delim, size_t *restrict np)
|
||||
{
|
||||
char **ls;
|
||||
ssize_t n;
|
||||
|
||||
n = strchrscnt(s, delim) + 2;
|
||||
|
||||
ls = MALLOC(n, char *);
|
||||
if (ls == NULL)
|
||||
return NULL;
|
||||
|
||||
n = strsep2ls(s, delim, n, ls);
|
||||
if (n == -1) {
|
||||
free(ls);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (np != NULL)
|
||||
*np = n;
|
||||
|
||||
return ls;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
14
lib/string/strtok/strsep2arr.c
Normal file
14
lib/string/strtok/strsep2arr.c
Normal file
@@ -0,0 +1,14 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/strtok/strsep2arr.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
extern inline ssize_t strsep2arr(char *s, const char *restrict delim,
|
||||
size_t n, char *a[restrict n]);
|
||||
52
lib/string/strtok/strsep2arr.h
Normal file
52
lib/string/strtok/strsep2arr.h
Normal file
@@ -0,0 +1,52 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_STRSEP2ARR_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_STRTOK_STRSEP2ARR_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "attr.h"
|
||||
#include "sizeof.h"
|
||||
|
||||
|
||||
#define STRSEP2ARR(s, delim, a) \
|
||||
( \
|
||||
strsep2arr(s, delim, countof(a), a) == countof(a) ? 0 : -1 \
|
||||
)
|
||||
|
||||
|
||||
ATTR_ACCESS(read_write, 1) ATTR_ACCESS(write_only, 4, 3)
|
||||
ATTR_STRING(1) ATTR_STRING(2)
|
||||
inline ssize_t strsep2arr(char *s, const char *restrict delim,
|
||||
size_t n, char *a[restrict n]);
|
||||
|
||||
|
||||
// string separate to array-of-strings
|
||||
// strsep(3) a string into an array of strings.
|
||||
// Return the number of fields in the string, or -1 on error.
|
||||
inline ssize_t
|
||||
strsep2arr(char *s, const char *restrict delim, size_t n, char *a[restrict n])
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < n && s != NULL; i++)
|
||||
a[i] = strsep(&s, delim);
|
||||
|
||||
if (s != NULL) {
|
||||
errno = E2BIG;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
14
lib/string/strtok/strsep2ls.c
Normal file
14
lib/string/strtok/strsep2ls.c
Normal file
@@ -0,0 +1,14 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/strtok/strsep2ls.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
extern inline ssize_t strsep2ls(char *s, const char *restrict delim,
|
||||
size_t n, char *ls[restrict n]);
|
||||
48
lib/string/strtok/strsep2ls.h
Normal file
48
lib/string/strtok/strsep2ls.h
Normal file
@@ -0,0 +1,48 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_STRSEP2LS_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_STRTOK_STRSEP2LS_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "attr.h"
|
||||
#include "sizeof.h"
|
||||
#include "string/strtok/strsep2arr.h"
|
||||
|
||||
|
||||
#define STRSEP2LS(s, delim, ls) strsep2ls(s, delim, countof(ls), ls)
|
||||
|
||||
|
||||
ATTR_ACCESS(read_write, 1) ATTR_ACCESS(write_only, 4, 3)
|
||||
ATTR_STRING(1) ATTR_STRING(2)
|
||||
inline ssize_t strsep2ls(char *s, const char *restrict delim,
|
||||
size_t n, char *ls[restrict n]);
|
||||
|
||||
|
||||
// string separate to list-of-strings
|
||||
// Like strsep2arr(), but add a NULL terminator.
|
||||
inline ssize_t
|
||||
strsep2ls(char *s, const char *restrict delim, size_t n, char *ls[restrict n])
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = strsep2arr(s, delim, n, ls);
|
||||
if (i >= n) {
|
||||
errno = E2BIG;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ls[i] = NULL;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
13
lib/string/strtok/xastrsep2ls.c
Normal file
13
lib/string/strtok/xastrsep2ls.c
Normal file
@@ -0,0 +1,13 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "string/strtok/xastrsep2ls.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
extern inline char **xastrsep2ls(char *restrict s, const char *restrict delim,
|
||||
size_t *restrict np);
|
||||
46
lib/string/strtok/xastrsep2ls.h
Normal file
46
lib/string/strtok/xastrsep2ls.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_XASTRSEP2LS_H_
|
||||
#define SHADOW_INCLUDE_LIB_STRING_STRTOK_XASTRSEP2LS_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "attr.h"
|
||||
#include "shadowlog.h"
|
||||
#include "string/strtok/astrsep2ls.h"
|
||||
|
||||
|
||||
ATTR_ACCESS(read_write, 1) ATTR_ACCESS(write_only, 3)
|
||||
ATTR_STRING(1) ATTR_STRING(2)
|
||||
inline char **xastrsep2ls(char *restrict s, const char *restrict delim,
|
||||
size_t *restrict np);
|
||||
|
||||
|
||||
// exit-on-error allocate string separate to list-of-strings
|
||||
inline char **
|
||||
xastrsep2ls(char *s, const char *restrict delim, size_t *restrict np)
|
||||
{
|
||||
char **ls;
|
||||
|
||||
ls = astrsep2ls(s, delim, np);
|
||||
if (ls == NULL)
|
||||
goto x;
|
||||
|
||||
return ls;
|
||||
x:
|
||||
fprintf(log_get_logfd(), "%s: %s\n",
|
||||
log_get_progname(), strerror(errno));
|
||||
exit(13);
|
||||
}
|
||||
|
||||
|
||||
#endif // include guard
|
||||
Reference in New Issue
Block a user