]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strsep.3
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man3 / strsep.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
5fbde956 3.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
4.\"
5.\" References consulted:
6.\" Linux libc source code
7.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
8.\" 386BSD man pages
9.\" Modified Sat Jul 24 18:00:10 1993 by Rik Faith (faith@cs.unc.edu)
10.\" Modified Mon Jan 20 12:04:18 1997 by Andries Brouwer (aeb@cwi.nl)
11.\" Modified Tue Jan 23 20:23:07 2001 by Andries Brouwer (aeb@cwi.nl)
12.\"
45186a5d 13.TH STRSEP 3 2021-03-22 "Linux man-pages (unreleased)"
fea681da
MK
14.SH NAME
15strsep \- extract token from string
5e277835
AC
16.SH LIBRARY
17Standard C library
8fc3b2cf 18.RI ( libc ", " \-lc )
fea681da
MK
19.SH SYNOPSIS
20.nf
21.B #include <string.h>
68e4db0a 22.PP
2d91ea53 23.BI "char *strsep(char **restrict " stringp ", const char *restrict " delim );
fea681da 24.fi
68e4db0a 25.PP
d39ad78f 26.RS -4
cc4615cc
MK
27Feature Test Macro Requirements for glibc (see
28.BR feature_test_macros (7)):
d39ad78f 29.RE
68e4db0a 30.PP
cc4615cc 31.BR strsep ():
9d281e06 32.nf
51c612fb
MK
33 Since glibc 2.19:
34 _DEFAULT_SOURCE
35 Glibc 2.19 and earlier:
36 _BSD_SOURCE
9d281e06 37.fi
fea681da 38.SH DESCRIPTION
46d8df8e
MK
39If
40.I *stringp
41is NULL, the
60a90ecd
MK
42.BR strsep ()
43function returns NULL
c13182ef
MK
44and does nothing else.
45Otherwise, this function finds the first token
46d8df8e 46in the string
addd36ed 47.I *stringp
4d9fbd4e 48that is delimited by one of the bytes in the string
46d8df8e 49.IR delim .
71d9e7ae 50This token is terminated by overwriting the delimiter
d1a71985 51with a null byte (\(aq\e0\(aq),
46d8df8e
MK
52and
53.I *stringp
54is updated to point past the token.
fea681da 55In case no delimiter was found, the token is taken to be
46d8df8e
MK
56the entire string
57.IR *stringp ,
58and
59.I *stringp
60is made NULL.
47297adb 61.SH RETURN VALUE
60a90ecd
MK
62The
63.BR strsep ()
64function returns a pointer to the token,
46d8df8e
MK
65that is, it returns the original value of
66.IR *stringp .
178c91f4 67.SH ATTRIBUTES
0c5deb50
PH
68For an explanation of the terms used in this section, see
69.BR attributes (7).
c466875e
MK
70.ad l
71.nh
0c5deb50
PH
72.TS
73allbox;
c466875e 74lbx lb lb
0c5deb50
PH
75l l l.
76Interface Attribute Value
77T{
178c91f4 78.BR strsep ()
0c5deb50
PH
79T} Thread safety MT-Safe
80.TE
c466875e
MK
81.hy
82.ad
83.sp 1
3113c7f3 84.SH STANDARDS
44a2c328 854.4BSD.
fea681da 86.SH NOTES
60a90ecd
MK
87The
88.BR strsep ()
89function was introduced as a replacement for
90.BR strtok (3),
91since the latter cannot handle empty fields.
92However,
93.BR strtok (3)
94conforms to C89/C99 and hence is more portable.
fea681da 95.SH BUGS
d1a7f7d9
MK
96Be cautious when using this function.
97If you do use it, note that:
98.IP * 2
99This function modifies its first argument.
100.IP *
101This function cannot be used on constant strings.
102.IP *
103The identity of the delimiting character is lost.
395380d3 104.SH EXAMPLES
105The program below is a port of the one found in
106.BR strtok (3),
107which, however, doesn't discard multiple delimiters or empty tokens:
108.PP
109.in +4n
110.EX
111.RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
1121: a/bbb///cc
113 \-\-> a
114 \-\-> bbb
115 \-\->
116 \-\->
117 \-\-> cc
1182: xxx
119 \-\-> xxx
1203: yyy
121 \-\-> yyy
1224:
123 \-\->
124.EE
125.in
126.SS Program source
127\&
128.EX
129#include <stdio.h>
130#include <stdlib.h>
131#include <string.h>
132
133int
134main(int argc, char *argv[])
135{
136 char *token, *subtoken;
137
138 if (argc != 4) {
764b8fa9 139 fprintf(stderr, "Usage: %s string delim subdelim\en", argv[0]);
395380d3 140 exit(EXIT_FAILURE);
141 }
142
143 for (int j = 1; (token = strsep(&argv[1], argv[2])); j++) {
144 printf("%d: %s\en", j, token);
145
146 while ((subtoken = strsep(&token, argv[3])))
147 printf("\et \-\-> %s\en", subtoken);
148 }
149
150 exit(EXIT_SUCCESS);
151}
152.EE
47297adb 153.SH SEE ALSO
fea681da
MK
154.BR index (3),
155.BR memchr (3),
156.BR rindex (3),
157.BR strchr (3),
d095200e 158.BR string (3),
fea681da
MK
159.BR strpbrk (3),
160.BR strspn (3),
161.BR strstr (3),
162.BR strtok (3)