1STRTOK(3P)                 POSIX Programmer's Manual                STRTOK(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       strtok, strtok_r - split string into tokens
13

SYNOPSIS

15       #include <string.h>
16
17       char *strtok(char *restrict s1, const char *restrict s2);
18
19
20       char *strtok_r(char *restrict s, const char *restrict sep,
21              char **restrict lasts);
22
23

DESCRIPTION

25       For strtok():   The functionality described on this reference  page  is
26       aligned  with the ISO C standard. Any conflict between the requirements
27       described here and the ISO C standard is unintentional. This volume  of
28       IEEE Std 1003.1-2001 defers to the ISO C standard.
29
30       A sequence of calls to strtok() breaks the string pointed to by s1 into
31       a sequence of tokens, each of which is delimited by  a  byte  from  the
32       string  pointed  to by s2. The first call in the sequence has s1 as its
33       first argument, and is followed by calls with a null pointer  as  their
34       first argument.  The separator string pointed to by s2 may be different
35       from call to call.
36
37       The first call in the sequence searches the string pointed to by s1 for
38       the  first  byte  that is not contained in the current separator string
39       pointed to by s2. If no such byte is found, then there are no tokens in
40       the  string  pointed to by s1 and strtok() shall return a null pointer.
41       If such a byte is found, it is the start of the first token.
42
43       The strtok() function then searches from there for a byte that is  con‐
44       tained  in  the current separator string. If no such byte is found, the
45       current token extends to the end of the string pointed to  by  s1,  and
46       subsequent  searches for a token shall return a null pointer. If such a
47       byte is found, it is overwritten by a null byte, which  terminates  the
48       current  token.  The strtok() function saves a pointer to the following
49       byte, from which the next search for a token shall start.
50
51       Each subsequent call, with a null pointer as the  value  of  the  first
52       argument,  starts  searching  from  the  saved  pointer  and behaves as
53       described above.
54
55       The implementation shall behave as if no function defined in this  vol‐
56       ume of IEEE Std 1003.1-2001 calls strtok().
57
58       The  strtok()  function  need  not be reentrant. A function that is not
59       required to be reentrant is not required to be thread-safe.
60
61       The strtok_r() function considers the null-terminated  string  s  as  a
62       sequence  of zero or more text tokens separated by spans of one or more
63       characters from the separator string sep.  The argument lasts points to
64       a  user-provided  pointer  which points to stored information necessary
65       for strtok_r() to continue scanning the same string.
66
67       In the first call to strtok_r(), s points to a null-terminated  string,
68       sep  to a null-terminated string of separator characters, and the value
69       pointed to by lasts is ignored.  The strtok_r() function shall return a
70       pointer to the first character of the first token, write a null charac‐
71       ter into s immediately following the returned  token,  and  update  the
72       pointer to which lasts points.
73
74       In  subsequent  calls, s is a NULL pointer and lasts shall be unchanged
75       from the previous call so that subsequent calls shall move through  the
76       string s, returning successive tokens until no tokens remain. The sepa‐
77       rator string sep may be different from call  to  call.  When  no  token
78       remains in s, a NULL pointer shall be returned.
79

RETURN VALUE

81       Upon  successful  completion,  strtok()  shall  return a pointer to the
82       first byte of a token. Otherwise, if there is no token, strtok()  shall
83       return a null pointer.
84
85       The strtok_r() function shall return a pointer to the token found, or a
86       NULL pointer when no token is found.
87

ERRORS

89       No errors are defined.
90
91       The following sections are informative.
92

EXAMPLES

94   Searching for Word Separators
95       The following example searches for tokens separated by <space>s.
96
97
98              #include <string.h>
99              ...
100              char *token;
101              char *line = "LINE TO BE SEPARATED";
102              char *search = " ";
103
104
105              /* Token will point to "LINE". */
106              token = strtok(line, search);
107
108
109              /* Token will point to "TO". */
110              token = strtok(NULL, search);
111
112   Breaking a Line
113       The following example uses strtok() to break a line into two  character
114       strings  separated  by  any  combination  of <space>s, <tab>s, or <new‐
115       line>s.
116
117
118              #include <string.h>
119              ...
120              struct element {
121                  char *key;
122                  char *data;
123              };
124              ...
125              char line[LINE_MAX];
126              char *key, *data;
127              ...
128              key = strtok(line, "    \n");
129              data = strtok(NULL, "   \n");
130              ...
131

APPLICATION USAGE

133       The strtok_r() function is thread-safe and stores its state in a  user-
134       supplied  buffer  instead of possibly using a static data area that may
135       be overwritten by an unrelated call from another thread.
136

RATIONALE

138       The strtok() function searches for a separator string within  a  larger
139       string.  It  returns  a pointer to the last substring between separator
140       strings. This function uses static storage to keep track of the current
141       string  position  between calls. The new function, strtok_r(), takes an
142       additional argument, lasts, to keep track of the  current  position  in
143       the string.
144

FUTURE DIRECTIONS

146       None.
147

SEE ALSO

149       The Base Definitions volume of IEEE Std 1003.1-2001, <string.h>
150
152       Portions  of  this text are reprinted and reproduced in electronic form
153       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
154       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
155       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
156       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
157       event of any discrepancy between this version and the original IEEE and
158       The  Open Group Standard, the original IEEE and The Open Group Standard
159       is the referee document. The original Standard can be  obtained  online
160       at http://www.opengroup.org/unix/online.html .
161
162
163
164IEEE/The Open Group                  2003                           STRTOK(3P)
Impressum