xref: /freebsd/lib/libc/string/strtok.3 (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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. All advertising materials mentioning features or use of this
26.\"    software must display the following acknowledgement:
27.\"
28.\"	This product includes software developed by Softweyr LLC, the
29.\"      University of California, Berkeley, and its contributors.
30.\"
31.\" 4. Neither the name of Softweyr LLC, the University nor the names
32.\"    of its contributors may be used to endorse or promote products
33.\"    derived from this software without specific prior written
34.\"    permission.
35.\"
36.\" THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND
37.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
38.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
39.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40.\" DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE REGENTS, OR
41.\" CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48.\" SUCH DAMAGE.
49.\"
50.\"     @(#)strtok.3	8.2 (Berkeley) 2/3/94
51.\" $FreeBSD$
52.\"
53.Dd November 27, 1998
54.Dt STRTOK 3
55.Os FreeBSD 3.0
56.Sh NAME
57.Nm strtok, strtok_r
58.Nd string tokens
59.Sh SYNOPSIS
60.Fd #include <string.h>
61.Ft char *
62.Fn strtok "char *str" "const char *sep"
63.Ft char *
64.Fn strtok_r "char *str" "const char *sep" "char **last"
65.Sh DESCRIPTION
66.Bf -symbolic
67This interface is obsoleted by strsep(3).
68.Ef
69.Pp
70The
71.Fn strtok
72function
73is used to isolate sequential tokens in a null-terminated string,
74.Fa str .
75These tokens are separated in the string by at least one of the
76characters in
77.Fa sep .
78The first time that
79.Fn strtok
80is called,
81.Fa str
82should be specified; subsequent calls, wishing to obtain further tokens
83from the same string, should pass a null pointer instead.
84The separator string,
85.Fa sep ,
86must be supplied each time, and may change between calls.
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.
95.Fn strtok_r
96may also be used to nest two parsing loops within one another, as
97long as separate context pointers are used.
98.Pp
99The
100.Fn strtok
101and
102.Fn strtok_r
103functions
104return a pointer to the beginning of each subsequent token in the string,
105after replacing the token itself with a
106.Dv NUL
107character.
108When no more tokens remain, a null pointer is returned.
109.Sh EXAMPLE
110The following uses
111.Fn strtok_r
112to parse two strings using separate contexts:
113.Bd -literal
114char test[80], blah[80];
115char *sep = "\e\e/:;=-";
116char *word, *phrase, *brkt, *brkb;
117
118strcpy(test, "This;is.a:test:of=the/string\e\etokenizer-function.");
119
120for (word = strtok_r(test, sep, &brkt);
121     word;
122     word = strtok_r(NULL, sep, &brkt))
123{
124    strcpy(blah, "blah:blat:blab:blag");
125
126    for (phrase = strtok_r(blah, sep, &brkb);
127         phrase;
128         phrase = strtok_r(NULL, sep, &brkb))
129    {
130        printf("So far we're at %s:%s\en", word, phrase);
131    }
132}
133.Ed
134.Sh SEE ALSO
135.Xr index 3 ,
136.Xr memchr 3 ,
137.Xr rindex 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.Sh STANDARDS
146The
147.Fn strtok
148function
149conforms to
150.St -ansiC .
151.Sh BUGS
152The System V
153.Fn strtok ,
154if handed a string containing only delimiter characters,
155will not alter the next starting point, so that a call to
156.Fn strtok
157with a different (or empty) delimiter string
158may return a
159.Pf non- Dv NULL
160value.
161Since this implementation always alters the next starting point,
162such a sequence of calls would always return
163.Dv NULL .
164.Sh AUTHORS
165.An Wes Peters, Softweyr LLC: Aq wes@softweyr.com
166.br
167Based on the
168.Fx 3.0
169implementation.
170