1 .\" Copyright (C) 2005, 2013 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" a few fragments from an earlier (1996) version by
3 .\" Andries Brouwer (aeb@cwi.nl) remain.
5 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Rewritten old page, 960210, aeb@cwi.nl
28 .\" Updated, added strtok_r. 2000-02-13 Nicolás Lichtmaier <nick@debian.org>
29 .\" 2005-11-17, mtk: Substantial parts rewritten
30 .\" 2013-05-19, mtk: added much further detail on the operation of strtok()
32 .TH STRTOK 3 2019-03-06 "GNU" "Linux Programmer's Manual"
34 strtok, strtok_r \- extract tokens from strings
37 .B #include <string.h>
39 .BI "char *strtok(char *" str ", const char *" delim );
41 .BI "char *strtok_r(char *" str ", const char *" delim ", char **" saveptr );
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
52 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
57 function breaks a string into a sequence of zero or more nonempty tokens.
60 the string to be parsed should be
63 In each subsequent call that should parse the same string,
69 argument specifies a set of bytes that
70 delimit the tokens in the parsed string.
71 The caller may specify different strings in
74 calls that parse the same string.
78 returns a pointer to a
79 null-terminated string containing the next token.
80 This string does not include the delimiting byte.
81 If no more tokens are found,
85 A sequence of calls to
87 that operate on the same string maintains a pointer
88 that determines the point from which to start searching for the next token.
91 sets this pointer to point to the first byte of the string.
92 The start of the next token is determined by scanning forward
93 for the next nondelimiter byte in
95 If such a byte is found, it is taken as the start of the next token.
96 If no such byte is found,
97 then there are no more tokens, and
100 (A string that is empty or that contains only delimiters
103 to return NULL on the first call.)
105 The end of each token is found by scanning forward until either
106 the next delimiter byte is found or until the
107 terminating null byte (\(aq\e0\(aq) is encountered.
108 If a delimiter byte is found, it is overwritten with
109 a null byte to terminate the current token, and
111 saves a pointer to the following byte;
112 that pointer will be used as the starting point
113 when searching for the next token.
116 returns a pointer to the start of the found token.
118 From the above description,
119 it follows that a sequence of two or more contiguous delimiter bytes in
120 the parsed string is considered to be a single delimiter, and that
121 delimiter bytes at the start or end of the string are ignored.
122 Put another way: the tokens returned by
124 are always nonempty strings.
125 Thus, for example, given the string "\fIaaa;;bbb,\fP",
128 that specify the delimiter string "\fI;,\fP"
129 would return the strings "\fIaaa\fP" and "\fIbbb\fP",
130 and then a null pointer.
134 function is a reentrant version
138 argument is a pointer to a
140 variable that is used internally by
142 in order to maintain context between successive calls that parse the
148 should point to the string to be parsed, and the value of
155 should be unchanged since the previous call.
157 Different strings may be parsed concurrently using sequences of calls to
159 that specify different
167 functions return a pointer to
168 the next token, or NULL if there are no more tokens.
170 For an explanation of the terms used in this section, see
176 Interface Attribute Value
179 T} Thread safety MT-Unsafe race:strtok
182 T} Thread safety MT-Safe
187 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
190 POSIX.1-2001, POSIX.1-2008.
192 Be cautious when using these functions.
193 If you do use them, note that:
195 These functions modify their first argument.
197 These functions cannot be used on constant strings.
199 The identity of the delimiting byte is lost.
203 function uses a static buffer while parsing, so it's not thread safe.
206 if this matters to you.
208 The program below uses nested loops that employ
210 to break a string into a two-level hierarchy of tokens.
211 The first command-line argument specifies the string to be parsed.
212 The second argument specifies the delimiter byte(s)
213 to be used to separate that string into "major" tokens.
214 The third argument specifies the delimiter byte(s)
215 to be used to separate the "major" tokens into subtokens.
217 An example of the output produced by this program is the following:
221 .RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
240 main(int argc, char *argv[])
242 char *str1, *str2, *token, *subtoken;
243 char *saveptr1, *saveptr2;
247 fprintf(stderr, "Usage: %s string delim subdelim\en",
252 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
253 token = strtok_r(str1, argv[2], &saveptr1);
256 printf("%d: %s\en", j, token);
258 for (str2 = token; ; str2 = NULL) {
259 subtoken = strtok_r(str2, argv[3], &saveptr2);
260 if (subtoken == NULL)
262 printf("\t \-\-> %s\en", subtoken);
270 Another example program using
273 .BR getaddrinfo_a (3).