-.\" Copyright (C) 1996 Andries Brouwer (aeb@cwi.nl)
-.\" and Copyright (C) 2005 Michael Kerrisk (mtk.manpages@gmail.com)
+.\" Copyright (C) 2005, 2013 Michael Kerrisk (mtk.manpages@gmail.com)
+.\" a few fragments from an earlier (1996) version by
+.\" Andries Brouwer (aeb@cwi.nl) remain.
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" %%%LICENSE_END
.\"
.\" Rewritten old page, 960210, aeb@cwi.nl
-.\" Updated, added strtok_r. 2000-02-13 Nicolás Lichtmaier <nick@debian.org>
+.\" Updated, added strtok_r. 2000-02-13 Nicolás Lichtmaier <nick@debian.org>
.\" 2005-11-17, mtk: Substantial parts rewritten
+.\" 2013-05-19, mtk: added much further detail on the operation of strtok()
.\"
-.TH STRTOK 3 2012-05-10 "GNU" "Linux Programmer's Manual"
+.TH STRTOK 3 2013-05-19 "GNU" "Linux Programmer's Manual"
.SH NAME
strtok, strtok_r \- extract tokens from strings
.SH SYNOPSIS
.SH DESCRIPTION
The
.BR strtok ()
-function parses a string into a sequence of tokens.
+function breaks a string into a sequence of zero or more nonempty tokens.
On the first call to
.BR strtok ()
the string to be parsed should be
-specified in \fIstr\fP.
+specified in
+.IR str .
In each subsequent call that should parse the same string,
-\fIstr\fP should be NULL.
+.I str
+must be NULL.
-The \fIdelim\fP argument specifies a set of bytes that
+The
+.I delim
+argument specifies a set of bytes that
delimit the tokens in the parsed string.
-The caller may specify different strings in \fIdelim\fP in successive
+The caller may specify different strings in
+.I delim
+in successive
calls that parse the same string.
Each call to
.BR strtok ()
returns NULL.
-A sequence of two or more contiguous delimiter bytes in
-the parsed string is considered to be a single delimiter.
-Delimiter bytes at the start or end of the string are ignored.
+A sequence of calls to
+.BR strtok ()
+that operate on the same string maintains a pointer
+that determines the point from which to start searching for the next token.
+The first call to
+.BR strtok ()
+sets this pointer to point to the first byte of the string.
+The start of the next token is determined by scanning forward
+for the next nondelimiter byte in
+.IR str .
+If such a byte is found, it is taken as the start of the next token.
+If no such byte is found,
+then there are no more tokens, and
+.BR strtok ()
+returns NULL.
+(A string that is empty or that contains only delimiters
+will thus cause
+.BR strtok ()
+to return NULL on the first call.)
+
+The end of each token is found by scanning forward until either
+the next delimiter byte is found or until the
+terminating null byte (\(aq\\0\(aq) is encountered.
+If a delimiter byte is found, it is overwritten with
+a null byte to terminate the current token, and
+.BR strtok ()
+saves a pointer to the following byte;
+that pointer will be used as the starting point
+when searching for the next token.
+In this case,
+.BR strtok ()
+returns a pointer to the start of the found token.
+
+From the above description,
+it follows that a sequence of two or more contiguous delimiter bytes in
+the parsed string is considered to be a single delimiter, and that
+delimiter bytes at the start or end of the string are ignored.
Put another way: the tokens returned by
.BR strtok ()
are always nonempty strings.
+Thus, for example, given the string "\fIaaa;;bbb,\fP",
+successive calls to
+.BR strtok ()
+that specify the delimiter string "\fI;,\fP"
+would return the strings "\fIaaa\fP" and "\fIbbb\fP",
+and then a null pointer.
The
.BR strtok_r ()
function is a reentrant version
.BR strtok ().
-The \fIsaveptr\fP argument is a pointer to a
-\fIchar *\fP variable that is used internally by
+The
+.I saveptr
+argument is a pointer to a
+.IR "char\ *"
+variable that is used internally by
.BR strtok_r ()
in order to maintain context between successive calls that parse the
same string.
should point to the string to be parsed, and the value of
.I saveptr
is ignored.
-In subsequent calls, \fIstr\fP should be NULL, and
-\fIsaveptr\fP should be unchanged since the previous call.
+In subsequent calls,
+.I str
+should be NULL, and
+.I saveptr
+should be unchanged since the previous call.
Different strings may be parsed concurrently using sequences of calls to
.BR strtok_r ()
-that specify different \fIsaveptr\fP arguments.
+that specify different
+.I saveptr
+arguments.
.SH RETURN VALUE
The
.BR strtok ()
.BR strtok_r ()
functions return a pointer to
the next token, or NULL if there are no more tokens.
+.SH ATTRIBUTES
+.SS Multithreading (see pthreads(7))
+The
+.BR strtok ()
+function is not thread-safe.
+.LP
+The
+.BR strtok_r ()
+function is thread-safe.
.SH CONFORMING TO
.TP
.BR strtok ()
.BR strspn (3),
.BR strstr (3),
.BR wcstok (3)
+.SH COLOPHON
+This page is part of release 3.79 of the Linux
+.I man-pages
+project.
+A description of the project,
+information about reporting bugs,
+and the latest version of this page,
+can be found at
+\%http://www.kernel.org/doc/man\-pages/.