xref: /freebsd/lib/libc/string/strsep.3 (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.Dd May 28, 2018
32.Dt STRSEP 3
33.Os
34.Sh NAME
35.Nm strsep
36.Nd separate strings
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In string.h
41.Ft char *
42.Fn strsep "char **stringp" "const char *delim"
43.Sh DESCRIPTION
44The
45.Fn strsep
46function locates, in the string referenced by
47.Fa *stringp ,
48the first occurrence of any character in the string
49.Fa delim
50(or the terminating
51.Ql \e0
52character) and replaces it with a
53.Ql \e0 .
54The location of the next character after the delimiter character
55(or NULL, if the end of the string was reached) is stored in
56.Fa *stringp .
57The original value of
58.Fa *stringp
59is returned.
60.Pp
61An
62.Dq empty
63field (i.e., a character in the string
64.Fa delim
65occurs as the first character of
66.Fa *stringp )
67can be detected by comparing the location referenced by the returned pointer
68to
69.Ql \e0 .
70.Pp
71If
72.Fa *stringp
73is initially
74.Dv NULL ,
75.Fn strsep
76returns
77.Dv NULL .
78.Sh EXAMPLES
79The following uses
80.Fn strsep
81to parse a string, and prints each token in separate line:
82.Bd -literal -offset indent
83char *token, *string, *tofree;
84
85tofree = string = strdup("abc,def,ghi");
86if (string == NULL)
87	err(1, "strdup");
88while ((token = strsep(&string, ",")) != NULL)
89	printf("%s\en", token);
90
91free(tofree);
92.Ed
93.Pp
94The following uses
95.Fn strsep
96to parse a string, containing tokens delimited by white space, into an
97argument vector:
98.Bd -literal -offset indent
99char **ap, *argv[10], *inputstring;
100
101for (ap = argv; (*ap = strsep(&inputstring, " \et")) != NULL;)
102	if (**ap != '\e0')
103		if (++ap >= &argv[10])
104			break;
105.Ed
106.Sh SEE ALSO
107.Xr memchr 3 ,
108.Xr strchr 3 ,
109.Xr strcspn 3 ,
110.Xr strpbrk 3 ,
111.Xr strrchr 3 ,
112.Xr strspn 3 ,
113.Xr strstr 3 ,
114.Xr strtok 3
115.Sh HISTORY
116The
117.Fn strsep
118function
119is intended as a replacement for the
120.Fn strtok
121function.
122While the
123.Fn strtok
124function should be preferred for portability reasons (it conforms to
125.St -isoC )
126it is unable to handle empty fields, i.e., detect fields delimited by
127two adjacent delimiter characters, or to be used for more than a single
128string at a time.
129The
130.Fn strsep
131function first appeared in
132.Bx 4.4 .
133