From: Alejandro Colomar Date: Sat, 7 Dec 2024 00:15:12 +0000 (+0100) Subject: lib/string/strtok/: astrsep2ls(): Add function X-Git-Tag: 4.18.0-rc1~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e168508b0019772b007954e3c8e9bb24dcffb2d8;p=thirdparty%2Fshadow.git lib/string/strtok/: astrsep2ls(): Add function Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 918e77d20..aafc9fd7c 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -243,6 +243,8 @@ libshadow_la_SOURCES = \ string/strspn/strrspn.h \ string/strtok/stpsep.c \ string/strtok/stpsep.h \ + string/strtok/astrsep2ls.c \ + string/strtok/astrsep2ls.h \ string/strtok/strsep2arr.c \ string/strtok/strsep2arr.h \ string/strtok/strsep2ls.c \ diff --git a/lib/string/strtok/astrsep2ls.c b/lib/string/strtok/astrsep2ls.c new file mode 100644 index 000000000..03d143242 --- /dev/null +++ b/lib/string/strtok/astrsep2ls.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strtok/astrsep2ls.h" + +#include + + +extern inline char **astrsep2ls(char *restrict s, const char *restrict delim, + size_t *restrict np); diff --git a/lib/string/strtok/astrsep2ls.h b/lib/string/strtok/astrsep2ls.h new file mode 100644 index 000000000..aa5d2f6ae --- /dev/null +++ b/lib/string/strtok/astrsep2ls.h @@ -0,0 +1,52 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_ASTRSEP2LS_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRTOK_ASTRSEP2LS_H_ + + +#include + +#include + +#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