OSDN Git Service

(split) LDP man-pages の original/ を v3.29 に更新。
[linuxjm/LDP_man-pages.git] / original / man3 / strtok.3
1 .\" Copyright (C) 1996 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2005 Michael Kerrisk (mtk.manpages@gmail.com)
3 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
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
27 .\"
28 .TH STRTOK 3  2010-09-27 "GNU" "Linux Programmer's Manual"
29 .SH NAME
30 strtok, strtok_r \- extract tokens from strings
31 .SH SYNOPSIS
32 .nf
33 .B #include <string.h>
34 .sp
35 .BI "char *strtok(char *" str ", const char *" delim );
36 .sp
37 .BI "char *strtok_r(char *" str ", const char *" delim ", char **" saveptr );
38 .fi
39 .sp
40 .in -4n
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
43 .in
44 .sp
45 .ad l
46 .BR strtok_r ():
47 _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
48 _XOPEN_SOURCE || _POSIX_SOURCE
49 .ad b
50 .SH DESCRIPTION
51 The
52 .BR strtok ()
53 function parses a string into a sequence of tokens.
54 On the first call to
55 .BR strtok ()
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.
60
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.
65
66 Each call to
67 .BR strtok ()
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,
72 .BR strtok ()
73 returns NULL.
74
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
79 .BR strtok ()
80 are always nonempty strings.
81
82 The
83 .BR strtok_r ()
84 function is a reentrant version
85 .BR strtok ().
86 The \fIsaveptr\fP argument is a pointer to a
87 \fIchar *\fP variable that is used internally by
88 .BR strtok_r ()
89 in order to maintain context between successive calls that parse the
90 same string.
91
92 On the first call to
93 .BR strtok_r (),
94 .I str
95 should point to the string to be parsed, and the value of
96 .I saveptr
97 is ignored.
98 In subsequent calls, \fIstr\fP should be NULL, and
99 \fIsaveptr\fP should be unchanged since the previous call.
100
101 Different strings may be parsed concurrently using sequences of calls to
102 .BR strtok_r ()
103 that specify different \fIsaveptr\fP arguments.
104 .SH "RETURN VALUE"
105 The
106 .BR strtok ()
107 and
108 .BR strtok_r ()
109 functions return a pointer to
110 the next token, or NULL if there are no more tokens.
111 .SH "CONFORMING TO"
112 .TP
113 .BR strtok ()
114 SVr4, POSIX.1-2001, 4.3BSD, C89, C99.
115 .TP
116 .BR strtok_r ()
117 POSIX.1-2001.
118 .SH BUGS
119 Be cautious when using these functions.
120 If you do use them, note that:
121 .IP * 2
122 These functions modify their first argument.
123 .IP *
124 These functions cannot be used on constant strings.
125 .IP *
126 The identity of the delimiting character is lost.
127 .IP *
128 The
129 .BR strtok ()
130 function uses a static buffer while parsing, so it's not thread safe.
131 Use
132 .BR strtok_r ()
133 if this matters to you.
134 .SH EXAMPLE
135 The program below uses nested loops that employ
136 .BR strtok_r ()
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.
143 .PP
144 An example of the output produced by this program is the following:
145 .PP
146 .in +4n
147 .nf
148 .RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
149 1: a/bbb///cc
150          \-\-> a
151          \-\-> bbb
152          \-\-> cc
153 2: xxx
154          \-\-> xxx
155 3: yyy
156          \-\-> yyy
157 .fi
158 .in
159 .SS Program source
160 \&
161 .nf
162 #include <stdio.h>
163 #include <stdlib.h>
164 #include <string.h>
165
166 int
167 main(int argc, char *argv[])
168 {
169     char *str1, *str2, *token, *subtoken;
170     char *saveptr1, *saveptr2;
171     int j;
172
173     if (argc != 4) {
174         fprintf(stderr, "Usage: %s string delim subdelim\\n",
175                 argv[0]);
176         exit(EXIT_FAILURE);
177     }
178
179     for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
180         token = strtok_r(str1, argv[2], &saveptr1);
181         if (token == NULL)
182             break;
183         printf("%d: %s\\n", j, token);
184
185         for (str2 = token; ; str2 = NULL) {
186             subtoken = strtok_r(str2, argv[3], &saveptr2);
187             if (subtoken == NULL)
188                 break;
189             printf("\t \-\-> %s\\n", subtoken);
190         }
191     }
192
193     exit(EXIT_SUCCESS);
194 }
195 .fi
196 .PP
197 Another example program using
198 .BR strtok ()
199 can be found in
200 .BR getaddrinfo_a (3).
201 .SH "SEE ALSO"
202 .BR index (3),
203 .BR memchr (3),
204 .BR rindex (3),
205 .BR strchr (3),
206 .BR string (3),
207 .BR strpbrk (3),
208 .BR strsep (3),
209 .BR strspn (3),
210 .BR strstr (3),
211 .BR wcstok (3)