Skip to content

Commit 39da156

Browse files
alejandro-colomarhallyn
authored andcommitted
lib/string/strtok/stpsep.[ch]: stpsep(): Add function
This function is somewhat simpler to use than strsep(3) in some cases. Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 843c151 commit 39da156

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

lib/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ libshadow_la_SOURCES = \
186186
string/strdup/xstrndup.h \
187187
string/strftime.c \
188188
string/strftime.h \
189+
string/strtok/stpsep.c \
190+
string/strtok/stpsep.h \
189191
strtoday.c \
190192
sub.c \
191193
subordinateio.h \

lib/string/strtok/stpsep.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
5+
#include <config.h>
6+
7+
#include "string/strtok/stpsep.h"
8+
9+
10+
extern inline char *stpsep(char *s, const char *delim);

lib/string/strtok/stpsep.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
5+
#ifndef SHADOW_INCLUDE_LIB_STRING_STRTOK_STPSEP_H_
6+
#define SHADOW_INCLUDE_LIB_STRING_STRTOK_STPSEP_H_
7+
8+
9+
#include <config.h>
10+
11+
#include <string.h>
12+
13+
#include "attr.h"
14+
15+
16+
ATTR_STRING(1) ATTR_STRING(2)
17+
inline char *stpsep(char *s, const char *delim);
18+
19+
20+
// Similar to strsep(3),
21+
// but return the next token, and don't update the input pointer.
22+
// Similar to strtok(3),
23+
// but don't store a state, and don't skip empty fields.
24+
inline char *
25+
stpsep(char *s, const char *delim)
26+
{
27+
strsep(&s, delim);
28+
29+
return s;
30+
}
31+
32+
33+
#endif // include guard

0 commit comments

Comments
 (0)