]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strtok/: astrsep2ls(): Add function
authorAlejandro Colomar <alx@kernel.org>
Sat, 7 Dec 2024 00:15:12 +0000 (01:15 +0100)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Sat, 7 Jun 2025 14:52:03 +0000 (16:52 +0200)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/strtok/astrsep2ls.c [new file with mode: 0644]
lib/string/strtok/astrsep2ls.h [new file with mode: 0644]

index 918e77d206bb9b0adaff0950bf33249d192dad40..aafc9fd7cc2b735968bc9558b92397deecd83673 100644 (file)
@@ -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 (file)
index 0000000..03d1432
--- /dev/null
@@ -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);
diff --git a/lib/string/strtok/astrsep2ls.h b/lib/string/strtok/astrsep2ls.h
new file mode 100644 (file)
index 0000000..aa5d2f6
--- /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_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