xref: /freebsd/lib/libc/string/strtok.3 (revision 25bb73e063c17cd9048cf60100dbc0ac5177e94a)
1b1e12513SWes Peters.\" Copyright (c) 1998 Softweyr LLC.  All rights reserved.
2b1e12513SWes Peters.\"
3b1e12513SWes Peters.\" strtok_r, from Berkeley strtok
4b1e12513SWes Peters.\" Oct 13, 1998 by Wes Peters <wes@softweyr.com>
5b1e12513SWes Peters.\"
658f0484fSRodney W. Grimes.\" Copyright (c) 1988, 1991, 1993
758f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
858f0484fSRodney W. Grimes.\"
958f0484fSRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
1058f0484fSRodney W. Grimes.\" the American National Standards Committee X3, on Information
1158f0484fSRodney W. Grimes.\" Processing Systems.
1258f0484fSRodney W. Grimes.\"
1358f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
1458f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
1558f0484fSRodney W. Grimes.\" are met:
1658f0484fSRodney W. Grimes.\"
17b1e12513SWes Peters.\" 1. Redistributions of source code must retain the above copyright
18b1e12513SWes Peters.\"    notices, this list of conditions and the following disclaimer.
19b1e12513SWes Peters.\"
20b1e12513SWes Peters.\" 2. Redistributions in binary form must reproduce the above
21b1e12513SWes Peters.\"    copyright notices, this list of conditions and the following
22b1e12513SWes Peters.\"    disclaimer in the documentation and/or other materials provided
23b1e12513SWes Peters.\"    with the distribution.
24b1e12513SWes Peters.\"
25b1e12513SWes Peters.\" 3. All advertising materials mentioning features or use of this
26b1e12513SWes Peters.\"    software must display the following acknowledgement:
27b1e12513SWes Peters.\"
28b1e12513SWes Peters.\"	This product includes software developed by Softweyr LLC, the
29b1e12513SWes Peters.\"      University of California, Berkeley, and its contributors.
30b1e12513SWes Peters.\"
31b1e12513SWes Peters.\" 4. Neither the name of Softweyr LLC, the University nor the names
32b1e12513SWes Peters.\"    of its contributors may be used to endorse or promote products
33b1e12513SWes Peters.\"    derived from this software without specific prior written
34b1e12513SWes Peters.\"    permission.
35b1e12513SWes Peters.\"
36b1e12513SWes Peters.\" THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND
37b1e12513SWes Peters.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
38b1e12513SWes Peters.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
39b1e12513SWes Peters.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40b1e12513SWes Peters.\" DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE REGENTS, OR
41b1e12513SWes Peters.\" CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42b1e12513SWes Peters.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43b1e12513SWes Peters.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44b1e12513SWes Peters.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45b1e12513SWes Peters.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46b1e12513SWes Peters.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47b1e12513SWes Peters.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4858f0484fSRodney W. Grimes.\" SUCH DAMAGE.
4958f0484fSRodney W. Grimes.\"
5058f0484fSRodney W. Grimes.\"     @(#)strtok.3	8.2 (Berkeley) 2/3/94
517f3dea24SPeter Wemm.\" $FreeBSD$
5258f0484fSRodney W. Grimes.\"
53b1e12513SWes Peters.Dd November 27, 1998
5458f0484fSRodney W. Grimes.Dt STRTOK 3
55b1e12513SWes Peters.Os FreeBSD 3.0
5658f0484fSRodney W. Grimes.Sh NAME
57b1e12513SWes Peters.Nm strtok, strtok_r
5858f0484fSRodney W. Grimes.Nd string tokens
5925bb73e0SAlexey Zelkin.Sh LIBRARY
6025bb73e0SAlexey Zelkin.Lb libc
6158f0484fSRodney W. Grimes.Sh SYNOPSIS
6258f0484fSRodney W. Grimes.Fd #include <string.h>
6358f0484fSRodney W. Grimes.Ft char *
6458f0484fSRodney W. Grimes.Fn strtok "char *str" "const char *sep"
65b1e12513SWes Peters.Ft char *
66b1e12513SWes Peters.Fn strtok_r "char *str" "const char *sep" "char **last"
6758f0484fSRodney W. Grimes.Sh DESCRIPTION
6858f0484fSRodney W. Grimes.Bf -symbolic
6958f0484fSRodney W. GrimesThis interface is obsoleted by strsep(3).
7058f0484fSRodney W. Grimes.Ef
7158f0484fSRodney W. Grimes.Pp
7258f0484fSRodney W. GrimesThe
7358f0484fSRodney W. Grimes.Fn strtok
7458f0484fSRodney W. Grimesfunction
7558f0484fSRodney W. Grimesis used to isolate sequential tokens in a null-terminated string,
7658f0484fSRodney W. Grimes.Fa str .
7758f0484fSRodney W. GrimesThese tokens are separated in the string by at least one of the
7858f0484fSRodney W. Grimescharacters in
7958f0484fSRodney W. Grimes.Fa sep .
8058f0484fSRodney W. GrimesThe first time that
8158f0484fSRodney W. Grimes.Fn strtok
8258f0484fSRodney W. Grimesis called,
8358f0484fSRodney W. Grimes.Fa str
8458f0484fSRodney W. Grimesshould be specified; subsequent calls, wishing to obtain further tokens
85555fff21STim Vanderhoekfrom the same string, should pass a null pointer instead.
8658f0484fSRodney W. GrimesThe separator string,
8758f0484fSRodney W. Grimes.Fa sep ,
8858f0484fSRodney W. Grimesmust be supplied each time, and may change between calls.
8958f0484fSRodney W. Grimes.Pp
9058f0484fSRodney W. GrimesThe
91b1e12513SWes Peters.Fn strtok_r
92b1e12513SWes Petersfunction is a reentrant version of
9332150a67SJohn Polstra.Fn strtok .
9432150a67SJohn PolstraThe context pointer
95b1e12513SWes Peters.Fa last
96b1e12513SWes Petersmust be provided on each call.
97b1e12513SWes Peters.Fn strtok_r
98b1e12513SWes Petersmay also be used to nest two parsing loops within one another, as
9932150a67SJohn Polstralong as separate context pointers are used.
100b1e12513SWes Peters.Pp
101b1e12513SWes PetersThe
10258f0484fSRodney W. Grimes.Fn strtok
103b1e12513SWes Petersand
104b1e12513SWes Peters.Fn strtok_r
105b1e12513SWes Petersfunctions
106b1e12513SWes Petersreturn a pointer to the beginning of each subsequent token in the string,
10758f0484fSRodney W. Grimesafter replacing the token itself with a
10858f0484fSRodney W. Grimes.Dv NUL
10958f0484fSRodney W. Grimescharacter.
110555fff21STim VanderhoekWhen no more tokens remain, a null pointer is returned.
111b1e12513SWes Peters.Sh EXAMPLE
112b1e12513SWes PetersThe following uses
113b7edc980SWes Peters.Fn strtok_r
114b1e12513SWes Petersto parse two strings using separate contexts:
115b1e12513SWes Peters.Bd -literal
116b1e12513SWes Peterschar test[80], blah[80];
1172704b2cbSGuy Helmerchar *sep = "\e\e/:;=-";
118b1e12513SWes Peterschar *word, *phrase, *brkt, *brkb;
119b1e12513SWes Peters
1202704b2cbSGuy Helmerstrcpy(test, "This;is.a:test:of=the/string\e\etokenizer-function.");
121b1e12513SWes Peters
122b1e12513SWes Petersfor (word = strtok_r(test, sep, &brkt);
123b1e12513SWes Peters     word;
124b1e12513SWes Peters     word = strtok_r(NULL, sep, &brkt))
125b1e12513SWes Peters{
126b1e12513SWes Peters    strcpy(blah, "blah:blat:blab:blag");
127b1e12513SWes Peters
128b1e12513SWes Peters    for (phrase = strtok_r(blah, sep, &brkb);
129b1e12513SWes Peters         phrase;
130b1e12513SWes Peters         phrase = strtok_r(NULL, sep, &brkb))
131b1e12513SWes Peters    {
1322704b2cbSGuy Helmer        printf("So far we're at %s:%s\en", word, phrase);
133b1e12513SWes Peters    }
134b1e12513SWes Peters}
135b1e12513SWes Peters.Ed
13658f0484fSRodney W. Grimes.Sh SEE ALSO
13758f0484fSRodney W. Grimes.Xr index 3 ,
13858f0484fSRodney W. Grimes.Xr memchr 3 ,
13958f0484fSRodney W. Grimes.Xr rindex 3 ,
14058f0484fSRodney W. Grimes.Xr strchr 3 ,
14158f0484fSRodney W. Grimes.Xr strcspn 3 ,
14258f0484fSRodney W. Grimes.Xr strpbrk 3 ,
14358f0484fSRodney W. Grimes.Xr strrchr 3 ,
14458f0484fSRodney W. Grimes.Xr strsep 3 ,
14558f0484fSRodney W. Grimes.Xr strspn 3 ,
14658f0484fSRodney W. Grimes.Xr strstr 3
14758f0484fSRodney W. Grimes.Sh STANDARDS
14858f0484fSRodney W. GrimesThe
14958f0484fSRodney W. Grimes.Fn strtok
15058f0484fSRodney W. Grimesfunction
15158f0484fSRodney W. Grimesconforms to
15258f0484fSRodney W. Grimes.St -ansiC .
15358f0484fSRodney W. Grimes.Sh BUGS
15458f0484fSRodney W. GrimesThe System V
15558f0484fSRodney W. Grimes.Fn strtok ,
15658f0484fSRodney W. Grimesif handed a string containing only delimiter characters,
15758f0484fSRodney W. Grimeswill not alter the next starting point, so that a call to
15858f0484fSRodney W. Grimes.Fn strtok
15958f0484fSRodney W. Grimeswith a different (or empty) delimiter string
16058f0484fSRodney W. Grimesmay return a
16158f0484fSRodney W. Grimes.Pf non- Dv NULL
16258f0484fSRodney W. Grimesvalue.
16358f0484fSRodney W. GrimesSince this implementation always alters the next starting point,
16458f0484fSRodney W. Grimessuch a sequence of calls would always return
16558f0484fSRodney W. Grimes.Dv NULL .
16674804d58SMike Pritchard.Sh AUTHORS
16774804d58SMike Pritchard.An Wes Peters, Softweyr LLC: Aq wes@softweyr.com
168b1e12513SWes Peters.br
16974804d58SMike PritchardBased on the
17074804d58SMike Pritchard.Fx 3.0
17174804d58SMike Pritchardimplementation.
172