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