OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / draft / 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 Nicolas Lichtmaier <nick@debian.org>
26 .\" 2005-11-17, mtk: Substantial parts rewritten
27 .\"
28 .\" Japanese Version Copyright (c) 1998 Ishii Tatsuo all rights reserved.
29 .\" Translated 1998-03-27, Ishii Tatsuo <rfun@azusa.shinshu-u.ac.jp>
30 .\" Updated 2000-04-05, Kentaro Shirakata <argrath@ub32.org>
31 .\" Updated 2000-09-21, Kentaro Shirakata
32 .\" Updated 2002-03-28, Kentaro Shirakata
33 .\" Updated 2005-11-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
34 .\"
35 .TH STRTOK 3  2008-10-29 "GNU" "Linux Programmer's Manual"
36 .\"O .SH NAME
37 .\"O strtok, strtok_r \- extract tokens from strings
38 .SH Ì¾Á°
39 strtok, strtok_r \- Ê¸»úÎ󤫤é¥È¡¼¥¯¥ó¤ò¼è¤ê½Ð¤¹
40 .\"O .SH SYNOPSIS
41 .SH ½ñ¼°
42 .nf
43 .B #include <string.h>
44 .sp
45 .BI "char *strtok(char *" str ", const char *" delim );
46 .sp
47 .BI "char *strtok_r(char *" str ", const char *" delim ", char **" saveptr );
48 .fi
49 .sp
50 .in -4n
51 .\"O Feature Test Macro Requirements for glibc (see
52 .\"O .BR feature_test_macros (7)):
53 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
54 .RB ( feature_test_macros (7)
55 »²¾È):
56 .in
57 .sp
58 .ad l
59 .BR strtok_r ():
60 _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
61 _XOPEN_SOURCE || _POSIX_SOURCE
62 .ad b
63 .\"O .SH DESCRIPTION
64 .SH ÀâÌÀ
65 .\"O The
66 .\"O .BR strtok ()
67 .\"O function parses a string into a sequence of tokens.
68 .\"O On the first call to
69 .\"O .BR strtok ()
70 .\"O the string to be parsed should be
71 .\"O specified in \fIstr\fP.
72 .\"O In each subsequent call that should parse the same string,
73 .\"O \fIstr\fP should be NULL.
74 .BR strtok ()
75 ´Ø¿ô¤Ïʸ»úÎó¤ò²òÀϤ·¤Æ¥È¡¼¥¯¥ó¤Ëʬ³ä¤¹¤ë¡£
76 .BR strtok ()
77 ¤òºÇ½é¤Ë¸Æ¤Ó½Ð¤¹ºÝ¤Ë¤Ï¡¢²òÀÏÂоݤÎʸ»úÎó¤ò \fIstr\fP ¤Ë
78 »ØÄꤹ¤ë¡£Æ±¤¸Ê¸»úÎó¤Î²òÀϤò¹Ô¤¦¤½¤Î¸å¤Î¸Æ¤Ó½Ð¤·¤Ç¤Ï¡¢
79 \fIstr\fP ¤Ë¤Ï NULL ¤ò»ØÄꤹ¤ë¡£
80
81 .\"O The \fIdelim\fP argument specifies a set of characters that
82 .\"O delimit the tokens in the parsed string.
83 .\"O The caller may specify different strings in \fIdelim\fP in successive
84 .\"O calls that parse the same string.
85 \fIdelim\fP °ú¤­¿ô¤Ë¤Ï¡¢²òÀϤ¹¤ëʸ»úÎó¤ò¥È¡¼¥¯¥ó¤Ë¶èÀÚ¤ëʸ»ú½¸¹ç¤ò
86 »ØÄꤹ¤ë¡£Æ±¤¸Ê¸»úÎó¤ò²òÀϤ¹¤ë°ìÏ¢¤Î¸Æ¤Ó½Ð¤·¤Ë¤ª¤¤¤Æ¡¢
87 \fIdelim\fP ¤Ë°ã¤¦Ê¸»úÎó¤ò»ØÄꤷ¤Æ¤â¤è¤¤¡£
88
89 .\"O Each call to
90 .\"O .BR strtok ()
91 .\"O returns a pointer to a
92 .\"O null-terminated string containing the next token.
93 .\"O This string does not include the delimiting character.
94 .\"O If no more tokens are found,
95 .\"O .BR strtok ()
96 .\"O returns NULL.
97 .BR strtok ()
98 ¤Î¤½¤ì¤¾¤ì¤Î¸Æ¤Ó½Ð¤·¤Ç¤Ï¡¢¼¡¤Î¥È¡¼¥¯¥ó¤ò
99 ³ÊǼ¤·¤¿ NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬ÊÖ¤µ¤ì¤ë¡£
100 ¤³¤Îʸ»úÎó¤Ë¤Ï¶èÀÚ¤êʸ»ú¤Ï´Þ¤Þ¤ì¤Ê¤¤¡£
101 ¤³¤ì°Ê¾å¥È¡¼¥¯¥ó¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë¤Ï¡¢NULL ¤¬ÊÖ¤µ¤ì¤ë¡£
102
103 .\"O A sequence of two or more contiguous delimiter characters in
104 .\"O the parsed string is considered to be a single delimiter.
105 .\"O Delimiter characters at the start or end of the string are ignored.
106 .\"O Put another way: the tokens returned by
107 .\"O .BR strtok ()
108 .\"O are always nonempty strings.
109 ²òÀÏÂоݤÎʸ»úÎó¤Ë2¤Ä°Ê¾å¤Î¶èÀÚ¤êʸ»ú¤¬Ï¢Â³¤·¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¡¢
110 °ì¤Ä¤Î¶èÀÚ¤êʸ»ú¤È¤ß¤Ê¤µ¤ì¤ë¡£
111 ʸ»úÎó¤ÎÀèƬ¤äËöÈø¤Ë¤¢¤ë¶èÀÚ¤êʸ»ú¤Ï̵»ë¤µ¤ì¤ë¡£¸À¤¤´¹¤¨¤ë¤È¡¢
112 .BR strtok ()
113 ¤¬ÊÖ¤¹¥È¡¼¥¯¥ó¤Ï¾ï¤Ë¶õ¤Ç¤Ê¤¤Ê¸»úÎó¤È¤Ê¤ë¡£
114
115 .\"O The
116 .\"O .BR strtok_r ()
117 .\"O function is a reentrant version
118 .\"O .BR strtok ().
119 .\"O The \fIsaveptr\fP argument is a pointer to a
120 .\"O \fIchar *\fP variable that is used internally by
121 .\"O .BR strtok_r ()
122 .\"O in order to maintain context between successive calls that parse the
123 .\"O same string.
124 .BR strtok_r ()
125 ´Ø¿ô¤Ï
126 .BR strtok ()
127 ¤Î¥ê¥¨¥ó¥È¥é¥ó¥ÈÈǤǤ¢¤ë¡£
128 \fIsaveptr\fP °ú¤­¿ô¤Ï \fIchar *\fP ÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ê¡¢
129 Ʊ¤¸Ê¸»úÎó¤Î²òÀϤò¹Ô¤¦
130 .BR strtok_r ()
131 ¤Î¸Æ¤Ó½Ð¤·´Ö¤Ç½èÍý¾õ¶·¤òÊݸ¤¹¤ë¤¿¤á¤Ë
132 .BR strtok_r ()
133 ÆâÉô¤Ç»ÈÍѤµ¤ì¤ë¡£
134
135 .\"O On the first call to
136 .\"O .BR strtok_r (),
137 .\"O .I str
138 .\"O should point to the string to be parsed, and the value of
139 .\"O .I saveptr
140 .\"O is ignored.
141 .\"O In subsequent calls, \fIstr\fP should be NULL, and
142 .\"O \fIsaveptr\fP should be unchanged since the previous call.
143 .BR strtok_r ()
144 ¤òºÇ½é¤Ë¸Æ¤Ó½Ð¤¹ºÝ¤Ë¤Ï¡¢
145 .I str
146 ¤Ï²òÀÏÂоݤÎʸ»úÎó¤ò»Ø¤·¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤º¡¢
147 .I saveptr
148 ¤ÎÃͤÏ̵»ë¤µ¤ì¤ë¡£¤½¤ì°Ê¹ß¤Î¸Æ¤Ó½Ð¤·¤Ç¤Ï¡¢
149 .I str
150 ¤Ï NULL ¤È¤·¡¢
151 .I saveptr
152 ¤ÏÁ°²ó¤Î¸Æ¤Ó½Ð¤·°Ê¹ßÊѹ¹¤·¤Ê¤¤¤è¤¦¤Ë¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
153
154 .\"O Different strings may be parsed concurrently using sequences of calls to
155 .\"O .BR strtok_r ()
156 .\"O that specify different \fIsaveptr\fP arguments.
157 .BR strtok_r ()
158 ¤Î¸Æ¤Ó½Ð¤·»þ¤Ë°Û¤Ê¤ë \fIsaveptr\fP °ú¤­¿ô¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢
159 °Û¤Ê¤ëʸ»úÎó¤Î²òÀϤòƱ»þ¤Ë¹Ô¤¦¤³¤È¤¬¤Ç¤­¤ë¡£
160 .\"O .SH "RETURN VALUE"
161 .SH ÊÖ¤êÃÍ
162 .\"O The
163 .\"O .BR strtok ()
164 .\"O and
165 .\"O .BR strtok_r ()
166 .\"O functions return a pointer to
167 .\"O the next token, or NULL if there are no more tokens.
168 .BR strtok ()
169 ¤È
170 .BR strtok_r ()
171 ¤Ï¼¡¤Î¥È¡¼¥¯¥ó¤Ø¤Î¥Ý¥¤¥ó¥¿¤«¡¢
172 ¥È¡¼¥¯¥ó¤¬¤Ê¤±¤ì¤Ð NULL ¤òÊÖ¤¹¡£
173 .\"O .SH "CONFORMING TO"
174 .SH ½àµò
175 .TP
176 .BR strtok ()
177 SVr4, POSIX.1-2001, 4.3BSD, C89, C99.
178 .TP
179 .BR strtok_r ()
180 POSIX.1-2001.
181 .\"O .SH "BUGS"
182 .SH ¥Ð¥°
183 .\"O Be cautious when using these functions.
184 .\"O If you do use them, note that:
185 ¤³¤ì¤é¤Î´Ø¿ô¤ò»È¤¦¤Î¤Ï¿µ½Å¤Ë¶ãÌ£¤¹¤ë¤³¤È¡£
186 »ÈÍѤ¹¤ë¾ì¹ç¤Ï¡¢°Ê²¼¤ÎÅÀ¤ËÃí°Õ¤¬É¬ÍפǤ¢¤ë¡£
187 .IP * 2
188 .\"O These functions modify their first argument.
189 ¤³¤ì¤é¤Î´Ø¿ô¤Ï¤½¤ÎºÇ½é¤Î°ú¿ô¤òÊѹ¹¤¹¤ë¡£
190 .IP *
191 .\"O These functions cannot be used on constant strings.
192 ¤³¤ì¤é¤Î´Ø¿ô¤Ï const ¤Êʸ»úÎó¤Ç¤Ï»È¤¨¤Ê¤¤¡£
193 .IP *
194 .\"O The identity of the delimiting character is lost.
195 ¶èÀÚ¤êʸ»ú¼«ÂΤϼº¤ï¤ì¤Æ¤·¤Þ¤¦¡£
196 .IP *
197 .\"O The
198 .\"O .BR strtok ()
199 .\"O function uses a static buffer while parsing, so it's not thread safe.
200 .\"O Use
201 .\"O .BR strtok_r ()
202 .\"O if this matters to you.
203 .BR strtok ()
204 ´Ø¿ô¤Ïʸ»úÎó¤Î²òÀϤËÀÅŪ¥Ð¥Ã¥Õ¥¡¤òÍѤ¤¤ë¤Î¤Ç¡¢¥¹¥ì¥Ã¥É¥»¡¼¥Õ¤Ç¤Ê¤¤¡£
205 ¤³¤ì¤¬ÌäÂê¤Ë¤Ê¤ë¾ì¹ç¤Ï
206 .BR strtok_r ()
207 ¤òÍѤ¤¤ë¤³¤È¡£
208 .\"O .SH EXAMPLE
209 .SH Îã
210 .\"O The program below uses nested loops that employ
211 .\"O .BR strtok_r ()
212 .\"O to break a string into a two-level hierarchy of tokens.
213 .\"O The first command-line argument specifies the string to be parsed.
214 .\"O The second argument specifies the delimiter character(s)
215 .\"O to be used to separate that string into "major" tokens.
216 .\"O The third argument specifies the delimiter character(s)
217 .\"O to be used to separate the "major" tokens into subtokens.
218 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï¡¢
219 .BR strtok_r ()
220 ¤òÍøÍѤ¹¤ë¥ë¡¼¥×¤òÆþ¤ì»Ò¤Ë¤·¤Æ»ÈÍѤ·¡¢
221 ʸ»úÎó¤ò2³¬ÁؤΥȡ¼¥¯¥ó¤Ëʬ³ä¤¹¤ë¤â¤Î¤Ç¤¢¤ë¡£
222 1ÈÖÌܤΥ³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤Ë¤Ï¡¢²òÀÏÂоݤÎʸ»úÎó¤ò»ØÄꤹ¤ë¡£
223 2ÈÖÌܤΰú¤­¿ô¤Ë¤Ï¡¢Ê¸»úÎó¤ò¡ÖÂ礭¤Ê¡×¥È¡¼¥¯¥ó¤Ëʬ³ä¤¹¤ë¤¿¤á¤Ë
224 »ÈÍѤ¹¤ë¶èÀÚ¤êʸ»ú¤ò»ØÄꤹ¤ë¡£
225 3ÈÖÌܤΰú¤­¿ô¤Ë¤Ï¡¢¡ÖÂ礭¤Ê¡×¥È¡¼¥¯¥ó¤òºÙ¤«¤¯Ê¬³ä¤¹¤ë¤¿¤á¤Ë
226 »ÈÍѤ¹¤ë¶èÀÚ¤êʸ»ú¤ò»ØÄꤹ¤ë¡£
227 .PP
228 .PP
229 .\"O An example of the output produced by this program is the following:
230 ¤³¤Î¥×¥í¥°¥é¥à¤Î½ÐÎÏÎã¤ò°Ê²¼¤Ë¼¨¤¹¡£
231 .PP
232 .in +4n
233 .nf
234 .RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
235 1: a/bbb///cc
236          \-\-> a
237          \-\-> bbb
238          \-\-> cc
239 2: xxx
240          \-\-> xxx
241 3: yyy
242          \-\-> yyy
243 .fi
244 .in
245 .\"O .SS Program source
246 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
247 \&
248 .nf
249 #include <stdio.h>
250 #include <stdlib.h>
251 #include <string.h>
252
253 int
254 main(int argc, char *argv[])
255 {
256     char *str1, *str2, *token, *subtoken;
257     char *saveptr1, *saveptr2;
258     int j;
259
260     if (argc != 4) {
261         fprintf(stderr, "Usage: %s string delim subdelim\\n",
262                 argv[0]);
263         exit(EXIT_FAILURE);
264     }
265
266     for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
267         token = strtok_r(str1, argv[2], &saveptr1);
268         if (token == NULL)
269             break;
270         printf("%d: %s\\n", j, token);
271
272         for (str2 = token; ; str2 = NULL) {
273             subtoken = strtok_r(str2, argv[3], &saveptr2);
274             if (subtoken == NULL)
275                 break;
276             printf("\t \-\-> %s\\n", subtoken);
277         }
278     }
279
280     exit(EXIT_SUCCESS);
281 } /* main */
282 .fi
283 .\"O .SH "SEE ALSO"
284 .SH ´ØÏ¢¹àÌÜ
285 .BR index (3),
286 .BR memchr (3),
287 .BR rindex (3),
288 .BR strchr (3),
289 .BR strpbrk (3),
290 .BR strsep (3),
291 .BR strspn (3),
292 .BR strstr (3),
293 .BR wcstok (3)