OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / 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  2010-09-27 "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 }
282 .fi
283 .PP
284 .\"O Another example program using
285 .\"O .BR strtok ()
286 .\"O can be found in
287 .\"O .BR getaddrinfo_a (3).
288 .BR strtok ()
289 を使った別のプログラム例が
290 .BR getaddrinfo_a (3)
291 にある。
292 .\"O .SH "SEE ALSO"
293 .SH 関連項目
294 .BR index (3),
295 .BR memchr (3),
296 .BR rindex (3),
297 .BR strchr (3),
298 .BR string (3),
299 .BR strpbrk (3),
300 .BR strsep (3),
301 .BR strspn (3),
302 .BR strstr (3),
303 .BR wcstok (3)