1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
'\" et
.TH STRTOK "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.SH NAME
strtok,
strtok_r
\(em split string into tokens
.SH SYNOPSIS
.LP
.nf
#include <string.h>
.P
char *strtok(char *restrict \fIs1\fP, const char *restrict \fIs2\fP);
char *strtok_r(char *restrict \fIs\fP, const char *restrict \fIsep\fP,
char **restrict \fIlasts\fP);
.fi
.SH DESCRIPTION
For
\fIstrtok\fR():
The functionality described on this reference page is aligned with the
ISO\ C standard. Any conflict between the requirements described here and the
ISO\ C standard is unintentional. This volume of POSIX.1\(hy2008 defers to the ISO\ C standard.
.P
A sequence of calls to
\fIstrtok\fR()
breaks the string pointed to by
.IR s1
into a sequence of tokens, each of which is delimited by a byte from
the string pointed to by
.IR s2 .
The first call in the sequence has
.IR s1
as its first argument, and is followed by calls with a null pointer as
their first argument. The separator string pointed to by
.IR s2
may be different from call to call.
.P
The first call in the sequence searches the string pointed to by
.IR s1
for the first byte that is
.IR not
contained in the current separator string pointed to by
.IR s2 .
If no such byte is found, then there are no tokens in the string
pointed to by
.IR s1
and
\fIstrtok\fR()
shall return a null pointer. If such a byte is found, it is the
start of the first token.
.P
The
\fIstrtok\fR()
function then searches from there for a byte that
.IR is
contained in the current separator string. If no such byte is found,
the current token extends to the end of the string pointed to by
.IR s1 ,
and subsequent searches for a token shall return a null pointer. If
such a byte is found, it is overwritten by a NUL character, which
terminates the current token. The
\fIstrtok\fR()
function saves a pointer to the following byte, from which the next
search for a token shall start.
.P
Each subsequent call, with a null pointer as the value of the first
argument, starts searching from the saved pointer and behaves as
described above.
.P
The implementation shall behave as if no function defined in this volume of POSIX.1\(hy2008
calls
\fIstrtok\fR().
.P
The
\fIstrtok\fR()
function need not be thread-safe.
.P
The
\fIstrtok_r\fR()
function considers the null-terminated string
.IR s
as a sequence of zero or more text tokens separated by spans of one or
more characters from the separator string
.IR sep .
The argument
.IR lasts
points to a user-provided pointer which points to stored information
necessary for
\fIstrtok_r\fR()
to continue scanning the same string.
.P
In the first call to
\fIstrtok_r\fR(),
.IR s
points to a null-terminated string,
.IR sep
to a null-terminated string of separator characters, and the value
pointed to by
.IR lasts
is ignored. The
\fIstrtok_r\fR()
function shall return a pointer to the first character of the first
token, write a null character into
.IR s
immediately following the returned token, and update the pointer to
which
.IR lasts
points.
.P
In subsequent calls,
.IR s
is a null pointer and
.IR lasts
shall be unchanged from the previous call so that subsequent calls
shall move through the string
.IR s ,
returning successive tokens until no tokens remain. The separator
string
.IR sep
may be different from call to call. When no token remains in
.IR s ,
a null pointer shall be returned.
.SH "RETURN VALUE"
Upon successful completion,
\fIstrtok\fR()
shall return a pointer to the first byte of a token. Otherwise,
if there is no token,
\fIstrtok\fR()
shall return a null pointer.
.P
The
\fIstrtok_r\fR()
function shall return a pointer to the token found, or a null pointer
when no token is found.
.SH ERRORS
No errors are defined.
.LP
.IR "The following sections are informative."
.SH EXAMPLES
.SS "Searching for Word Separators"
.P
The following example searches for tokens separated by
<space>
characters.
.sp
.RS 4
.nf
\fB
#include <string.h>
\&...
char *token;
char line[] = "LINE TO BE SEPARATED";
char *search = " ";
.P
/* Token will point to "LINE". */
token = strtok(line, search);
.P
/* Token will point to "TO". */
token = strtok(NULL, search);
.fi \fR
.P
.RE
.SS "Find First two Fields in a Buffer"
.P
The following example uses
\fIstrtok\fR()
to find two character strings (a key and data associated with that key)
separated by any combination of
<space>,
<tab>,
or
<newline>
characters at the start of the array of characters pointed to by
.IR buffer .
.sp
.RS 4
.nf
\fB
#include <string.h>
\&...
char *buffer;
\&...
struct element {
char *key;
char *data;
} e;
\&...
// Load the buffer...
\&...
// Get the key and its data...
e.key = strtok(buffer, " \et\en");
e.data = strtok(NULL, " \et\en");
// Process the rest of the contents of the buffer...
\&...
.fi \fR
.P
.RE
.SH "APPLICATION USAGE"
The
\fIstrtok_r\fR()
function is thread-safe and stores its state in a user-supplied buffer
instead of possibly using a static data area that may be overwritten
by an unrelated call from another thread.
.SH RATIONALE
The
\fIstrtok\fR()
function searches for a separator string within a larger string. It
returns a pointer to the last substring between separator strings.
This function uses static storage to keep track of the current string
position between calls. The new function,
\fIstrtok_r\fR(),
takes an additional argument,
.IR lasts ,
to keep track of the current position in the string.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
The Base Definitions volume of POSIX.1\(hy2008,
.IR "\fB<string.h>\fP"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.unix.org/online.html .
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
|