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