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(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE >= 1 ||
18 _XOPEN_SOURCE || _POSIX_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 parsed
23 should be specified in str. In each subsequent call that should parse
24 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 Multithreading (see pthreads(7))
80 The strtok() function is not thread-safe.
81
82 The strtok_r() function is thread-safe.
83
85 strtok()
86 SVr4, POSIX.1-2001, 4.3BSD, C89, C99.
87
88 strtok_r()
89 POSIX.1-2001.
90
92 Be cautious when using these functions. If you do use them, note that:
93
94 * These functions modify their first argument.
95
96 * These functions cannot be used on constant strings.
97
98 * The identity of the delimiting byte is lost.
99
100 * The strtok() function uses a static buffer while parsing, so it's not
101 thread safe. Use strtok_r() if this matters to you.
102
104 The program below uses nested loops that employ strtok_r() to break a
105 string into a two-level hierarchy of tokens. The first command-line
106 argument specifies the string to be parsed. The second argument speci‐
107 fies the delimiter byte(s) to be used to separate that string into
108 "major" tokens. The third argument specifies the delimiter byte(s) to
109 be used to separate the "major" tokens into subtokens.
110
111 An example of the output produced by this program is the following:
112
113 $ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
114 1: a/bbb///cc
115 --> a
116 --> bbb
117 --> cc
118 2: xxx
119 --> xxx
120 3: yyy
121 --> yyy
122
123 Program source
124
125 #include <stdio.h>
126 #include <stdlib.h>
127 #include <string.h>
128
129 int
130 main(int argc, char *argv[])
131 {
132 char *str1, *str2, *token, *subtoken;
133 char *saveptr1, *saveptr2;
134 int j;
135
136 if (argc != 4) {
137 fprintf(stderr, "Usage: %s string delim subdelim\n",
138 argv[0]);
139 exit(EXIT_FAILURE);
140 }
141
142 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
143 token = strtok_r(str1, argv[2], &saveptr1);
144 if (token == NULL)
145 break;
146 printf("%d: %s\n", j, token);
147
148 for (str2 = token; ; str2 = NULL) {
149 subtoken = strtok_r(str2, argv[3], &saveptr2);
150 if (subtoken == NULL)
151 break;
152 printf(" --> %s\n", subtoken);
153 }
154 }
155
156 exit(EXIT_SUCCESS);
157 }
158
159 Another example program using strtok() can be found in getad‐
160 drinfo_a(3).
161
163 index(3), memchr(3), rindex(3), strchr(3), string(3), strpbrk(3),
164 strsep(3), strspn(3), strstr(3), wcstok(3)
165
167 This page is part of release 3.53 of the Linux man-pages project. A
168 description of the project, and information about reporting bugs, can
169 be found at http://www.kernel.org/doc/man-pages/.
170
171
172
173GNU 2013-05-19 STRTOK(3)