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
12 strtok, strtok_r — split string into tokens
13
15 #include <string.h>
16
17 char *strtok(char *restrict s, const char *restrict sep);
18 char *strtok_r(char *restrict s, const char *restrict sep,
19 char **restrict state);
20
22 For strtok(): The functionality described on this reference page is
23 aligned with the ISO C standard. Any conflict between the requirements
24 described here and the ISO C standard is unintentional. This volume of
25 POSIX.1‐2017 defers to the ISO C standard.
26
27 A sequence of calls to strtok() breaks the string pointed to by s into
28 a sequence of tokens, each of which is delimited by a byte from the
29 string pointed to by sep. The first call in the sequence has s as its
30 first argument, and is followed by calls with a null pointer as their
31 first argument. The separator string pointed to by sep may be different
32 from call to call.
33
34 The first call in the sequence searches the string pointed to by s for
35 the first byte that is not contained in the current separator string
36 pointed to by sep. If no such byte is found, then there are no tokens
37 in the string pointed to by s and strtok() shall return a null pointer.
38 If such a byte is found, it is the start of the first token.
39
40 The strtok() function then searches from there for a byte that is con‐
41 tained in the current separator string. If no such byte is found, the
42 current token extends to the end of the string pointed to by s, and
43 subsequent searches for a token shall return a null pointer. If such a
44 byte is found, it is overwritten by a NUL character, which terminates
45 the current token. The strtok() function saves a pointer to the follow‐
46 ing byte, from which the next search for a token shall start.
47
48 Each subsequent call, with a null pointer as the value of the first
49 argument, starts searching from the saved pointer and behaves as
50 described above.
51
52 The implementation shall behave as if no function defined in this vol‐
53 ume of POSIX.1‐2017 calls strtok().
54
55 The strtok() function need not be thread-safe.
56
57 The strtok_r() function shall be equivalent to strtok(), except that
58 strtok_r() shall be thread-safe and the argument state points to a
59 user-provided pointer that allows strtok_r() to maintain state between
60 calls which scan the same string. The application shall ensure that the
61 pointer pointed to by state is unique for each string (s) being pro‐
62 cessed concurrently by strtok_r() calls. The application need not ini‐
63 tialize the pointer pointed to by state to any particular value. The
64 implementation shall not update the pointer pointed to by state to
65 point (directly or indirectly) to resources, other than within the
66 string s, that need to be freed or released by the caller.
67
69 Upon successful completion, strtok() shall return a pointer to the
70 first byte of a token. Otherwise, if there is no token, strtok() shall
71 return a null pointer.
72
73 The strtok_r() function shall return a pointer to the token found, or a
74 null pointer when no token is found.
75
77 No errors are defined.
78
79 The following sections are informative.
80
82 Searching for Word Separators
83 The following example searches for tokens separated by <space> charac‐
84 ters.
85
86
87 #include <string.h>
88 ...
89 char *token;
90 char line[] = "LINE TO BE SEPARATED";
91 char *search = " ";
92
93 /* Token will point to "LINE". */
94 token = strtok(line, search);
95
96 /* Token will point to "TO". */
97 token = strtok(NULL, search);
98
99 Find First two Fields in a Buffer
100 The following example uses strtok() to find two character strings (a
101 key and data associated with that key) separated by any combination of
102 <space>, <tab>, or <newline> characters at the start of the array of
103 characters pointed to by buffer.
104
105
106 #include <string.h>
107 ...
108 char *buffer;
109 ...
110 struct element {
111 char *key;
112 char *data;
113 } e;
114 ...
115 // Load the buffer...
116 ...
117 // Get the key and its data...
118 e.key = strtok(buffer, " \t\n");
119 e.data = strtok(NULL, " \t\n");
120 // Process the rest of the contents of the buffer...
121 ...
122
124 Note that if sep is the empty string, strtok() and strtok_r() return a
125 pointer to the remainder of the string being tokenized.
126
127 The strtok_r() function is thread-safe and stores its state in a user-
128 supplied buffer instead of possibly using a static data area that may
129 be overwritten by an unrelated call from another thread.
130
132 The strtok() function searches for a separator string within a larger
133 string. It returns a pointer to the last substring between separator
134 strings. This function uses static storage to keep track of the cur‐
135 rent string position between calls. The new function, strtok_r(), takes
136 an additional argument, state, to keep track of the current position in
137 the string.
138
140 None.
141
143 The Base Definitions volume of POSIX.1‐2017, <string.h>
144
146 Portions of this text are reprinted and reproduced in electronic form
147 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
148 table Operating System Interface (POSIX), The Open Group Base Specifi‐
149 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
150 Electrical and Electronics Engineers, Inc and The Open Group. In the
151 event of any discrepancy between this version and the original IEEE and
152 The Open Group Standard, the original IEEE and The Open Group Standard
153 is the referee document. The original Standard can be obtained online
154 at http://www.opengroup.org/unix/online.html .
155
156 Any typographical or formatting errors that appear in this page are
157 most likely to have been introduced during the conversion of the source
158 files to man page format. To report such errors, see https://www.ker‐
159 nel.org/doc/man-pages/reporting_bugs.html .
160
161
162
163IEEE/The Open Group 2017 STRTOK(3P)