Sync lib/libc/string with FreeBSD:
[games.git] / lib / libc / string / strtok.3
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
65 This interface is obsoleted by
66 .Xr strsep 3 .
67 .Ef
68 .Pp
69 The
70 .Fn strtok
71 function
72 is used to isolate sequential tokens in a null-terminated string,
73 .Fa str .
74 These tokens are separated in the string by at least one of the
75 characters in
76 .Fa sep .
77 The first time that
78 .Fn strtok
79 is called,
80 .Fa str
81 should be specified; subsequent calls, wishing to obtain further tokens
82 from the same string, should pass a null pointer instead.
83 The separator string,
84 .Fa sep ,
85 must be supplied each time, and may change between calls.
86 .Pp
87 The implementation will behave as if no library function calls
88 .Fn strtok .
89 .Pp
90 The
91 .Fn strtok_r
92 function is a reentrant version of
93 .Fn strtok .
94 The context pointer
95 .Fa last
96 must be provided on each call.
97 The
98 .Fn strtok_r
99 function
100 may also be used to nest two parsing loops within one another, as
101 long as separate context pointers are used.
102 .Pp
103 The
104 .Fn strtok
105 and
106 .Fn strtok_r
107 functions
108 return a pointer to the beginning of each subsequent token in the string,
109 after replacing the token itself with a
110 .Dv NUL
111 character.
112 When no more tokens remain, a null pointer is returned.
113 .Sh EXAMPLES
114 The following uses
115 .Fn strtok_r
116 to parse two strings using separate contexts:
117 .Bd -literal
118 char test[80], blah[80];
119 char *sep = "\e\e/:;=-";
120 char *word, *phrase, *brkt, *brkb;
121
122 strcpy(test, "This;is.a:test:of=the/string\e\etokenizer-function.");
123
124 for (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
149 The
150 .Fn strtok
151 function
152 conforms to
153 .St -isoC .
154 .Sh AUTHORS
155 .An Wes Peters ,
156 Softweyr LLC:
157 .Aq wes@softweyr.com
158 .Pp
159 Based on the
160 .Fx 3.0
161 implementation.
162 .Sh BUGS
163 The System V
164 .Fn strtok ,
165 if handed a string containing only delimiter characters,
166 will not alter the next starting point, so that a call to
167 .Fn strtok
168 with a different (or empty) delimiter string
169 may return a
170 .Pf non- Dv NULL
171 value.
172 Since this implementation always alters the next starting point,
173 such a sequence of calls would always return
174 .Dv NULL .