xref: /freebsd/lib/libc/string/strtok.3 (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
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.\"
52.Dd November 27, 1998
53.Dt STRTOK 3
54.Os FreeBSD 3.0
55.Sh NAME
56.Nm strtok, strtok_r
57.Nd string tokens
58.Sh SYNOPSIS
59.Fd #include <string.h>
60.Ft char *
61.Fn strtok "char *str" "const char *sep"
62.Ft char *
63.Fn strtok_r "char *str" "const char *sep" "char **last"
64.Sh DESCRIPTION
65.Bf -symbolic
66This interface is obsoleted by strsep(3).
67.Ef
68.Pp
69The
70.Fn strtok
71function
72is used to isolate sequential tokens in a null-terminated string,
73.Fa str .
74These tokens are separated in the string by at least one of the
75characters in
76.Fa sep .
77The first time that
78.Fn strtok
79is called,
80.Fa str
81should be specified; subsequent calls, wishing to obtain further tokens
82from the same string, should pass a null pointer instead.
83The separator string,
84.Fa sep ,
85must be supplied each time, and may change between calls.
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.
94.Fn strtok_r
95may also be used to nest two parsing loops within one another, as
96long as seperate context pointers are used.
97.Pp
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 EXAMPLE
109The following uses
110.Fn strtok_r ()
111to parse two strings using separate contexts:
112.Bd -literal
113char test[80], blah[80];
114char *sep = "\\/:;=-";
115char *word, *phrase, *brkt, *brkb;
116
117strcpy(test, "This;is.a:test:of=the/string\\tokenizer-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\n", word, phrase);
130    }
131}
132.Ed
133.Sh SEE ALSO
134.Xr index 3 ,
135.Xr memchr 3 ,
136.Xr rindex 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.Sh STANDARDS
145The
146.Fn strtok
147function
148conforms to
149.St -ansiC .
150.Sh BUGS
151The System V
152.Fn strtok ,
153if handed a string containing only delimiter characters,
154will not alter the next starting point, so that a call to
155.Fn strtok
156with a different (or empty) delimiter string
157may return a
158.Pf non- Dv NULL
159value.
160Since this implementation always alters the next starting point,
161such a sequence of calls would always return
162.Dv NULL .
163.Sh AUTHOR
164Wes Peters, Softweyr LLC:
165.Xr <wes@softweyr.com>
166.br
167Based on the FreeBSD 3.0 implementation.
168