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