OSDN Git Service

aed41b3f374f3ca240388ae8c5f1563b7939d359
[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 .TH STRTOK 3 2013\-05\-19 GNU "Linux Programmer's Manual"
38 .SH 名前
39 strtok, strtok_r \- 文字列からトークンを取り出す
40 .SH 書式
41 .nf
42 \fB#include <string.h>\fP
43 .sp
44 \fBchar *strtok(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB);\fP
45 .sp
46 \fBchar *strtok_r(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB, char **\fP\fIsaveptr\fP\fB);\fP
47 .fi
48 .sp
49 .in -4n
50 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
51 .in
52 .sp
53 .ad l
54 \fBstrtok_r\fP(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
55 _XOPEN_SOURCE || _POSIX_SOURCE
56 .ad b
57 .SH 説明
58 \fBstrtok\fP()  関数は文字列を 0 個以上の空でないトークンの列に分割する。 \fBstrtok\fP()
59 を最初に呼び出す際には、解析対象の文字列を \fIstr\fP に 指定する。同じ文字列の解析を行うその後の呼び出しでは、 \fIstr\fP は NULL
60 にしなければならない。
61
62 \fIdelim\fP 引き数には、解析対象の文字列をトークンに区切るのに使用する
63 バイト集合を指定する。同じ文字列を解析する一連の呼び出しにおいて、
64 \fIdelim\fP に違う文字列を指定してもよい。
65
66 \fBstrtok\fP() のそれぞれの呼び出しでは、次のトークンを格納した NULL 終端
67 された文字列へのポインタが返される。この文字列には区切りバイトは含まれ
68 ない。これ以上トークンが見つからなかった場合には、NULL が返される。
69
70 同じ文字列に対して操作を行う \fBstrtok\fP() を連続して呼び出す場合、 次のトークンを探し始める位置を決めるためのポインタが保持される。 最初の
71 \fBstrtok\fP の呼び出しでは、 このポインタは対象の文字列の最初のバイトにセットされる。 次のトークンの先頭は、 \fIstr\fP
72 内で次の区切りバイト以外のバイトを前方に検索して決定される。 区切りバイト以外のバイトが見つからなかった場合は、 トークンはこれ以上なく、
73 \fBstrtok\fP() は NULL を返す (したがって、 空の文字列や区切りバイトだけを含む文字列の場合には、 最初の \fBstrtok\fP()
74 の呼び出しで NULL が返ることになる)。
75
76 各トークンの末尾は、次の区切りバイトが見つかるか、終端の NULL バイト (\(aq\e0\(aq)
77 に達するまで文字列を前方に検索することで見つかる。 区切りバイトが見つかった場合には、 現在のトークンの終わりを示すために、 見つかった区切りバイトが
78 NULL バイトで上書きされ、 \fBstrtok\fP() はポインタを次のバイトに設定する。
79 このポインタは、次のトークンを検索する際の開始点として使用される。 この場合、 \fBstrtok\fP() は見つかったトークンの先頭へのポインタを返す。
80
81 上記の説明の通り、 解析対象の文字列に 2 つ以上の区切りバイトが連続している場合には、 一つの区切りバイトとみなされ、
82 文字列の先頭や末尾にある区切りバイトは無視される。 言い換えると、 \fBstrtok\fP() が返すトークンは必ず空でない文字列となる。
83 したがって、例えば "\fIaaa;;bbb,\fP" という文字列が与えられたとすると、 区切り文字列 "\fI;,\fP" を指定した一連の
84 \fBstrtok\fP() の呼び出しでは、 "\fIaaa\fP" と \fIbbb\fP" が返り、その次に NULL ポインタが返る。
85
86 \fBstrtok_r\fP()  関数は \fBstrtok\fP()  のリエントラント版である。 \fIsaveptr\fP 引き数は \fIchar\ *\fP
87 変数へのポインタであり、 同じ文字列の解析を行う \fBstrtok_r\fP()  の呼び出し間で処理状況を保存するために \fBstrtok_r\fP()
88 内部で使用される。
89
90 \fBstrtok_r\fP()  を最初に呼び出す際には、 \fIstr\fP は解析対象の文字列を指していなければならず、 \fIsaveptr\fP
91 の値は無視される。それ以降の呼び出しでは、 \fIstr\fP は NULL とし、 \fIsaveptr\fP
92 は前回の呼び出し以降変更しないようにしなければならない。
93
94 \fBstrtok_r\fP()  の呼び出し時に異なる \fIsaveptr\fP 引き数を指定することで、 異なる文字列の解析を同時に行うことができる。
95 .SH 返り値
96 \fBstrtok\fP()  と \fBstrtok_r\fP()  は次のトークンへのポインタか、 トークンがなければ NULL を返す。
97 .SH 属性
98 .SS "マルチスレッディング (pthreads(7) 参照)"
99 The \fBstrtok\fP() 関数はスレッドセーフではない。
100 .LP
101 \fBstrtok_r\fP() 関数はスレッドセーフである。
102 .SH 準拠
103 .TP 
104 \fBstrtok\fP()
105 SVr4, POSIX.1\-2001, 4.3BSD, C89, C99.
106 .TP 
107 \fBstrtok_r\fP()
108 POSIX.1\-2001.
109 .SH バグ
110 これらの関数を使うのは慎重に吟味すること。 使用する場合は、以下の点に注意が必要である。
111 .IP * 2
112 これらの関数はその最初の引数を変更する。
113 .IP *
114 これらの関数は const な文字列では使えない。
115 .IP *
116 区切りバイト自体は失われてしまう。
117 .IP *
118 \fBstrtok\fP()  関数は文字列の解析に静的バッファを用いるので、スレッドセーフでない。 これが問題になる場合は \fBstrtok_r\fP()
119 を用いること。
120 .SH 例
121 以下のプログラムは、 \fBstrtok_r\fP() を利用するループを入れ子にして使用し、
122 文字列を 2 階層のトークンに分割するものである。 1番目のコマンドライン
123 引き数には、解析対象の文字列を指定する。 2 番目の引き数には、文字列を
124 「大きな」トークンに分割するために 使用する区切りバイトを指定する。
125 3 番目の引き数には、「大きな」トークンを細かく分割するために使用する
126 区切りバイトを指定する。
127 .PP
128 このプログラムの出力例を以下に示す。
129 .PP
130 .in +4n
131 .nf
132 $\fB ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq\fP
133 1: a/bbb///cc
134          \-\-> a
135          \-\-> bbb
136          \-\-> cc
137 2: xxx
138          \-\-> xxx
139 3: yyy
140          \-\-> yyy
141 .fi
142 .in
143 .SS プログラムのソース
144 \&
145 .nf
146 #include <stdio.h>
147 #include <stdlib.h>
148 #include <string.h>
149
150 int
151 main(int argc, char *argv[])
152 {
153     char *str1, *str2, *token, *subtoken;
154     char *saveptr1, *saveptr2;
155     int j;
156
157     if (argc != 4) {
158         fprintf(stderr, "Usage: %s string delim subdelim\en",
159                 argv[0]);
160         exit(EXIT_FAILURE);
161     }
162
163     for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
164         token = strtok_r(str1, argv[2], &saveptr1);
165         if (token == NULL)
166             break;
167         printf("%d: %s\en", j, token);
168
169         for (str2 = token; ; str2 = NULL) {
170             subtoken = strtok_r(str2, argv[3], &saveptr2);
171             if (subtoken == NULL)
172                 break;
173             printf("\t \-\-> %s\en", subtoken);
174         }
175     }
176
177     exit(EXIT_SUCCESS);
178 }
179 .fi
180 .PP
181 \fBstrtok\fP()  を使った別のプログラム例が \fBgetaddrinfo_a\fP(3)  にある。
182 .SH 関連項目
183 \fBindex\fP(3), \fBmemchr\fP(3), \fBrindex\fP(3), \fBstrchr\fP(3), \fBstring\fP(3),
184 \fBstrpbrk\fP(3), \fBstrsep\fP(3), \fBstrspn\fP(3), \fBstrstr\fP(3), \fBwcstok\fP(3)
185 .SH この文書について
186 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
187 である。プロジェクトの説明とバグ報告に関する情報は
188 http://www.kernel.org/doc/man\-pages/ に書かれている。