xref: /freebsd/lib/libc/string/strtok.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
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.\"
253fb3b97cSEd Maste.\" 3. Neither the name of Softweyr LLC, the University nor the names
26b1e12513SWes Peters.\"    of its contributors may be used to endorse or promote products
27b1e12513SWes Peters.\"    derived from this software without specific prior written
28b1e12513SWes Peters.\"    permission.
29b1e12513SWes Peters.\"
30b1e12513SWes Peters.\" THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND
31b1e12513SWes Peters.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
32b1e12513SWes Peters.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33b1e12513SWes Peters.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34b1e12513SWes Peters.\" DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE REGENTS, OR
35b1e12513SWes Peters.\" CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36b1e12513SWes Peters.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37b1e12513SWes Peters.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
38b1e12513SWes Peters.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39b1e12513SWes Peters.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
40b1e12513SWes Peters.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
41b1e12513SWes Peters.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4258f0484fSRodney W. Grimes.\" SUCH DAMAGE.
4358f0484fSRodney W. Grimes.\"
44c18d8171SWarren Block.Dd January 22, 2016
4558f0484fSRodney W. Grimes.Dt STRTOK 3
46a307d598SRuslan Ermilov.Os
4758f0484fSRodney W. Grimes.Sh NAME
48b1e12513SWes Peters.Nm strtok , strtok_r
4958f0484fSRodney W. Grimes.Nd string tokens
5025bb73e0SAlexey Zelkin.Sh LIBRARY
5125bb73e0SAlexey Zelkin.Lb libc
5258f0484fSRodney W. Grimes.Sh SYNOPSIS
5332eef9aeSRuslan Ermilov.In string.h
5458f0484fSRodney W. Grimes.Ft char *
5558f0484fSRodney W. Grimes.Fn strtok "char *str" "const char *sep"
56b1e12513SWes Peters.Ft char *
57b1e12513SWes Peters.Fn strtok_r "char *str" "const char *sep" "char **last"
5858f0484fSRodney W. Grimes.Sh DESCRIPTION
5958f0484fSRodney W. Grimes.Bf -symbolic
606575e6daSRuslan ErmilovThis interface is obsoleted by
616575e6daSRuslan Ermilov.Xr strsep 3 .
6258f0484fSRodney W. Grimes.Ef
6358f0484fSRodney W. Grimes.Pp
6458f0484fSRodney W. GrimesThe
6558f0484fSRodney W. Grimes.Fn strtok
6658f0484fSRodney W. Grimesfunction
6758f0484fSRodney W. Grimesis used to isolate sequential tokens in a null-terminated string,
6858f0484fSRodney W. Grimes.Fa str .
6958f0484fSRodney W. GrimesThese tokens are separated in the string by at least one of the
7058f0484fSRodney W. Grimescharacters in
7158f0484fSRodney W. Grimes.Fa sep .
7258f0484fSRodney W. GrimesThe first time that
7358f0484fSRodney W. Grimes.Fn strtok
7458f0484fSRodney W. Grimesis called,
7558f0484fSRodney W. Grimes.Fa str
7658f0484fSRodney W. Grimesshould be specified; subsequent calls, wishing to obtain further tokens
77555fff21STim Vanderhoekfrom the same string, should pass a null pointer instead.
7858f0484fSRodney W. GrimesThe separator string,
7958f0484fSRodney W. Grimes.Fa sep ,
8058f0484fSRodney W. Grimesmust be supplied each time, and may change between calls.
8158f0484fSRodney W. Grimes.Pp
82e4269284SAndrey A. ChernovThe implementation will behave as if no library function calls
83e4269284SAndrey A. Chernov.Fn strtok .
84e4269284SAndrey A. Chernov.Pp
8558f0484fSRodney W. GrimesThe
86b1e12513SWes Peters.Fn strtok_r
87b1e12513SWes Petersfunction is a reentrant version of
8832150a67SJohn Polstra.Fn strtok .
8932150a67SJohn PolstraThe context pointer
90b1e12513SWes Peters.Fa last
91b1e12513SWes Petersmust be provided on each call.
921fae73b1SRuslan ErmilovThe
93b1e12513SWes Peters.Fn strtok_r
941fae73b1SRuslan Ermilovfunction
95b1e12513SWes Petersmay also be used to nest two parsing loops within one another, as
9632150a67SJohn Polstralong as separate context pointers are used.
97*2abf05c4SPiotr Pawel Stefaniak.Sh RETURN VALUES
98b1e12513SWes PetersThe
9958f0484fSRodney W. Grimes.Fn strtok
100b1e12513SWes Petersand
101b1e12513SWes Peters.Fn strtok_r
102b1e12513SWes Petersfunctions
103b1e12513SWes Petersreturn a pointer to the beginning of each subsequent token in the string,
10458f0484fSRodney W. Grimesafter replacing the token itself with a
10558f0484fSRodney W. Grimes.Dv NUL
10658f0484fSRodney W. Grimescharacter.
107555fff21STim VanderhoekWhen no more tokens remain, a null pointer is returned.
108251c176fSRuslan Ermilov.Sh EXAMPLES
109b1e12513SWes PetersThe following uses
110b7edc980SWes Peters.Fn strtok_r
111b1e12513SWes Petersto parse two strings using separate contexts:
112b1e12513SWes Peters.Bd -literal
113b1e12513SWes Peterschar test[80], blah[80];
1142704b2cbSGuy Helmerchar *sep = "\e\e/:;=-";
115b1e12513SWes Peterschar *word, *phrase, *brkt, *brkb;
116b1e12513SWes Peters
1172704b2cbSGuy Helmerstrcpy(test, "This;is.a:test:of=the/string\e\etokenizer-function.");
118b1e12513SWes Peters
119b1e12513SWes Petersfor (word = strtok_r(test, sep, &brkt);
120b1e12513SWes Peters     word;
121b1e12513SWes Peters     word = strtok_r(NULL, sep, &brkt))
122b1e12513SWes Peters{
123b1e12513SWes Peters    strcpy(blah, "blah:blat:blab:blag");
124b1e12513SWes Peters
125b1e12513SWes Peters    for (phrase = strtok_r(blah, sep, &brkb);
126b1e12513SWes Peters         phrase;
127b1e12513SWes Peters         phrase = strtok_r(NULL, sep, &brkb))
128b1e12513SWes Peters    {
1292704b2cbSGuy Helmer        printf("So far we're at %s:%s\en", word, phrase);
130b1e12513SWes Peters    }
131b1e12513SWes Peters}
132b1e12513SWes Peters.Ed
13358f0484fSRodney W. Grimes.Sh SEE ALSO
13458f0484fSRodney W. Grimes.Xr memchr 3 ,
13558f0484fSRodney W. Grimes.Xr strchr 3 ,
13658f0484fSRodney W. Grimes.Xr strcspn 3 ,
13758f0484fSRodney W. Grimes.Xr strpbrk 3 ,
13858f0484fSRodney W. Grimes.Xr strrchr 3 ,
13958f0484fSRodney W. Grimes.Xr strsep 3 ,
14058f0484fSRodney W. Grimes.Xr strspn 3 ,
1412fe3e5cdSTim J. Robbins.Xr strstr 3 ,
1422fe3e5cdSTim J. Robbins.Xr wcstok 3
14358f0484fSRodney W. Grimes.Sh STANDARDS
14458f0484fSRodney W. GrimesThe
14558f0484fSRodney W. Grimes.Fn strtok
14658f0484fSRodney W. Grimesfunction
14758f0484fSRodney W. Grimesconforms to
148588a200cSRuslan Ermilov.St -isoC .
149c18d8171SWarren BlockThe
150c18d8171SWarren Block.Fn strtok_r
151c18d8171SWarren Blockfunction
152c18d8171SWarren Blockconforms to
153c18d8171SWarren Block.St -p1003.1-2001 .
15424a0682cSRuslan Ermilov.Sh AUTHORS
1558fbf3d50SBaptiste Daroussin.An Wes Peters Aq Mt wes@softweyr.com ,
1568fbf3d50SBaptiste DaroussinSoftweyr LLC
15724a0682cSRuslan Ermilov.Pp
15824a0682cSRuslan ErmilovBased on the
15924a0682cSRuslan Ermilov.Fx 3.0
16024a0682cSRuslan Ermilovimplementation.
16158f0484fSRodney W. Grimes.Sh BUGS
16258f0484fSRodney W. GrimesThe System V
16358f0484fSRodney W. Grimes.Fn strtok ,
16458f0484fSRodney W. Grimesif handed a string containing only delimiter characters,
16558f0484fSRodney W. Grimeswill not alter the next starting point, so that a call to
16658f0484fSRodney W. Grimes.Fn strtok
16758f0484fSRodney W. Grimeswith a different (or empty) delimiter string
16858f0484fSRodney W. Grimesmay return a
16958f0484fSRodney W. Grimes.Pf non- Dv NULL
17058f0484fSRodney W. Grimesvalue.
17158f0484fSRodney W. GrimesSince this implementation always alters the next starting point,
17258f0484fSRodney W. Grimessuch a sequence of calls would always return
17358f0484fSRodney W. Grimes.Dv NULL .
174