xref: /freebsd/lib/libc/string/strtok.3 (revision 4b9d6057)
1.\" Copyright (c) 1998 Softweyr LLC.  All rights reserved.
2.\"
3.\" strtok_r, from Berkeley strtok
4.\" Oct 13, 1998 by Wes Peters <wes@softweyr.com>
5.\"
6.\" Copyright (c) 1988, 1991, 1993
7.\"	The Regents of the University of California.  All rights reserved.
8.\"
9.\" This code is derived from software contributed to Berkeley by
10.\" the American National Standards Committee X3, on Information
11.\" Processing Systems.
12.\"
13.\" Redistribution and use in source and binary forms, with or without
14.\" modification, are permitted provided that the following conditions
15.\" are met:
16.\"
17.\" 1. Redistributions of source code must retain the above copyright
18.\"    notices, this list of conditions and the following disclaimer.
19.\"
20.\" 2. Redistributions in binary form must reproduce the above
21.\"    copyright notices, this list of conditions and the following
22.\"    disclaimer in the documentation and/or other materials provided
23.\"    with the distribution.
24.\"
25.\" 3. Neither the name of Softweyr LLC, the University nor the names
26.\"    of its contributors may be used to endorse or promote products
27.\"    derived from this software without specific prior written
28.\"    permission.
29.\"
30.\" THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND
31.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
32.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34.\" DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE REGENTS, OR
35.\" CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
38.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
40.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
41.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42.\" SUCH DAMAGE.
43.\"
44.Dd January 22, 2016
45.Dt STRTOK 3
46.Os
47.Sh NAME
48.Nm strtok , strtok_r
49.Nd string tokens
50.Sh LIBRARY
51.Lb libc
52.Sh SYNOPSIS
53.In string.h
54.Ft char *
55.Fn strtok "char *str" "const char *sep"
56.Ft char *
57.Fn strtok_r "char *str" "const char *sep" "char **last"
58.Sh DESCRIPTION
59.Bf -symbolic
60This interface is obsoleted by
61.Xr strsep 3 .
62.Ef
63.Pp
64The
65.Fn strtok
66function
67is used to isolate sequential tokens in a null-terminated string,
68.Fa str .
69These tokens are separated in the string by at least one of the
70characters in
71.Fa sep .
72The first time that
73.Fn strtok
74is called,
75.Fa str
76should be specified; subsequent calls, wishing to obtain further tokens
77from the same string, should pass a null pointer instead.
78The separator string,
79.Fa sep ,
80must be supplied each time, and may change between calls.
81.Pp
82The implementation will behave as if no library function calls
83.Fn strtok .
84.Pp
85The
86.Fn strtok_r
87function is a reentrant version of
88.Fn strtok .
89The context pointer
90.Fa last
91must be provided on each call.
92The
93.Fn strtok_r
94function
95may also be used to nest two parsing loops within one another, as
96long as separate context pointers are used.
97.Sh RETURN VALUES
98The
99.Fn strtok
100and
101.Fn strtok_r
102functions
103return a pointer to the beginning of each subsequent token in the string,
104after replacing the token itself with a
105.Dv NUL
106character.
107When no more tokens remain, a null pointer is returned.
108.Sh EXAMPLES
109The following uses
110.Fn strtok_r
111to parse two strings using separate contexts:
112.Bd -literal
113char test[80], blah[80];
114char *sep = "\e\e/:;=-";
115char *word, *phrase, *brkt, *brkb;
116
117strcpy(test, "This;is.a:test:of=the/string\e\etokenizer-function.");
118
119for (word = strtok_r(test, sep, &brkt);
120     word;
121     word = strtok_r(NULL, sep, &brkt))
122{
123    strcpy(blah, "blah:blat:blab:blag");
124
125    for (phrase = strtok_r(blah, sep, &brkb);
126         phrase;
127         phrase = strtok_r(NULL, sep, &brkb))
128    {
129        printf("So far we're at %s:%s\en", word, phrase);
130    }
131}
132.Ed
133.Sh SEE ALSO
134.Xr memchr 3 ,
135.Xr strchr 3 ,
136.Xr strcspn 3 ,
137.Xr strpbrk 3 ,
138.Xr strrchr 3 ,
139.Xr strsep 3 ,
140.Xr strspn 3 ,
141.Xr strstr 3 ,
142.Xr wcstok 3
143.Sh STANDARDS
144The
145.Fn strtok
146function
147conforms to
148.St -isoC .
149The
150.Fn strtok_r
151function
152conforms to
153.St -p1003.1-2001 .
154.Sh AUTHORS
155.An Wes Peters Aq Mt wes@softweyr.com ,
156Softweyr LLC
157.Pp
158Based on the
159.Fx 3.0
160implementation.
161.Sh BUGS
162The System V
163.Fn strtok ,
164if handed a string containing only delimiter characters,
165will not alter the next starting point, so that a call to
166.Fn strtok
167with a different (or empty) delimiter string
168may return a
169.Pf non- Dv NULL
170value.
171Since this implementation always alters the next starting point,
172such a sequence of calls would always return
173.Dv NULL .
174