OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[linuxjm/LDP_man-pages.git] / release / 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 Nicolás Lichtmaier <nick@debian.org>
26 .\" 2005-11-17, mtk: Substantial parts rewritten
27 .\"
28 .\"*******************************************************************
29 .\"
30 .\" This file was generated with po4a. Translate the source file.
31 .\"
32 .\"*******************************************************************
33 .TH STRTOK 3 2010\-09\-27 GNU "Linux Programmer's Manual"
34 .SH 名前
35 strtok, strtok_r \- 文字列からトークンを取り出す
36 .SH 書式
37 .nf
38 \fB#include <string.h>\fP
39 .sp
40 \fBchar *strtok(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB);\fP
41 .sp
42 \fBchar *strtok_r(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB, char **\fP\fIsaveptr\fP\fB);\fP
43 .fi
44 .sp
45 .in -4n
46 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
47 .in
48 .sp
49 .ad l
50 \fBstrtok_r\fP(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
51 _XOPEN_SOURCE || _POSIX_SOURCE
52 .ad b
53 .SH 説明
54 \fBstrtok\fP()  関数は文字列を解析してトークンに分割する。 \fBstrtok\fP()  を最初に呼び出す際には、解析対象の文字列を \fIstr\fP
55 に 指定する。同じ文字列の解析を行うその後の呼び出しでは、 \fIstr\fP には NULL を指定する。
56
57 \fIdelim\fP 引き数には、解析する文字列をトークンに区切る文字集合を 指定する。同じ文字列を解析する一連の呼び出しにおいて、 \fIdelim\fP
58 に違う文字列を指定してもよい。
59
60 \fBstrtok\fP()  のそれぞれの呼び出しでは、次のトークンを 格納した NULL 終端された文字列へのポインタが返される。
61 この文字列には区切り文字は含まれない。 これ以上トークンが見つからなかった場合には、NULL が返される。
62
63 解析対象の文字列に2つ以上の区切り文字が連続している場合には、 一つの区切り文字とみなされる。
64 文字列の先頭や末尾にある区切り文字は無視される。言い換えると、 \fBstrtok\fP()  が返すトークンは常に空でない文字列となる。
65
66 \fBstrtok_r\fP()  関数は \fBstrtok\fP()  のリエントラント版である。 \fIsaveptr\fP 引き数は \fIchar *\fP
67 変数へのポインタであり、 同じ文字列の解析を行う \fBstrtok_r\fP()  の呼び出し間で処理状況を保存するために \fBstrtok_r\fP()
68 内部で使用される。
69
70 \fBstrtok_r\fP()  を最初に呼び出す際には、 \fIstr\fP は解析対象の文字列を指していなければならず、 \fIsaveptr\fP
71 の値は無視される。それ以降の呼び出しでは、 \fIstr\fP は NULL とし、 \fIsaveptr\fP
72 は前回の呼び出し以降変更しないようにしなければならない。
73
74 \fBstrtok_r\fP()  の呼び出し時に異なる \fIsaveptr\fP 引き数を指定することで、 異なる文字列の解析を同時に行うことができる。
75 .SH 返り値
76 \fBstrtok\fP()  と \fBstrtok_r\fP()  は次のトークンへのポインタか、 トークンがなければ NULL を返す。
77 .SH 準拠
78 .TP 
79 \fBstrtok\fP()
80 SVr4, POSIX.1\-2001, 4.3BSD, C89, C99.
81 .TP 
82 \fBstrtok_r\fP()
83 POSIX.1\-2001.
84 .SH バグ
85 これらの関数を使うのは慎重に吟味すること。 使用する場合は、以下の点に注意が必要である。
86 .IP * 2
87 これらの関数はその最初の引数を変更する。
88 .IP *
89 これらの関数は const な文字列では使えない。
90 .IP *
91 区切り文字自体は失われてしまう。
92 .IP *
93 \fBstrtok\fP()  関数は文字列の解析に静的バッファを用いるので、スレッドセーフでない。 これが問題になる場合は \fBstrtok_r\fP()
94 を用いること。
95 .SH 例
96 以下のプログラムは、 \fBstrtok_r\fP()  を利用するループを入れ子にして使用し、 文字列を2階層のトークンに分割するものである。
97 1番目のコマンドライン引き数には、解析対象の文字列を指定する。 2番目の引き数には、文字列を「大きな」トークンに分割するために
98 使用する区切り文字を指定する。 3番目の引き数には、「大きな」トークンを細かく分割するために 使用する区切り文字を指定する。
99 .PP
100 このプログラムの出力例を以下に示す。
101 .PP
102 .in +4n
103 .nf
104 $\fB ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq\fP
105 1: a/bbb///cc
106          \-\-> a
107          \-\-> bbb
108          \-\-> cc
109 2: xxx
110          \-\-> xxx
111 3: yyy
112          \-\-> yyy
113 .fi
114 .in
115 .SS プログラムのソース
116 \&
117 .nf
118 #include <stdio.h>
119 #include <stdlib.h>
120 #include <string.h>
121
122 int
123 main(int argc, char *argv[])
124 {
125     char *str1, *str2, *token, *subtoken;
126     char *saveptr1, *saveptr2;
127     int j;
128
129     if (argc != 4) {
130         fprintf(stderr, "Usage: %s string delim subdelim\en",
131                 argv[0]);
132         exit(EXIT_FAILURE);
133     }
134
135     for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
136         token = strtok_r(str1, argv[2], &saveptr1);
137         if (token == NULL)
138             break;
139         printf("%d: %s\en", j, token);
140
141         for (str2 = token; ; str2 = NULL) {
142             subtoken = strtok_r(str2, argv[3], &saveptr2);
143             if (subtoken == NULL)
144                 break;
145             printf("\t \-\-> %s\en", subtoken);
146         }
147     }
148
149     exit(EXIT_SUCCESS);
150 }
151 .fi
152 .PP
153 \fBstrtok\fP()  を使った別のプログラム例が \fBgetaddrinfo_a\fP(3)  にある。
154 .SH 関連項目
155 \fBindex\fP(3), \fBmemchr\fP(3), \fBrindex\fP(3), \fBstrchr\fP(3), \fBstring\fP(3),
156 \fBstrpbrk\fP(3), \fBstrsep\fP(3), \fBstrspn\fP(3), \fBstrstr\fP(3), \fBwcstok\fP(3)