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 Nicolas Lichtmaier <nick@debian.org>
26 .\" 2005-11-17, mtk: Substantial parts rewritten
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>
35 .TH STRTOK 3 2010-09-27 "GNU" "Linux Programmer's Manual"
37 .\"O strtok, strtok_r \- extract tokens from strings
39 strtok, strtok_r \- 文字列からトークンを取り出す
43 .B #include <string.h>
45 .BI "char *strtok(char *" str ", const char *" delim );
47 .BI "char *strtok_r(char *" str ", const char *" delim ", char **" saveptr );
51 .\"O Feature Test Macro Requirements for glibc (see
52 .\"O .BR feature_test_macros (7)):
54 .RB ( feature_test_macros (7)
60 _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
61 _XOPEN_SOURCE || _POSIX_SOURCE
67 .\"O function parses a string into a sequence of tokens.
68 .\"O On the first call to
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.
77 を最初に呼び出す際には、解析対象の文字列を \fIstr\fP に
78 指定する。同じ文字列の解析を行うその後の呼び出しでは、
79 \fIstr\fP には NULL を指定する。
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 に違う文字列を指定してもよい。
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,
99 格納した NULL 終端された文字列へのポインタが返される。
101 これ以上トークンが見つからなかった場合には、NULL が返される。
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
108 .\"O are always nonempty strings.
109 解析対象の文字列に2つ以上の区切り文字が連続している場合には、
111 文字列の先頭や末尾にある区切り文字は無視される。言い換えると、
113 が返すトークンは常に空でない文字列となる。
117 .\"O function is a reentrant version
119 .\"O The \fIsaveptr\fP argument is a pointer to a
120 .\"O \fIchar *\fP variable that is used internally by
122 .\"O in order to maintain context between successive calls that parse the
128 \fIsaveptr\fP 引き数は \fIchar *\fP 変数へのポインタであり、
135 .\"O On the first call to
136 .\"O .BR strtok_r (),
138 .\"O should point to the string to be parsed, and the value of
141 .\"O In subsequent calls, \fIstr\fP should be NULL, and
142 .\"O \fIsaveptr\fP should be unchanged since the previous call.
146 は解析対象の文字列を指していなければならず、
148 の値は無視される。それ以降の呼び出しでは、
152 は前回の呼び出し以降変更しないようにしなければならない。
154 .\"O Different strings may be parsed concurrently using sequences of calls to
156 .\"O that specify different \fIsaveptr\fP arguments.
158 の呼び出し時に異なる \fIsaveptr\fP 引き数を指定することで、
159 異なる文字列の解析を同時に行うことができる。
160 .\"O .SH "RETURN VALUE"
166 .\"O functions return a pointer to
167 .\"O the next token, or NULL if there are no more tokens.
173 .\"O .SH "CONFORMING TO"
177 SVr4, POSIX.1-2001, 4.3BSD, C89, C99.
183 .\"O Be cautious when using these functions.
184 .\"O If you do use them, note that:
185 これらの関数を使うのは慎重に吟味すること。
186 使用する場合は、以下の点に注意が必要である。
188 .\"O These functions modify their first argument.
191 .\"O These functions cannot be used on constant strings.
192 これらの関数は const な文字列では使えない。
194 .\"O The identity of the delimiting character is lost.
199 .\"O function uses a static buffer while parsing, so it's not thread safe.
202 .\"O if this matters to you.
204 関数は文字列の解析に静的バッファを用いるので、スレッドセーフでない。
210 .\"O The program below uses nested loops that employ
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.
221 文字列を2階層のトークンに分割するものである。
222 1番目のコマンドライン引き数には、解析対象の文字列を指定する。
223 2番目の引き数には、文字列を「大きな」トークンに分割するために
225 3番目の引き数には、「大きな」トークンを細かく分割するために
229 .\"O An example of the output produced by this program is the following:
234 .RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
245 .\"O .SS Program source
254 main(int argc, char *argv[])
256 char *str1, *str2, *token, *subtoken;
257 char *saveptr1, *saveptr2;
261 fprintf(stderr, "Usage: %s string delim subdelim\\n",
266 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
267 token = strtok_r(str1, argv[2], &saveptr1);
270 printf("%d: %s\\n", j, token);
272 for (str2 = token; ; str2 = NULL) {
273 subtoken = strtok_r(str2, argv[3], &saveptr2);
274 if (subtoken == NULL)
276 printf("\t \-\-> %s\\n", subtoken);
284 .\"O Another example program using
287 .\"O .BR getaddrinfo_a (3).
290 .BR getaddrinfo_a (3)