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