1STRTOK(P) POSIX Programmer's Manual STRTOK(P)
2
3
4
6 strtok, strtok_r - split string into tokens
7
9 #include <string.h>
10
11 char *strtok(char *restrict s1, const char *restrict s2);
12
13
14 char *strtok_r(char *restrict s, const char *restrict sep,
15 char **restrict lasts);
16
17
19 For strtok(): The functionality described on this reference page is
20 aligned with the ISO C standard. Any conflict between the requirements
21 described here and the ISO C standard is unintentional. This volume of
22 IEEE Std 1003.1-2001 defers to the ISO C standard.
23
24 A sequence of calls to strtok() breaks the string pointed to by s1 into
25 a sequence of tokens, each of which is delimited by a byte from the
26 string pointed to by s2. The first call in the sequence has s1 as its
27 first argument, and is followed by calls with a null pointer as their
28 first argument. The separator string pointed to by s2 may be different
29 from call to call.
30
31 The first call in the sequence searches the string pointed to by s1 for
32 the first byte that is not contained in the current separator string
33 pointed to by s2. If no such byte is found, then there are no tokens in
34 the string pointed to by s1 and strtok() shall return a null pointer.
35 If such a byte is found, it is the start of the first token.
36
37 The strtok() function then searches from there for a byte that is con‐
38 tained in the current separator string. If no such byte is found, the
39 current token extends to the end of the string pointed to by s1, and
40 subsequent searches for a token shall return a null pointer. If such a
41 byte is found, it is overwritten by a null byte, which terminates the
42 current token. The strtok() function saves a pointer to the following
43 byte, from which the next search for a token shall start.
44
45 Each subsequent call, with a null pointer as the value of the first
46 argument, starts searching from the saved pointer and behaves as
47 described above.
48
49 The implementation shall behave as if no function defined in this vol‐
50 ume of IEEE Std 1003.1-2001 calls strtok().
51
52 The strtok() function need not be reentrant. A function that is not
53 required to be reentrant is not required to be thread-safe.
54
55 The strtok_r() function considers the null-terminated string s as a
56 sequence of zero or more text tokens separated by spans of one or more
57 characters from the separator string sep. The argument lasts points to
58 a user-provided pointer which points to stored information necessary
59 for strtok_r() to continue scanning the same string.
60
61 In the first call to strtok_r(), s points to a null-terminated string,
62 sep to a null-terminated string of separator characters, and the value
63 pointed to by lasts is ignored. The strtok_r() function shall return a
64 pointer to the first character of the first token, write a null charac‐
65 ter into s immediately following the returned token, and update the
66 pointer to which lasts points.
67
68 In subsequent calls, s is a NULL pointer and lasts shall be unchanged
69 from the previous call so that subsequent calls shall move through the
70 string s, returning successive tokens until no tokens remain. The sepa‐
71 rator string sep may be different from call to call. When no token
72 remains in s, a NULL pointer shall be returned.
73
75 Upon successful completion, strtok() shall return a pointer to the
76 first byte of a token. Otherwise, if there is no token, strtok() shall
77 return a null pointer.
78
79 The strtok_r() function shall return a pointer to the token found, or a
80 NULL pointer when no token is found.
81
83 No errors are defined.
84
85 The following sections are informative.
86
88 Searching for Word Separators
89 The following example searches for tokens separated by <space>s.
90
91
92 #include <string.h>
93 ...
94 char *token;
95 char *line = "LINE TO BE SEPARATED";
96 char *search = " ";
97
98
99 /* Token will point to "LINE". */
100 token = strtok(line, search);
101
102
103 /* Token will point to "TO". */
104 token = strtok(NULL, search);
105
106 Breaking a Line
107 The following example uses strtok() to break a line into two character
108 strings separated by any combination of <space>s, <tab>s, or <new‐
109 line>s.
110
111
112 #include <string.h>
113 ...
114 struct element {
115 char *key;
116 char *data;
117 };
118 ...
119 char line[LINE_MAX];
120 char *key, *data;
121 ...
122 key = strtok(line, " \n");
123 data = strtok(NULL, " \n");
124 ...
125
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 current
135 string position between calls. The new function, strtok_r(), takes an
136 additional argument, lasts, to keep track of the current position in
137 the string.
138
140 None.
141
143 The Base Definitions volume of IEEE Std 1003.1-2001, <string.h>
144
146 Portions of this text are reprinted and reproduced in electronic form
147 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
148 -- Portable Operating System Interface (POSIX), The Open Group Base
149 Specifications Issue 6, Copyright (C) 2001-2003 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
157
158IEEE/The Open Group 2003 STRTOK(P)