]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strtok/: strsep2arr(), STRSEP2ARR(): Add APIs
authorAlejandro Colomar <alx@kernel.org>
Fri, 6 Dec 2024 20:15:10 +0000 (21:15 +0100)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Sat, 7 Jun 2025 14:52:03 +0000 (16:52 +0200)
This API set implements the usual loop around strsep(3).

This one implements a loop where we are interested in an exact number of
fields.  I'll add another API set, strsep2ls() and STRSEP2LS(), which
will add a NULL terminator, for arbitrary numbers of fields.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/strtok/strsep2arr.c [new file with mode: 0644]
lib/string/strtok/strsep2arr.h [new file with mode: 0644]

index 788862d63e45973de5de5e30441b7bb144b2bcb8..93e84f20954a075c630f0bbbab6ffa01da6cee6d 100644 (file)
@@ -243,6 +243,8 @@ libshadow_la_SOURCES = \
        string/strspn/strrspn.h \
        string/strtok/stpsep.c \
        string/strtok/stpsep.h \
+       string/strtok/strsep2arr.c \
+       string/strtok/strsep2arr.h \
        strtoday.c \
        sub.c \
        subordinateio.h \
diff --git a/lib/string/strtok/strsep2arr.c b/lib/string/strtok/strsep2arr.c
new file mode 100644 (file)
index 0000000..775eee2
--- /dev/null
@@ -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]);
diff --git a/lib/string/strtok/strsep2arr.h b/lib/string/strtok/strsep2arr.h
new file mode 100644 (file)
index 0000000..7170472
--- /dev/null
@@ -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