xref: /openbsd-src/lib/libc/string/strtok.3 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\" Copyright (c) 1988, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"	This product includes software developed by the University of
19.\"	California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\"    may be used to endorse or promote products derived from this software
22.\"    without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\"	$OpenBSD: strtok.3,v 1.15 2001/08/06 10:42:26 mpech Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRTOK 3
40.Os
41.Sh NAME
42.Nm strtok ,
43.Nm strtok_r
44.Nd string token operations
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft char *
48.Fn strtok "char *str" "const char *sep"
49.Ft char *
50.Fn strtok_r "char *str" "const char *sep" "char **last"
51.Sh DESCRIPTION
52.Bf -symbolic
53This interface is obsoleted by
54.Xr strsep 3 .
55.Ef
56.Pp
57The
58.Fn strtok
59function is used to isolate sequential tokens in a null-terminated string,
60.Fa str .
61These tokens are separated in the string by at least one of the
62characters in
63.Fa sep .
64The first time that
65.Fn strtok
66is called,
67.Fa str
68should be specified; subsequent calls, wishing to obtain further tokens
69from the same string, should pass a null pointer instead.
70The separator string,
71.Fa sep ,
72must be supplied each time, and may change between calls.
73.Pp
74The
75.Fn strtok_r
76function is a version of
77.Fn strtok
78that takes an explicit context argument and is reentrant.
79.Pp
80The
81.Fn strtok
82and
83.Fn strtok_r
84functions return a pointer to the beginning of each subsequent token
85in the string, after replacing the separator character itself with an
86.Tn ASCII NUL
87character.
88When no more tokens remain, a null pointer is returned.
89.Pp
90Since
91.Fn strtok
92and
93.Fn strtok_r
94modify the string,
95.Fa str
96should not point to an area in the initialized data segment.
97.Sh EXAMPLES
98The following will construct an array of pointers to each individual word in
99the string
100.Va s :
101.Bd -literal -offset indent
102#define MAXTOKENS	128
103
104char s[512], *p, *tokens[MAXTOKENS];
105char *last;
106int i = 0;
107
108snprintf(s, sizeof(s), "cat dog horse cow");
109
110for ((p = strtok_r(s, " ", &last)); p;
111    (p = strtok_r(NULL, " ", &last)), i++) {
112	if (i < MAXTOKENS - 1)
113		tokens[i] = p;
114}
115tokens[i] = NULL;
116.Ed
117.Pp
118That is,
119.Li tokens[0]
120will point to
121.Qq cat ,
122.Li tokens[1]
123will point to
124.Qq dog ,
125.Li tokens[2]
126will point to
127.Qq horse ,
128and
129.Li tokens[3]
130will point to
131.Qq cow .
132.Sh SEE ALSO
133.Xr memchr 3 ,
134.Xr strchr 3 ,
135.Xr strcspn 3 ,
136.Xr strpbrk 3 ,
137.Xr strrchr 3 ,
138.Xr strsep 3 ,
139.Xr strspn 3 ,
140.Xr strstr 3
141.Sh STANDARDS
142The
143.Fn strtok
144function conforms to
145.St -ansiC .
146.Sh BUGS
147The System V
148.Fn strtok ,
149if handed a string containing only delimiter characters,
150will not alter the next starting point, so that a call to
151.Fn strtok
152with a different (or empty) delimiter string
153may return a non-null value.
154Since this implementation always alters the next starting point,
155such a sequence of calls would always return
156.Dv NULL .
157