File: strsep.3

package info (click to toggle)
manpages 6.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,184 kB
  • sloc: sh: 575; python: 222; perl: 190; makefile: 29; lisp: 22
file content (151 lines) | stat: -rw-r--r-- 2,999 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'\" t
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH strsep 3 2025-05-17 "Linux man-pages (unreleased)"
.SH NAME
strsep \- extract token from string
.SH LIBRARY
Standard C library
.RI ( libc ,\~ \-lc )
.SH SYNOPSIS
.nf
.B #include <string.h>
.P
.BI "char *strsep(char **restrict " stringp ", const char *restrict " delim );
.fi
.P
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.P
.BR strsep ():
.nf
    Since glibc 2.19:
        _DEFAULT_SOURCE
    glibc 2.19 and earlier:
        _BSD_SOURCE
.fi
.SH DESCRIPTION
If
.I *stringp
is NULL, the
.BR strsep ()
function returns NULL
and does nothing else.
Otherwise, this function finds the first token
in the string
.I *stringp
that is delimited by one of the bytes in the string
.IR delim .
This token is terminated by overwriting the delimiter
with a null byte (\[aq]\[rs]0\[aq]),
and
.I *stringp
is updated to point past the token.
In case no delimiter was found, the token is taken to be
the entire string
.IR *stringp ,
and
.I *stringp
is made NULL.
.SH RETURN VALUE
The
.BR strsep ()
function returns a pointer to the token,
that is, it returns the original value of
.IR *stringp .
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.na
.nh
.BR strsep ()
T}	Thread safety	MT-Safe
.TE
.SH STANDARDS
BSD.
.SH HISTORY
4.4BSD.
.P
The
.BR strsep ()
function was introduced as a replacement for
.BR strtok (3),
since the latter cannot handle empty fields.
.SH CAVEATS
Be cautious when using this function.
If you do use it, note that:
.IP \[bu] 3
This function modifies its first argument.
.IP \[bu]
This function cannot be used on constant strings.
.IP \[bu]
The identity of the delimiting character is lost.
.SH EXAMPLES
The program below is a port of the one found in
.BR strtok (3),
which, however, doesn't discard multiple delimiters or empty tokens:
.P
.in +4n
.EX
.RB "$" " ./a.out \[aq]a/bbb///cc;xxx:yyy:\[aq] \[aq]:;\[aq] \[aq]/\[aq]"
1: a/bbb///cc
         \-\-> a
         \-\-> bbb
         \-\->
         \-\->
         \-\-> cc
2: xxx
         \-\-> xxx
3: yyy
         \-\-> yyy
4:
         \-\->
.EE
.in
.SS Program source
\&
.\" SRC BEGIN (strsep.c)
.EX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
\&
int
main(int argc, char *argv[])
{
    char *token, *subtoken;
\&
    if (argc != 4) {
        fprintf(stderr, "Usage: %s string delim subdelim\[rs]n", argv[0]);
        exit(EXIT_FAILURE);
    }
\&
    for (unsigned int j = 1; (token = strsep(&argv[1], argv[2])); j++) {
        printf("%u: %s\[rs]n", j, token);
\&
        while ((subtoken = strsep(&token, argv[3])))
            printf("\[rs]t \-\-> %s\[rs]n", subtoken);
    }
\&
    exit(EXIT_SUCCESS);
}
.EE
.\" SRC END
.SH SEE ALSO
.BR memchr (3),
.BR strchr (3),
.BR string (3),
.BR strpbrk (3),
.BR strspn (3),
.BR strstr (3),
.BR strtok (3)