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 *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

DESCRIPTION

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 (but see NOTES).  In sub‐
68       sequent calls, str should be NULL, and saveptr (and the buffer that  it
69       points to) should be unchanged since the previous call.
70
71       Different  strings  may be parsed concurrently using sequences of calls
72       to strtok_r() that specify different saveptr arguments.
73

RETURN VALUE

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

ATTRIBUTES

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

CONFORMING TO

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

NOTES

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

BUGS

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

EXAMPLE

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

SEE ALSO

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

COLOPHON

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