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