1 .\" Copyright (C) 1996 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2005 Michael Kerrisk (mtk.manpages@gmail.com)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" Rewritten old page, 960210, aeb@cwi.nl
25 .\" Updated, added strtok_r. 2000-02-13 Nicolás Lichtmaier <nick@debian.org>
26 .\" 2005-11-17, mtk: Substantial parts rewritten
28 .TH STRTOK 3 2010-09-27 "GNU" "Linux Programmer's Manual"
30 strtok, strtok_r \- extract tokens from strings
33 .B #include <string.h>
35 .BI "char *strtok(char *" str ", const char *" delim );
37 .BI "char *strtok_r(char *" str ", const char *" delim ", char **" saveptr );
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
47 _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
48 _XOPEN_SOURCE || _POSIX_SOURCE
53 function parses a string into a sequence of tokens.
56 the string to be parsed should be
57 specified in \fIstr\fP.
58 In each subsequent call that should parse the same string,
59 \fIstr\fP should be NULL.
61 The \fIdelim\fP argument specifies a set of characters that
62 delimit the tokens in the parsed string.
63 The caller may specify different strings in \fIdelim\fP in successive
64 calls that parse the same string.
68 returns a pointer to a
69 null-terminated string containing the next token.
70 This string does not include the delimiting character.
71 If no more tokens are found,
75 A sequence of two or more contiguous delimiter characters in
76 the parsed string is considered to be a single delimiter.
77 Delimiter characters at the start or end of the string are ignored.
78 Put another way: the tokens returned by
80 are always nonempty strings.
84 function is a reentrant version
86 The \fIsaveptr\fP argument is a pointer to a
87 \fIchar *\fP variable that is used internally by
89 in order to maintain context between successive calls that parse the
95 should point to the string to be parsed, and the value of
98 In subsequent calls, \fIstr\fP should be NULL, and
99 \fIsaveptr\fP should be unchanged since the previous call.
101 Different strings may be parsed concurrently using sequences of calls to
103 that specify different \fIsaveptr\fP arguments.
109 functions return a pointer to
110 the next token, or NULL if there are no more tokens.
114 SVr4, POSIX.1-2001, 4.3BSD, C89, C99.
119 Be cautious when using these functions.
120 If you do use them, note that:
122 These functions modify their first argument.
124 These functions cannot be used on constant strings.
126 The identity of the delimiting character is lost.
130 function uses a static buffer while parsing, so it's not thread safe.
133 if this matters to you.
135 The program below uses nested loops that employ
137 to break a string into a two-level hierarchy of tokens.
138 The first command-line argument specifies the string to be parsed.
139 The second argument specifies the delimiter character(s)
140 to be used to separate that string into "major" tokens.
141 The third argument specifies the delimiter character(s)
142 to be used to separate the "major" tokens into subtokens.
144 An example of the output produced by this program is the following:
148 .RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
167 main(int argc, char *argv[])
169 char *str1, *str2, *token, *subtoken;
170 char *saveptr1, *saveptr2;
174 fprintf(stderr, "Usage: %s string delim subdelim\\n",
179 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
180 token = strtok_r(str1, argv[2], &saveptr1);
183 printf("%d: %s\\n", j, token);
185 for (str2 = token; ; str2 = NULL) {
186 subtoken = strtok_r(str2, argv[3], &saveptr2);
187 if (subtoken == NULL)
189 printf("\t \-\-> %s\\n", subtoken);
197 Another example program using
200 .BR getaddrinfo_a (3).