OSDN Git Service

(split) LDP: Restore and add Copyrights for draft pages
[linuxjm/LDP_man-pages.git] / draft / man3 / strtok.3
1 .\" Copyright (C) 2005, 2013 Michael Kerrisk (mtk.manpages@gmail.com)
2 .\" a few fragments from an earlier (1996) version by
3 .\" Andries Brouwer (aeb@cwi.nl) remain.
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Rewritten old page, 960210, aeb@cwi.nl
28 .\" Updated, added strtok_r. 2000-02-13 Nicolás Lichtmaier <nick@debian.org>
29 .\" 2005-11-17, mtk: Substantial parts rewritten
30 .\" 2013-05-19, mtk: added much further detail on the operation of strtok()
31 .\"
32 .\"*******************************************************************
33 .\"
34 .\" This file was generated with po4a. Translate the source file.
35 .\"
36 .\"*******************************************************************
37 .\"
38 .\" Japanese Version Copyright (c) 1998 Ishii Tatsuo all rights reserved.
39 .\" Translated 1998-03-27, Ishii Tatsuo <rfun@azusa.shinshu-u.ac.jp>
40 .\" Updated 2000-04-05, Kentaro Shirakata <argrath@ub32.org>
41 .\" Updated 2000-09-21, Kentaro Shirakata
42 .\" Updated 2002-03-28, Kentaro Shirakata
43 .\" Updated 2005-11-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
44 .\" Updated 2012-05-29, Akihiro MOTOKI <amotoki@gmail.com>
45 .\" Updated 2013-07-22, Akihiro MOTOKI <amotoki@gmail.com>
46 .\" Updated 2013-07-31, Akihiro MOTOKI <amotoki@gmail.com>
47 .\"
48 .TH STRTOK 3 2013\-05\-19 GNU "Linux Programmer's Manual"
49 .SH 名前
50 strtok, strtok_r \- 文字列からトークンを取り出す
51 .SH 書式
52 .nf
53 \fB#include <string.h>\fP
54 .sp
55 \fBchar *strtok(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB);\fP
56 .sp
57 \fBchar *strtok_r(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB, char **\fP\fIsaveptr\fP\fB);\fP
58 .fi
59 .sp
60 .in -4n
61 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
62 .in
63 .sp
64 .ad l
65 \fBstrtok_r\fP(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
66 _XOPEN_SOURCE || _POSIX_SOURCE
67 .ad b
68 .SH 説明
69 \fBstrtok\fP()  関数は文字列を 0 個以上の空でないトークンの列に分割する。 \fBstrtok\fP()
70 を最初に呼び出す際には、解析対象の文字列を \fIstr\fP に 指定する。同じ文字列の解析を行うその後の呼び出しでは、 \fIstr\fP は NULL
71 にしなければならない。
72
73 \fIdelim\fP 引き数には、解析対象の文字列をトークンに区切るのに使用する
74 バイト集合を指定する。同じ文字列を解析する一連の呼び出しにおいて、
75 \fIdelim\fP に違う文字列を指定してもよい。
76
77 \fBstrtok\fP() のそれぞれの呼び出しでは、次のトークンを格納した NULL 終端
78 された文字列へのポインタが返される。この文字列には区切りバイトは含まれ
79 ない。これ以上トークンが見つからなかった場合には、NULL が返される。
80
81 同じ文字列に対して操作を行う \fBstrtok\fP() を連続して呼び出す場合、 次のトークンを探し始める位置を決めるためのポインタが保持される。 最初の
82 \fBstrtok\fP の呼び出しでは、 このポインタは対象の文字列の最初のバイトにセットされる。 次のトークンの先頭は、 \fIstr\fP
83 内で次の区切りバイト以外のバイトを前方に検索して決定される。 区切りバイト以外のバイトが見つからなかった場合は、 トークンはこれ以上なく、
84 \fBstrtok\fP() は NULL を返す (したがって、 空の文字列や区切りバイトだけを含む文字列の場合には、 最初の \fBstrtok\fP()
85 の呼び出しで NULL が返ることになる)。
86
87 各トークンの末尾は、次の区切りバイトが見つかるか、終端の NULL バイト (\(aq\e0\(aq)
88 に達するまで文字列を前方に検索することで見つかる。 区切りバイトが見つかった場合には、 現在のトークンの終わりを示すために、 見つかった区切りバイトが
89 NULL バイトで上書きされ、 \fBstrtok\fP() はポインタを次のバイトに設定する。
90 このポインタは、次のトークンを検索する際の開始点として使用される。 この場合、 \fBstrtok\fP() は見つかったトークンの先頭へのポインタを返す。
91
92 上記の説明の通り、 解析対象の文字列に 2 つ以上の区切りバイトが連続している場合には、 一つの区切りバイトとみなされ、
93 文字列の先頭や末尾にある区切りバイトは無視される。 言い換えると、 \fBstrtok\fP() が返すトークンは必ず空でない文字列となる。
94 したがって、例えば "\fIaaa;;bbb,\fP" という文字列が与えられたとすると、 区切り文字列 "\fI;,\fP" を指定した一連の
95 \fBstrtok\fP() の呼び出しでは、 "\fIaaa\fP" と \fIbbb\fP" が返り、その次に NULL ポインタが返る。
96
97 \fBstrtok_r\fP()  関数は \fBstrtok\fP()  のリエントラント版である。 \fIsaveptr\fP 引き数は \fIchar\ *\fP
98 変数へのポインタであり、 同じ文字列の解析を行う \fBstrtok_r\fP()  の呼び出し間で処理状況を保存するために \fBstrtok_r\fP()
99 内部で使用される。
100
101 \fBstrtok_r\fP()  を最初に呼び出す際には、 \fIstr\fP は解析対象の文字列を指していなければならず、 \fIsaveptr\fP
102 の値は無視される。それ以降の呼び出しでは、 \fIstr\fP は NULL とし、 \fIsaveptr\fP
103 は前回の呼び出し以降変更しないようにしなければならない。
104
105 \fBstrtok_r\fP()  の呼び出し時に異なる \fIsaveptr\fP 引き数を指定することで、 異なる文字列の解析を同時に行うことができる。
106 .SH 返り値
107 \fBstrtok\fP()  と \fBstrtok_r\fP()  は次のトークンへのポインタか、 トークンがなければ NULL を返す。
108 .SH 属性
109 .SS "マルチスレッディング (pthreads(7) 参照)"
110 The \fBstrtok\fP() 関数はスレッドセーフではない。
111 .LP
112 \fBstrtok_r\fP() 関数はスレッドセーフである。
113 .SH 準拠
114 .TP 
115 \fBstrtok\fP()
116 SVr4, POSIX.1\-2001, 4.3BSD, C89, C99.
117 .TP 
118 \fBstrtok_r\fP()
119 POSIX.1\-2001.
120 .SH バグ
121 これらの関数を使うのは慎重に吟味すること。 使用する場合は、以下の点に注意が必要である。
122 .IP * 2
123 これらの関数はその最初の引数を変更する。
124 .IP *
125 これらの関数は const な文字列では使えない。
126 .IP *
127 区切りバイト自体は失われてしまう。
128 .IP *
129 \fBstrtok\fP()  関数は文字列の解析に静的バッファを用いるので、スレッドセーフでない。 これが問題になる場合は \fBstrtok_r\fP()
130 を用いること。
131 .SH 例
132 以下のプログラムは、 \fBstrtok_r\fP() を利用するループを入れ子にして使用し、
133 文字列を 2 階層のトークンに分割するものである。 1番目のコマンドライン
134 引き数には、解析対象の文字列を指定する。 2 番目の引き数には、文字列を
135 「大きな」トークンに分割するために 使用する区切りバイトを指定する。
136 3 番目の引き数には、「大きな」トークンを細かく分割するために使用する
137 区切りバイトを指定する。
138 .PP
139 このプログラムの出力例を以下に示す。
140 .PP
141 .in +4n
142 .nf
143 $\fB ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq\fP
144 1: a/bbb///cc
145          \-\-> a
146          \-\-> bbb
147          \-\-> cc
148 2: xxx
149          \-\-> xxx
150 3: yyy
151          \-\-> yyy
152 .fi
153 .in
154 .SS プログラムのソース
155 \&
156 .nf
157 #include <stdio.h>
158 #include <stdlib.h>
159 #include <string.h>
160
161 int
162 main(int argc, char *argv[])
163 {
164     char *str1, *str2, *token, *subtoken;
165     char *saveptr1, *saveptr2;
166     int j;
167
168     if (argc != 4) {
169         fprintf(stderr, "Usage: %s string delim subdelim\en",
170                 argv[0]);
171         exit(EXIT_FAILURE);
172     }
173
174     for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
175         token = strtok_r(str1, argv[2], &saveptr1);
176         if (token == NULL)
177             break;
178         printf("%d: %s\en", j, token);
179
180         for (str2 = token; ; str2 = NULL) {
181             subtoken = strtok_r(str2, argv[3], &saveptr2);
182             if (subtoken == NULL)
183                 break;
184             printf("\t \-\-> %s\en", subtoken);
185         }
186     }
187
188     exit(EXIT_SUCCESS);
189 }
190 .fi
191 .PP
192 \fBstrtok\fP()  を使った別のプログラム例が \fBgetaddrinfo_a\fP(3)  にある。
193 .SH 関連項目
194 \fBindex\fP(3), \fBmemchr\fP(3), \fBrindex\fP(3), \fBstrchr\fP(3), \fBstring\fP(3),
195 \fBstrpbrk\fP(3), \fBstrsep\fP(3), \fBstrspn\fP(3), \fBstrstr\fP(3), \fBwcstok\fP(3)
196 .SH この文書について
197 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
198 である。プロジェクトの説明とバグ報告に関する情報は
199 http://www.kernel.org/doc/man\-pages/ に書かれている。