1STRTOK(3)                  Linux Programmer's Manual                 STRTOK(3)
2
3
4

NAME

6       strtok, strtok_r - extract tokens from strings
7

SYNOPSIS

9       #include <string.h>
10
11       char *strtok(char *restrict str, const char *restrict delim);
12       char *strtok_r(char *restrict str, const char *restrict delim,
13                      char **restrict saveptr);
14
15   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17       strtok_r():
18           _POSIX_C_SOURCE
19               || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
20

DESCRIPTION

22       The  strtok()  function breaks a string into a sequence of zero or more
23       nonempty tokens.  On the first call  to  strtok(),  the  string  to  be
24       parsed should be specified in str.  In each subsequent call that should
25       parse the same string, str must be NULL.
26
27       The delim argument specifies a set of bytes that delimit the tokens  in
28       the  parsed  string.  The caller may specify different strings in delim
29       in successive calls that parse the same string.
30
31       Each call to strtok() returns a pointer  to  a  null-terminated  string
32       containing the next token.  This string does not include the delimiting
33       byte.  If no more tokens are found, strtok() returns NULL.
34
35       A sequence of calls to strtok() that operate on the same  string  main‐
36       tains a pointer that determines the point from which to start searching
37       for the next token.  The first call to strtok() sets  this  pointer  to
38       point  to the first byte of the string.  The start of the next token is
39       determined by scanning forward for the next nondelimiter byte  in  str.
40       If  such  a  byte is found, it is taken as the start of the next token.
41       If no such byte is found, then there are no more tokens,  and  strtok()
42       returns NULL.  (A string that is empty or that contains only delimiters
43       will thus cause strtok() to return NULL on the first call.)
44
45       The end of each token is found by scanning  forward  until  either  the
46       next  delimiter byte is found or until the terminating null byte ('\0')
47       is encountered.  If a delimiter byte is found, it is overwritten with a
48       null  byte to terminate the current token, and strtok() saves a pointer
49       to the following byte; that pointer will be used as the starting  point
50       when  searching  for  the next token.  In this case, strtok() returns a
51       pointer to the start of the found token.
52
53       From the above description, it follows that a sequence of two  or  more
54       contiguous  delimiter  bytes in the parsed string is considered to be a
55       single delimiter, and that delimiter bytes at the start or end  of  the
56       string  are  ignored.  Put another way: the tokens returned by strtok()
57       are always nonempty strings.   Thus,  for  example,  given  the  string
58       "aaa;;bbb,",  successive  calls  to strtok() that specify the delimiter
59       string ";," would return the strings "aaa" and "bbb", and then  a  null
60       pointer.
61
62       The  strtok_r()  function  is  a  reentrant  version  of strtok().  The
63       saveptr argument is a pointer to a char * variable that is used  inter‐
64       nally  by  strtok_r()  in  order to maintain context between successive
65       calls that parse the same string.
66
67       On the first call to strtok_r(), str should point to the string  to  be
68       parsed,  and the value of *saveptr is ignored (but see NOTES).  In sub‐
69       sequent calls, str should be NULL, and saveptr (and the buffer that  it
70       points to) should be unchanged since the previous call.
71
72       Different  strings  may be parsed concurrently using sequences of calls
73       to strtok_r() that specify different saveptr arguments.
74

RETURN VALUE

76       The strtok() and strtok_r() functions return a pointer to the next  to‐
77       ken, or NULL if there are no more tokens.
78

ATTRIBUTES

80       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
81       tributes(7).
82
83       ┌──────────────────────────────┬───────────────┬───────────────────────┐
84Interface                     Attribute     Value                 
85       ├──────────────────────────────┼───────────────┼───────────────────────┤
86strtok()                      │ Thread safety │ MT-Unsafe race:strtok │
87       ├──────────────────────────────┼───────────────┼───────────────────────┤
88strtok_r()                    │ Thread safety │ MT-Safe               │
89       └──────────────────────────────┴───────────────┴───────────────────────┘
90

CONFORMING TO

92       strtok()
93              POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
94
95       strtok_r()
96              POSIX.1-2001, POSIX.1-2008.
97

NOTES

99       On some implementations, *saveptr is required to be NULL on  the  first
100       call to strtok_r() that is being used to parse str.
101

BUGS

103       Be cautious when using these functions.  If you do use them, note that:
104
105       * These functions modify their first argument.
106
107       * These functions cannot be used on constant strings.
108
109       * The identity of the delimiting byte is lost.
110
111       * The strtok() function uses a static buffer while parsing, so it's not
112         thread safe.  Use strtok_r() if this matters to you.
113

EXAMPLES

115       The program below uses nested loops that employ strtok_r() to  break  a
116       string  into  a  two-level hierarchy of tokens.  The first command-line
117       argument specifies the string to be parsed.  The second argument speci‐
118       fies the delimiter byte(s) to be used to separate that string into "ma‐
119       jor" tokens.  The third argument specifies the delimiter byte(s) to  be
120       used to separate the "major" tokens into subtokens.
121
122       An example of the output produced by this program is the following:
123
124           $ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
125           1: a/bbb///cc
126                    --> a
127                    --> bbb
128                    --> cc
129           2: xxx
130                    --> xxx
131           3: yyy
132                    --> yyy
133
134   Program source
135
136       #include <stdio.h>
137       #include <stdlib.h>
138       #include <string.h>
139
140       int
141       main(int argc, char *argv[])
142       {
143           char *str1, *str2, *token, *subtoken;
144           char *saveptr1, *saveptr2;
145
146           if (argc != 4) {
147               fprintf(stderr, "Usage: %s string delim subdelim\n",
148                       argv[0]);
149               exit(EXIT_FAILURE);
150           }
151
152           for (int j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
153               token = strtok_r(str1, argv[2], &saveptr1);
154               if (token == NULL)
155                   break;
156               printf("%d: %s\n", j, token);
157
158               for (str2 = token; ; str2 = NULL) {
159                   subtoken = strtok_r(str2, argv[3], &saveptr2);
160                   if (subtoken == NULL)
161                       break;
162                   printf(" --> %s\n", subtoken);
163               }
164           }
165
166           exit(EXIT_SUCCESS);
167       }
168
169       Another   example  program  using  strtok()  can  be  found  in  getad‐
170       drinfo_a(3).
171

SEE ALSO

173       index(3),  memchr(3),  rindex(3),  strchr(3),  string(3),   strpbrk(3),
174       strsep(3), strspn(3), strstr(3), wcstok(3)
175

COLOPHON

177       This  page  is  part of release 5.12 of the Linux man-pages project.  A
178       description of the project, information about reporting bugs,  and  the
179       latest     version     of     this    page,    can    be    found    at
180       https://www.kernel.org/doc/man-pages/.
181
182
183
184GNU                               2021-03-22                         STRTOK(3)
Impressum