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 strtok, strtok_r \- 文字列からトークンを取り出す
40 .B #include <string.h>
42 .BI "char *strtok(char *" str ", const char *" delim );
44 .BI "char *strtok_r(char *" str ", const char *" delim ", char **" saveptr );
49 .RB ( feature_test_macros (7)
55 _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
56 _XOPEN_SOURCE || _POSIX_SOURCE
62 を最初に呼び出す際には、解析対象の文字列を \fIstr\fP に
63 指定する。同じ文字列の解析を行うその後の呼び出しでは、
64 \fIstr\fP には NULL を指定する。
66 \fIdelim\fP 引き数には、解析する文字列をトークンに区切る文字集合を
67 指定する。同じ文字列を解析する一連の呼び出しにおいて、
68 \fIdelim\fP に違う文字列を指定してもよい。
72 格納した NULL 終端された文字列へのポインタが返される。
74 これ以上トークンが見つからなかった場合には、NULL が返される。
76 解析対象の文字列に2つ以上の区切り文字が連続している場合には、
78 文字列の先頭や末尾にある区切り文字は無視される。言い換えると、
86 \fIsaveptr\fP 引き数は \fIchar *\fP 変数へのポインタであり、
96 は解析対象の文字列を指していなければならず、
102 は前回の呼び出し以降変更しないようにしなければならない。
105 の呼び出し時に異なる \fIsaveptr\fP 引き数を指定することで、
106 異なる文字列の解析を同時に行うことができる。
116 SVr4, POSIX.1-2001, 4.3BSD, C89, C99.
121 これらの関数を使うのは慎重に吟味すること。
122 使用する場合は、以下の点に注意が必要である。
126 これらの関数は const な文字列では使えない。
131 関数は文字列の解析に静的バッファを用いるので、スレッドセーフでない。
139 文字列を2階層のトークンに分割するものである。
140 1番目のコマンドライン引き数には、解析対象の文字列を指定する。
141 2番目の引き数には、文字列を「大きな」トークンに分割するために
143 3番目の引き数には、「大きな」トークンを細かく分割するために
151 .RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
170 main(int argc, char *argv[])
172 char *str1, *str2, *token, *subtoken;
173 char *saveptr1, *saveptr2;
177 fprintf(stderr, "Usage: %s string delim subdelim\\n",
182 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
183 token = strtok_r(str1, argv[2], &saveptr1);
186 printf("%d: %s\\n", j, token);
188 for (str2 = token; ; str2 = NULL) {
189 subtoken = strtok_r(str2, argv[3], &saveptr2);
190 if (subtoken == NULL)
192 printf("\t \-\-> %s\\n", subtoken);
202 .BR getaddrinfo_a (3)