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