1STRTOK(3P) POSIX Programmer's Manual STRTOK(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 strtok, strtok_r — split string into tokens
14
16 #include <string.h>
17
18 char *strtok(char *restrict s1, const char *restrict s2);
19 char *strtok_r(char *restrict s, const char *restrict sep,
20 char **restrict lasts);
21
23 For strtok(): The functionality described on this reference page is
24 aligned with the ISO C standard. Any conflict between the requirements
25 described here and the ISO C standard is unintentional. This volume of
26 POSIX.1‐2008 defers to the ISO C standard.
27
28 A sequence of calls to strtok() breaks the string pointed to by s1 into
29 a sequence of tokens, each of which is delimited by a byte from the
30 string pointed to by s2. The first call in the sequence has s1 as its
31 first argument, and is followed by calls with a null pointer as their
32 first argument. The separator string pointed to by s2 may be different
33 from call to call.
34
35 The first call in the sequence searches the string pointed to by s1 for
36 the first byte that is not contained in the current separator string
37 pointed to by s2. If no such byte is found, then there are no tokens
38 in the string pointed to by s1 and strtok() shall return a null
39 pointer. If such a byte is found, it is the start of the first token.
40
41 The strtok() function then searches from there for a byte that is con‐
42 tained in the current separator string. If no such byte is found, the
43 current token extends to the end of the string pointed to by s1, and
44 subsequent searches for a token shall return a null pointer. If such a
45 byte is found, it is overwritten by a NUL character, which terminates
46 the current token. The strtok() function saves a pointer to the follow‐
47 ing byte, from which the next search for a token shall start.
48
49 Each subsequent call, with a null pointer as the value of the first
50 argument, starts searching from the saved pointer and behaves as
51 described above.
52
53 The implementation shall behave as if no function defined in this vol‐
54 ume of POSIX.1‐2008 calls strtok().
55
56 The strtok() function need not be thread-safe.
57
58 The strtok_r() function considers the null-terminated string s as a
59 sequence of zero or more text tokens separated by spans of one or more
60 characters from the separator string sep. The argument lasts points to
61 a user-provided pointer which points to stored information necessary
62 for strtok_r() to continue scanning the same string.
63
64 In the first call to strtok_r(), s points to a null-terminated string,
65 sep to a null-terminated string of separator characters, and the value
66 pointed to by lasts is ignored. The strtok_r() function shall return a
67 pointer to the first character of the first token, write a null charac‐
68 ter into s immediately following the returned token, and update the
69 pointer to which lasts points.
70
71 In subsequent calls, s is a null pointer and lasts shall be unchanged
72 from the previous call so that subsequent calls shall move through the
73 string s, returning successive tokens until no tokens remain. The sepa‐
74 rator string sep may be different from call to call. When no token
75 remains in s, a null pointer shall be returned.
76
78 Upon successful completion, strtok() shall return a pointer to the
79 first byte of a token. Otherwise, if there is no token, strtok() shall
80 return a null pointer.
81
82 The strtok_r() function shall return a pointer to the token found, or a
83 null pointer when no token is found.
84
86 No errors are defined.
87
88 The following sections are informative.
89
91 Searching for Word Separators
92 The following example searches for tokens separated by <space> charac‐
93 ters.
94
95 #include <string.h>
96 ...
97 char *token;
98 char line[] = "LINE TO BE SEPARATED";
99 char *search = " ";
100
101 /* Token will point to "LINE". */
102 token = strtok(line, search);
103
104 /* Token will point to "TO". */
105 token = strtok(NULL, search);
106
107 Find First two Fields in a Buffer
108 The following example uses strtok() to find two character strings (a
109 key and data associated with that key) separated by any combination of
110 <space>, <tab>, or <newline> characters at the start of the array of
111 characters pointed to by buffer.
112
113 #include <string.h>
114 ...
115 char *buffer;
116 ...
117 struct element {
118 char *key;
119 char *data;
120 } e;
121 ...
122 // Load the buffer...
123 ...
124 // Get the key and its data...
125 e.key = strtok(buffer, " \t\n");
126 e.data = strtok(NULL, " \t\n");
127 // Process the rest of the contents of the buffer...
128 ...
129
131 The strtok_r() function is thread-safe and stores its state in a user-
132 supplied buffer instead of possibly using a static data area that may
133 be overwritten by an unrelated call from another thread.
134
136 The strtok() function searches for a separator string within a larger
137 string. It returns a pointer to the last substring between separator
138 strings. This function uses static storage to keep track of the cur‐
139 rent string position between calls. The new function, strtok_r(), takes
140 an additional argument, lasts, to keep track of the current position in
141 the string.
142
144 None.
145
147 The Base Definitions volume of POSIX.1‐2008, <string.h>
148
150 Portions of this text are reprinted and reproduced in electronic form
151 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
152 -- Portable Operating System Interface (POSIX), The Open Group Base
153 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
154 cal and Electronics Engineers, Inc and The Open Group. (This is
155 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
156 event of any discrepancy between this version and the original IEEE and
157 The Open Group Standard, the original IEEE and The Open Group Standard
158 is the referee document. The original Standard can be obtained online
159 at http://www.unix.org/online.html .
160
161 Any typographical or formatting errors that appear in this page are
162 most likely to have been introduced during the conversion of the source
163 files to man page format. To report such errors, see https://www.ker‐
164 nel.org/doc/man-pages/reporting_bugs.html .
165
166
167
168IEEE/The Open Group 2013 STRTOK(3P)