1STRTOK(3) Linux Programmer's Manual STRTOK(3)
2
3
4
6 strtok, strtok_r - extract tokens from strings
7
9 #include <string.h>
10
11 char *strtok(char *str, const char *delim);
12
13 char *strtok_r(char *str, const char *delim, char **saveptr);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 strtok_r(): _POSIX_C_SOURCE
18 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
19
21 The strtok() function breaks a string into a sequence of zero or more
22 nonempty tokens. On the first call to strtok(), the string to be
23 parsed should be specified in str. In each subsequent call that should
24 parse the same string, str must be NULL.
25
26 The delim argument specifies a set of bytes that delimit the tokens in
27 the parsed string. The caller may specify different strings in delim
28 in successive calls that parse the same string.
29
30 Each call to strtok() returns a pointer to a null-terminated string
31 containing the next token. This string does not include the delimiting
32 byte. If no more tokens are found, strtok() returns NULL.
33
34 A sequence of calls to strtok() that operate on the same string main‐
35 tains a pointer that determines the point from which to start searching
36 for the next token. The first call to strtok() sets this pointer to
37 point to the first byte of the string. The start of the next token is
38 determined by scanning forward for the next nondelimiter byte in str.
39 If such a byte is found, it is taken as the start of the next token.
40 If no such byte is found, then there are no more tokens, and strtok()
41 returns NULL. (A string that is empty or that contains only delimiters
42 will thus cause strtok() to return NULL on the first call.)
43
44 The end of each token is found by scanning forward until either the
45 next delimiter byte is found or until the terminating null byte ('\0')
46 is encountered. If a delimiter byte is found, it is overwritten with a
47 null byte to terminate the current token, and strtok() saves a pointer
48 to the following byte; that pointer will be used as the starting point
49 when searching for the next token. In this case, strtok() returns a
50 pointer to the start of the found token.
51
52 From the above description, it follows that a sequence of two or more
53 contiguous delimiter bytes in the parsed string is considered to be a
54 single delimiter, and that delimiter bytes at the start or end of the
55 string are ignored. Put another way: the tokens returned by strtok()
56 are always nonempty strings. Thus, for example, given the string
57 "aaa;;bbb,", successive calls to strtok() that specify the delimiter
58 string ";," would return the strings "aaa" and "bbb", and then a null
59 pointer.
60
61 The strtok_r() function is a reentrant version strtok(). The saveptr
62 argument is a pointer to a char * variable that is used internally by
63 strtok_r() in order to maintain context between successive calls that
64 parse the same string.
65
66 On the first call to strtok_r(), str should point to the string to be
67 parsed, and the value of saveptr is ignored. In subsequent calls, str
68 should be NULL, and saveptr should be unchanged since the previous
69 call.
70
71 Different strings may be parsed concurrently using sequences of calls
72 to strtok_r() that specify different saveptr arguments.
73
75 The strtok() and strtok_r() functions return a pointer to the next
76 token, or NULL if there are no more tokens.
77
79 For an explanation of the terms used in this section, see
80 attributes(7).
81
82 ┌───────────┬───────────────┬───────────────────────┐
83 │Interface │ Attribute │ Value │
84 ├───────────┼───────────────┼───────────────────────┤
85 │strtok() │ Thread safety │ MT-Unsafe race:strtok │
86 ├───────────┼───────────────┼───────────────────────┤
87 │strtok_r() │ Thread safety │ MT-Safe │
88 └───────────┴───────────────┴───────────────────────┘
90 strtok()
91 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
92
93 strtok_r()
94 POSIX.1-2001, POSIX.1-2008.
95
97 Be cautious when using these functions. If you do use them, note that:
98
99 * These functions modify their first argument.
100
101 * These functions cannot be used on constant strings.
102
103 * The identity of the delimiting byte is lost.
104
105 * The strtok() function uses a static buffer while parsing, so it's not
106 thread safe. Use strtok_r() if this matters to you.
107
109 The program below uses nested loops that employ strtok_r() to break a
110 string into a two-level hierarchy of tokens. The first command-line
111 argument specifies the string to be parsed. The second argument speci‐
112 fies the delimiter byte(s) to be used to separate that string into
113 "major" tokens. The third argument specifies the delimiter byte(s) to
114 be used to separate the "major" tokens into subtokens.
115
116 An example of the output produced by this program is the following:
117
118 $ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
119 1: a/bbb///cc
120 --> a
121 --> bbb
122 --> cc
123 2: xxx
124 --> xxx
125 3: yyy
126 --> yyy
127
128 Program source
129
130 #include <stdio.h>
131 #include <stdlib.h>
132 #include <string.h>
133
134 int
135 main(int argc, char *argv[])
136 {
137 char *str1, *str2, *token, *subtoken;
138 char *saveptr1, *saveptr2;
139 int j;
140
141 if (argc != 4) {
142 fprintf(stderr, "Usage: %s string delim subdelim\n",
143 argv[0]);
144 exit(EXIT_FAILURE);
145 }
146
147 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
148 token = strtok_r(str1, argv[2], &saveptr1);
149 if (token == NULL)
150 break;
151 printf("%d: %s\n", j, token);
152
153 for (str2 = token; ; str2 = NULL) {
154 subtoken = strtok_r(str2, argv[3], &saveptr2);
155 if (subtoken == NULL)
156 break;
157 printf(" --> %s\n", subtoken);
158 }
159 }
160
161 exit(EXIT_SUCCESS);
162 }
163
164 Another example program using strtok() can be found in getad‐
165 drinfo_a(3).
166
168 index(3), memchr(3), rindex(3), strchr(3), string(3), strpbrk(3),
169 strsep(3), strspn(3), strstr(3), wcstok(3)
170
172 This page is part of release 4.16 of the Linux man-pages project. A
173 description of the project, information about reporting bugs, and the
174 latest version of this page, can be found at
175 https://www.kernel.org/doc/man-pages/.
176
177
178
179GNU 2017-09-15 STRTOK(3)