Files
oldlinux-files/study/Ref-docs/POSIX/susv3/functions/strtok.html
2024-02-19 00:25:23 -05:00

230 lines
9.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>strtok</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="strtok"></a> <a name="tag_03_742"></a><!-- strtok -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_742_01"></a>NAME</h4>
<blockquote>strtok, strtok_r - split string into tokens</blockquote>
<h4><a name="tag_03_742_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/string.h.html">string.h</a>&gt;<br>
<br>
char *strtok(char *restrict</tt> <i>s1</i><tt>, const char *restrict</tt> <i>s2</i><tt>);<br>
</tt></code></p>
<div class="box"><code><tt><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> char *strtok_r(char *restrict</tt> <i>s</i><tt>, const char *restrict</tt> <i>sep</i><tt>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char **restrict</tt> <i>lasts</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]"
border="0"></tt></code></div>
<tt><br>
</tt></blockquote>
<h4><a name="tag_03_742_03"></a>DESCRIPTION</h4>
<blockquote>
<p>For <i>strtok</i>(): <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any
conflict between the requirements described here and the ISO&nbsp;C standard is unintentional. This volume of
IEEE&nbsp;Std&nbsp;1003.1-2001 defers to the ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border=
"0"></p>
<p>A sequence of calls to <i>strtok</i>() breaks the string pointed to by <i>s1</i> into a sequence of tokens, each of which is
delimited by a byte from the string pointed to by <i>s2</i>. The first call in the sequence has <i>s1</i> as its first argument,
and is followed by calls with a null pointer as their first argument. The separator string pointed to by <i>s2</i> may be different
from call to call.</p>
<p>The first call in the sequence searches the string pointed to by <i>s1</i> for the first byte that is <i>not</i> contained in
the current separator string pointed to by <i>s2</i>. If no such byte is found, then there are no tokens in the string pointed to
by <i>s1</i> and <i>strtok</i>() shall return a null pointer. If such a byte is found, it is the start of the first token.</p>
<p>The <i>strtok</i>() function then searches from there for a byte that <i>is</i> 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 <i>s1</i>, and subsequent searches for a token
shall return a null pointer. If such a byte is found, it is overwritten by a null byte, which terminates the current token. The
<i>strtok</i>() function saves a pointer to the following byte, from which the next search for a token shall start.</p>
<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>
<p>The implementation shall behave as if no function defined in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 calls
<i>strtok</i>().</p>
<p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The
<i>strtok</i>() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.
<img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
<p><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The <i>strtok_r</i>() function considers the null-terminated string <i>s</i> as a sequence of zero or more text tokens separated by
spans of one or more characters from the separator string <i>sep</i>. The argument <i>lasts</i> points to a user-provided pointer
which points to stored information necessary for <i>strtok_r</i>() to continue scanning the same string.</p>
<p>In the first call to <i>strtok_r</i>(), <i>s</i> points to a null-terminated string, <i>sep</i> to a null-terminated string of
separator characters, and the value pointed to by <i>lasts</i> is ignored. The <i>strtok_r</i>() function shall return a pointer to
the first character of the first token, write a null character into <i>s</i> immediately following the returned token, and update
the pointer to which <i>lasts</i> points.</p>
<p>In subsequent calls, <i>s</i> is a NULL pointer and <i>lasts</i> shall be unchanged from the previous call so that subsequent
calls shall move through the string <i>s</i>, returning successive tokens until no tokens remain. The separator string <i>sep</i>
may be different from call to call. When no token remains in <i>s</i>, a NULL pointer shall be returned. <img src=
"../images/opt-end.gif" alt="[Option End]" border="0"></p>
</blockquote>
<h4><a name="tag_03_742_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>strtok</i>() shall return a pointer to the first byte of a token. Otherwise, if there is no
token, <i>strtok</i>() shall return a null pointer.</p>
<p><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
The <i>strtok_r</i>() function shall return a pointer to the token found, or a NULL pointer when no token is found. <img src=
"../images/opt-end.gif" alt="[Option End]" border="0"></p>
</blockquote>
<h4><a name="tag_03_742_05"></a>ERRORS</h4>
<blockquote>
<p>No errors are defined.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_742_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_742_06_01"></a>Searching for Word Separators</h5>
<p>The following example searches for tokens separated by &lt;space&gt;s.</p>
<pre>
<tt>#include &lt;string.h&gt;
...
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
<br>
/* Token will point to "LINE". */
token = strtok(line, search);
<br>
/* Token will point to "TO". */
token = strtok(NULL, search);
</tt>
</pre>
<h5><a name="tag_03_742_06_02"></a>Breaking a Line</h5>
<p>The following example uses <i>strtok</i>() to break a line into two character strings separated by any combination of
&lt;space&gt;s, &lt;tab&gt;s, or &lt;newline&gt;s.</p>
<pre>
<tt>#include &lt;string.h&gt;
...
struct element {
char *key;
char *data;
};
...
char line[LINE_MAX];
char *key, *data;
...
key = strtok(line, " \n");
data = strtok(NULL, " \n");
...
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_742_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>The <i>strtok_r</i>() 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.</p>
</blockquote>
<h4><a name="tag_03_742_08"></a>RATIONALE</h4>
<blockquote>
<p>The <i>strtok</i>() 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, <i>strtok_r</i>(), takes an additional argument, <i>lasts</i>, to keep track of the current position in the string.</p>
</blockquote>
<h4><a name="tag_03_742_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_742_10"></a>SEE ALSO</h4>
<blockquote>
<p>The Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/string.h.html"><i>&lt;string.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_742_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
</blockquote>
<h4><a name="tag_03_742_12"></a>Issue 5</h4>
<blockquote>
<p>The <i>strtok_r</i>() function is included for alignment with the POSIX Threads Extension.</p>
<p>A note indicating that the <i>strtok</i>() function need not be reentrant is added to the DESCRIPTION.</p>
</blockquote>
<h4><a name="tag_03_742_13"></a>Issue 6</h4>
<blockquote>
<p>Extensions beyond the ISO&nbsp;C standard are marked.</p>
<p>The <i>strtok_r</i>() function is marked as part of the Thread-Safe Functions option.</p>
<p>In the DESCRIPTION, the note about reentrancy is expanded to cover thread-safety.</p>
<p>The APPLICATION USAGE section is updated to include a note on the thread-safe function and its avoidance of possibly using a
static data area.</p>
<p>The <b>restrict</b> keyword is added to the <i>strtok</i>() and <i>strtok_r</i>() prototypes for alignment with the
ISO/IEC&nbsp;9899:1999 standard.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>