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