]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strtok.3
twalk_r.3: New link to twalk(3) page
[thirdparty/man-pages.git] / man3 / strtok.3
CommitLineData
616c2730 1.\" Copyright (C) 2005, 2013 Michael Kerrisk <mtk.manpages@gmail.com>
ff452e0d
MK
2.\" a few fragments from an earlier (1996) version by
3.\" Andries Brouwer (aeb@cwi.nl) remain.
fea681da 4.\"
93015253 5.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
6.\" Permission is granted to make and distribute verbatim copies of this
7.\" manual provided the copyright notice and this permission notice are
8.\" preserved on all copies.
9.\"
10.\" Permission is granted to copy and distribute modified versions of this
11.\" manual under the conditions for verbatim copying, provided that the
12.\" entire resulting derived work is distributed under the terms of a
13.\" permission notice identical to this one.
c13182ef 14.\"
fea681da
MK
15.\" Since the Linux kernel and libraries are constantly changing, this
16.\" manual page may be incorrect or out-of-date. The author(s) assume no
17.\" responsibility for errors or omissions, or for damages resulting from
18.\" the use of the information contained herein. The author(s) may not
19.\" have taken the same level of care in the production of this manual,
20.\" which is licensed free of charge, as they might when working
21.\" professionally.
c13182ef 22.\"
fea681da
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 25.\" %%%LICENSE_END
fea681da
MK
26.\"
27.\" Rewritten old page, 960210, aeb@cwi.nl
e00c3a07 28.\" Updated, added strtok_r. 2000-02-13 Nicolás Lichtmaier <nick@debian.org>
87cee315 29.\" 2005-11-17, mtk: Substantial parts rewritten
ff452e0d 30.\" 2013-05-19, mtk: added much further detail on the operation of strtok()
87cee315 31.\"
9ba01802 32.TH STRTOK 3 2019-03-06 "GNU" "Linux Programmer's Manual"
fea681da
MK
33.SH NAME
34strtok, strtok_r \- extract tokens from strings
35.SH SYNOPSIS
36.nf
37.B #include <string.h>
68e4db0a 38.PP
87cee315 39.BI "char *strtok(char *" str ", const char *" delim );
68e4db0a 40.PP
87cee315 41.BI "char *strtok_r(char *" str ", const char *" delim ", char **" saveptr );
fea681da 42.fi
68e4db0a 43.PP
cc4615cc
MK
44.in -4n
45Feature Test Macro Requirements for glibc (see
46.BR feature_test_macros (7)):
47.in
68e4db0a 48.PP
cc4615cc
MK
49.ad l
50.BR strtok_r ():
d59161f9
MK
51_POSIX_C_SOURCE
52 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
cc4615cc 53.ad b
fea681da 54.SH DESCRIPTION
60a90ecd
MK
55The
56.BR strtok ()
ff452e0d 57function breaks a string into a sequence of zero or more nonempty tokens.
60a90ecd 58On the first call to
6b8b0e50 59.BR strtok (),
60a90ecd 60the string to be parsed should be
46d8df8e
MK
61specified in
62.IR str .
c13182ef 63In each subsequent call that should parse the same string,
46d8df8e
MK
64.I str
65must be NULL.
847e0d88 66.PP
46d8df8e
MK
67The
68.I delim
69argument specifies a set of bytes that
87cee315 70delimit the tokens in the parsed string.
46d8df8e
MK
71The caller may specify different strings in
72.I delim
73in successive
87cee315 74calls that parse the same string.
847e0d88 75.PP
60a90ecd
MK
76Each call to
77.BR strtok ()
78returns a pointer to a
87cee315 79null-terminated string containing the next token.
a00b7454 80This string does not include the delimiting byte.
60a90ecd
MK
81If no more tokens are found,
82.BR strtok ()
83returns NULL.
847e0d88 84.PP
ff452e0d
MK
85A sequence of calls to
86.BR strtok ()
87that operate on the same string maintains a pointer
88that determines the point from which to start searching for the next token.
89The first call to
90.BR strtok ()
91sets this pointer to point to the first byte of the string.
51700fd7 92The start of the next token is determined by scanning forward
ff452e0d
MK
93for the next nondelimiter byte in
94.IR str .
95If such a byte is found, it is taken as the start of the next token.
96If no such byte is found,
97then there are no more tokens, and
98.BR strtok ()
99returns NULL.
89b85ead 100(A string that is empty or that contains only delimiters
ff452e0d
MK
101will thus cause
102.BR strtok ()
103to return NULL on the first call.)
847e0d88 104.PP
ff452e0d
MK
105The end of each token is found by scanning forward until either
106the next delimiter byte is found or until the
d1a71985 107terminating null byte (\(aq\e0\(aq) is encountered.
ff452e0d
MK
108If a delimiter byte is found, it is overwritten with
109a null byte to terminate the current token, and
110.BR strtok ()
111saves a pointer to the following byte;
112that pointer will be used as the starting point
113when searching for the next token.
114In this case,
115.BR strtok ()
116returns a pointer to the start of the found token.
847e0d88 117.PP
ff452e0d
MK
118From the above description,
119it follows that a sequence of two or more contiguous delimiter bytes in
120the parsed string is considered to be a single delimiter, and that
121delimiter bytes at the start or end of the string are ignored.
60a90ecd
MK
122Put another way: the tokens returned by
123.BR strtok ()
aa796481 124are always nonempty strings.
ff452e0d
MK
125Thus, for example, given the string "\fIaaa;;bbb,\fP",
126successive calls to
127.BR strtok ()
51700fd7 128that specify the delimiter string "\fI;,\fP"
ff452e0d 129would return the strings "\fIaaa\fP" and "\fIbbb\fP",
b437fdd9 130and then a null pointer.
847e0d88 131.PP
c13182ef 132The
87cee315
MK
133.BR strtok_r ()
134function is a reentrant version
135.BR strtok ().
46d8df8e
MK
136The
137.I saveptr
138argument is a pointer to a
139.IR "char\ *"
140variable that is used internally by
87cee315
MK
141.BR strtok_r ()
142in order to maintain context between successive calls that parse the
143same string.
847e0d88 144.PP
c13182ef 145On the first call to
87cee315
MK
146.BR strtok_r (),
147.I str
c13182ef 148should point to the string to be parsed, and the value of
87cee315
MK
149.I saveptr
150is ignored.
46d8df8e
MK
151In subsequent calls,
152.I str
153should be NULL, and
154.I saveptr
155should be unchanged since the previous call.
847e0d88 156.PP
87cee315
MK
157Different strings may be parsed concurrently using sequences of calls to
158.BR strtok_r ()
46d8df8e
MK
159that specify different
160.I saveptr
161arguments.
47297adb 162.SH RETURN VALUE
2b2581ee
MK
163The
164.BR strtok ()
165and
166.BR strtok_r ()
167functions return a pointer to
168the next token, or NULL if there are no more tokens.
32208956 169.SH ATTRIBUTES
4d22c42d
PH
170For an explanation of the terms used in this section, see
171.BR attributes (7).
172.TS
173allbox;
174lb lb lb
175l l l.
176Interface Attribute Value
177T{
32208956 178.BR strtok ()
05e8fa8f 179T} Thread safety MT-Unsafe race:strtok
4d22c42d 180T{
32208956 181.BR strtok_r ()
4d22c42d
PH
182T} Thread safety MT-Safe
183.TE
47297adb 184.SH CONFORMING TO
2b2581ee
MK
185.TP
186.BR strtok ()
3546ed98 187POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
2b2581ee
MK
188.TP
189.BR strtok_r ()
3546ed98 190POSIX.1-2001, POSIX.1-2008.
2b2581ee 191.SH BUGS
132719a5 192Be cautious when using these functions.
2b2581ee 193If you do use them, note that:
7bb33c46 194.IP * 2
2b2581ee 195These functions modify their first argument.
7bb33c46 196.IP *
2b2581ee 197These functions cannot be used on constant strings.
7bb33c46 198.IP *
a00b7454 199The identity of the delimiting byte is lost.
7bb33c46 200.IP *
2b2581ee
MK
201The
202.BR strtok ()
203function uses a static buffer while parsing, so it's not thread safe.
204Use
205.BR strtok_r ()
206if this matters to you.
9b336505 207.SH EXAMPLE
a831ef97 208The program below uses nested loops that employ
87cee315
MK
209.BR strtok_r ()
210to break a string into a two-level hierarchy of tokens.
211The first command-line argument specifies the string to be parsed.
a00b7454 212The second argument specifies the delimiter byte(s)
87cee315 213to be used to separate that string into "major" tokens.
a00b7454 214The third argument specifies the delimiter byte(s)
87cee315 215to be used to separate the "major" tokens into subtokens.
fea681da 216.PP
a831ef97
MK
217An example of the output produced by this program is the following:
218.PP
219.in +4n
b8302363 220.EX
a831ef97
MK
221.RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
2221: a/bbb///cc
223 \-\-> a
224 \-\-> bbb
225 \-\-> cc
2262: xxx
227 \-\-> xxx
2283: yyy
229 \-\-> yyy
b8302363 230.EE
a831ef97
MK
231.in
232.SS Program source
d84d0300 233\&
e7d0bb47 234.EX
87cee315
MK
235#include <stdio.h>
236#include <stdlib.h>
237#include <string.h>
238
239int
240main(int argc, char *argv[])
241{
242 char *str1, *str2, *token, *subtoken;
243 char *saveptr1, *saveptr2;
244 int j;
245
246 if (argc != 4) {
d1a71985 247 fprintf(stderr, "Usage: %s string delim subdelim\en",
87cee315
MK
248 argv[0]);
249 exit(EXIT_FAILURE);
c13182ef 250 }
87cee315
MK
251
252 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
253 token = strtok_r(str1, argv[2], &saveptr1);
254 if (token == NULL)
255 break;
d1a71985 256 printf("%d: %s\en", j, token);
87cee315
MK
257
258 for (str2 = token; ; str2 = NULL) {
259 subtoken = strtok_r(str2, argv[3], &saveptr2);
260 if (subtoken == NULL)
261 break;
d1a71985 262 printf("\t \-\-> %s\en", subtoken);
c13182ef
MK
263 }
264 }
87cee315
MK
265
266 exit(EXIT_SUCCESS);
c54ed37e 267}
e7d0bb47 268.EE
f91b1db0
PB
269.PP
270Another example program using
271.BR strtok ()
272can be found in
273.BR getaddrinfo_a (3).
47297adb 274.SH SEE ALSO
fea681da
MK
275.BR index (3),
276.BR memchr (3),
277.BR rindex (3),
278.BR strchr (3),
d095200e 279.BR string (3),
fea681da
MK
280.BR strpbrk (3),
281.BR strsep (3),
282.BR strspn (3),
1709027c
MK
283.BR strstr (3),
284.BR wcstok (3)