OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / strtok.3
index e4da37c..7b6f5dc 100644 (file)
@@ -1,6 +1,8 @@
-.\" 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
 .\" manual provided the copyright notice and this permission notice are
 .\" preserved on all copies.
 .\"
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%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  2008-10-29 "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
@@ -50,41 +54,90 @@ _XOPEN_SOURCE || _POSIX_SOURCE
 .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 characters 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 a pointer to a
 null-terminated string containing the next token.
-This string does not include the delimiting character.
+This string does not include the delimiting byte.
 If no more tokens are found,
 .BR strtok ()
 returns NULL.
 
-A sequence of two or more contiguous delimiter characters in
-the parsed string is considered to be a single delimiter.
-Delimiter characters 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.
@@ -95,20 +148,34 @@ On the first call to
 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.
-.SH "RETURN VALUE"
+that specify different
+.I saveptr
+arguments.
+.SH RETURN VALUE
 The
 .BR strtok ()
 and
 .BR strtok_r ()
 functions return a pointer to
 the next token, or NULL if there are no more tokens.
-.SH "CONFORMING TO"
+.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 ()
 SVr4, POSIX.1-2001, 4.3BSD, C89, C99.
@@ -123,7 +190,7 @@ These functions modify their first argument.
 .IP *
 These functions cannot be used on constant strings.
 .IP *
-The identity of the delimiting character is lost.
+The identity of the delimiting byte is lost.
 .IP *
 The
 .BR strtok ()
@@ -136,9 +203,9 @@ The program below uses nested loops that employ
 .BR strtok_r ()
 to break a string into a two-level hierarchy of tokens.
 The first command-line argument specifies the string to be parsed.
-The second argument specifies the delimiter character(s)
+The second argument specifies the delimiter byte(s)
 to be used to separate that string into "major" tokens.
-The third argument specifies the delimiter character(s)
+The third argument specifies the delimiter byte(s)
 to be used to separate the "major" tokens into subtokens.
 .PP
 An example of the output produced by this program is the following:
@@ -191,15 +258,30 @@ main(int argc, char *argv[])
     }
 
     exit(EXIT_SUCCESS);
-} /* main */
+}
 .fi
-.SH "SEE ALSO"
+.PP
+Another example program using
+.BR strtok ()
+can be found in
+.BR getaddrinfo_a (3).
+.SH SEE ALSO
 .BR index (3),
 .BR memchr (3),
 .BR rindex (3),
 .BR strchr (3),
+.BR string (3),
 .BR strpbrk (3),
 .BR strsep (3),
 .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/.