]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strtok/stpsep.[ch]: stpsep(): Add function
authorAlejandro Colomar <alx@kernel.org>
Tue, 2 Jul 2024 17:31:38 +0000 (19:31 +0200)
committerSerge Hallyn <serge@hallyn.com>
Tue, 9 Jul 2024 01:25:01 +0000 (20:25 -0500)
This function is somewhat simpler to use than strsep(3) in some cases.

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

index 8cbb6dfdb51f0d438414232a6cffd6629e687d1a..4e7c0b169f9c1e3fcb5aa767b295b226dd0971ab 100644 (file)
@@ -186,6 +186,8 @@ libshadow_la_SOURCES = \
        string/strdup/xstrndup.h \
        string/strftime.c \
        string/strftime.h \
+       string/strtok/stpsep.c \
+       string/strtok/stpsep.h \
        strtoday.c \
        sub.c \
        subordinateio.h \
diff --git a/lib/string/strtok/stpsep.c b/lib/string/strtok/stpsep.c
new file mode 100644 (file)
index 0000000..d7813d1
--- /dev/null
@@ -0,0 +1,10 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "string/strtok/stpsep.h"
+
+
+extern inline char *stpsep(char *s, const char *delim);
diff --git a/lib/string/strtok/stpsep.h b/lib/string/strtok/stpsep.h
new file mode 100644 (file)
index 0000000..f084bc4
--- /dev/null
@@ -0,0 +1,33 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_STPSEP_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRTOK_STPSEP_H_
+
+
+#include <config.h>
+
+#include <string.h>
+
+#include "attr.h"
+
+
+ATTR_STRING(1) ATTR_STRING(2)
+inline char *stpsep(char *s, const char *delim);
+
+
+// Similar to strsep(3),
+// but return the next token, and don't update the input pointer.
+// Similar to strtok(3),
+// but don't store a state, and don't skip empty fields.
+inline char *
+stpsep(char *s, const char *delim)
+{
+       strsep(&s, delim);
+
+       return s;
+}
+
+
+#endif  // include guard