xref: /freebsd/lib/libc/string/strtok.3 (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
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.\"     @(#)strtok.3	8.2 (Berkeley) 2/3/94
45.\"
46.Dd January 22, 2016
47.Dt STRTOK 3
48.Os
49.Sh NAME
50.Nm strtok , strtok_r
51.Nd string tokens
52.Sh LIBRARY
53.Lb libc
54.Sh SYNOPSIS
55.In string.h
56.Ft char *
57.Fn strtok "char *str" "const char *sep"
58.Ft char *
59.Fn strtok_r "char *str" "const char *sep" "char **last"
60.Sh DESCRIPTION
61.Bf -symbolic
62This interface is obsoleted by
63.Xr strsep 3 .
64.Ef
65.Pp
66The
67.Fn strtok
68function
69is used to isolate sequential tokens in a null-terminated string,
70.Fa str .
71These tokens are separated in the string by at least one of the
72characters in
73.Fa sep .
74The first time that
75.Fn strtok
76is called,
77.Fa str
78should be specified; subsequent calls, wishing to obtain further tokens
79from the same string, should pass a null pointer instead.
80The separator string,
81.Fa sep ,
82must be supplied each time, and may change between calls.
83.Pp
84The implementation will behave as if no library function calls
85.Fn strtok .
86.Pp
87The
88.Fn strtok_r
89function is a reentrant version of
90.Fn strtok .
91The context pointer
92.Fa last
93must be provided on each call.
94The
95.Fn strtok_r
96function
97may also be used to nest two parsing loops within one another, as
98long as separate context pointers are used.
99.Sh RETURN VALUES
100The
101.Fn strtok
102and
103.Fn strtok_r
104functions
105return a pointer to the beginning of each subsequent token in the string,
106after replacing the token itself with a
107.Dv NUL
108character.
109When no more tokens remain, a null pointer is returned.
110.Sh EXAMPLES
111The following uses
112.Fn strtok_r
113to parse two strings using separate contexts:
114.Bd -literal
115char test[80], blah[80];
116char *sep = "\e\e/:;=-";
117char *word, *phrase, *brkt, *brkb;
118
119strcpy(test, "This;is.a:test:of=the/string\e\etokenizer-function.");
120
121for (word = strtok_r(test, sep, &brkt);
122     word;
123     word = strtok_r(NULL, sep, &brkt))
124{
125    strcpy(blah, "blah:blat:blab:blag");
126
127    for (phrase = strtok_r(blah, sep, &brkb);
128         phrase;
129         phrase = strtok_r(NULL, sep, &brkb))
130    {
131        printf("So far we're at %s:%s\en", word, phrase);
132    }
133}
134.Ed
135.Sh SEE ALSO
136.Xr memchr 3 ,
137.Xr strchr 3 ,
138.Xr strcspn 3 ,
139.Xr strpbrk 3 ,
140.Xr strrchr 3 ,
141.Xr strsep 3 ,
142.Xr strspn 3 ,
143.Xr strstr 3 ,
144.Xr wcstok 3
145.Sh STANDARDS
146The
147.Fn strtok
148function
149conforms to
150.St -isoC .
151The
152.Fn strtok_r
153function
154conforms to
155.St -p1003.1-2001 .
156.Sh AUTHORS
157.An Wes Peters Aq Mt wes@softweyr.com ,
158Softweyr LLC
159.Pp
160Based on the
161.Fx 3.0
162implementation.
163.Sh BUGS
164The System V
165.Fn strtok ,
166if handed a string containing only delimiter characters,
167will not alter the next starting point, so that a call to
168.Fn strtok
169with a different (or empty) delimiter string
170may return a
171.Pf non- Dv NULL
172value.
173Since this implementation always alters the next starting point,
174such a sequence of calls would always return
175.Dv NULL .
176