From: Alejandro Colomar Date: Fri, 6 Dec 2024 21:00:43 +0000 (+0100) Subject: lib/string/strtok/: strsep2ls(), STRSEP2LS(): Add APIs X-Git-Tag: 4.18.0-rc1~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2af9f9d012d3d97430a5f7fa20ab77ea2cb21ed5;p=thirdparty%2Fshadow.git lib/string/strtok/: strsep2ls(), STRSEP2LS(): Add APIs This API set implements another usual loop around strsep(3). This one implements a loop where we are interested in an arbitrary number of fields. For that, a NULL terminator is added at the end. That is commonly referred to as "list". Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 93e84f209..918e77d20 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -245,6 +245,8 @@ libshadow_la_SOURCES = \ string/strtok/stpsep.h \ string/strtok/strsep2arr.c \ string/strtok/strsep2arr.h \ + string/strtok/strsep2ls.c \ + string/strtok/strsep2ls.h \ strtoday.c \ sub.c \ subordinateio.h \ diff --git a/lib/string/strtok/strsep2ls.c b/lib/string/strtok/strsep2ls.c new file mode 100644 index 000000000..b7eed1809 --- /dev/null +++ b/lib/string/strtok/strsep2ls.c @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strtok/strsep2ls.h" + +#include +#include + + +extern inline ssize_t strsep2ls(char *s, const char *restrict delim, + size_t n, char *ls[restrict n]); diff --git a/lib/string/strtok/strsep2ls.h b/lib/string/strtok/strsep2ls.h new file mode 100644 index 000000000..473fe58c6 --- /dev/null +++ b/lib/string/strtok/strsep2ls.h @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_STRSEP2LS_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRTOK_STRSEP2LS_H_ + + +#include + +#include +#include +#include + +#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