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 Nicolás Lichtmaier <nick@debian.org>
26 .\" 2005-11-17, mtk: Substantial parts rewritten
28 .\"*******************************************************************
30 .\" This file was generated with po4a. Translate the source file.
32 .\"*******************************************************************
33 .TH STRTOK 3 2010\-09\-27 GNU "Linux Programmer's Manual"
35 strtok, strtok_r \- 文字列からトークンを取り出す
38 \fB#include <string.h>\fP
40 \fBchar *strtok(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB);\fP
42 \fBchar *strtok_r(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB, char **\fP\fIsaveptr\fP\fB);\fP
46 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
50 \fBstrtok_r\fP(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
51 _XOPEN_SOURCE || _POSIX_SOURCE
54 \fBstrtok\fP() 関数は文字列を解析してトークンに分割する。 \fBstrtok\fP() を最初に呼び出す際には、解析対象の文字列を \fIstr\fP
55 に 指定する。同じ文字列の解析を行うその後の呼び出しでは、 \fIstr\fP には NULL を指定する。
57 \fIdelim\fP 引き数には、解析する文字列をトークンに区切る文字集合を 指定する。同じ文字列を解析する一連の呼び出しにおいて、 \fIdelim\fP
60 \fBstrtok\fP() のそれぞれの呼び出しでは、次のトークンを 格納した NULL 終端された文字列へのポインタが返される。
61 この文字列には区切り文字は含まれない。 これ以上トークンが見つからなかった場合には、NULL が返される。
63 解析対象の文字列に2つ以上の区切り文字が連続している場合には、 一つの区切り文字とみなされる。
64 文字列の先頭や末尾にある区切り文字は無視される。言い換えると、 \fBstrtok\fP() が返すトークンは常に空でない文字列となる。
66 \fBstrtok_r\fP() 関数は \fBstrtok\fP() のリエントラント版である。 \fIsaveptr\fP 引き数は \fIchar *\fP
67 変数へのポインタであり、 同じ文字列の解析を行う \fBstrtok_r\fP() の呼び出し間で処理状況を保存するために \fBstrtok_r\fP()
70 \fBstrtok_r\fP() を最初に呼び出す際には、 \fIstr\fP は解析対象の文字列を指していなければならず、 \fIsaveptr\fP
71 の値は無視される。それ以降の呼び出しでは、 \fIstr\fP は NULL とし、 \fIsaveptr\fP
72 は前回の呼び出し以降変更しないようにしなければならない。
74 \fBstrtok_r\fP() の呼び出し時に異なる \fIsaveptr\fP 引き数を指定することで、 異なる文字列の解析を同時に行うことができる。
76 \fBstrtok\fP() と \fBstrtok_r\fP() は次のトークンへのポインタか、 トークンがなければ NULL を返す。
80 SVr4, POSIX.1\-2001, 4.3BSD, C89, C99.
85 これらの関数を使うのは慎重に吟味すること。 使用する場合は、以下の点に注意が必要である。
89 これらの関数は const な文字列では使えない。
93 \fBstrtok\fP() 関数は文字列の解析に静的バッファを用いるので、スレッドセーフでない。 これが問題になる場合は \fBstrtok_r\fP()
96 以下のプログラムは、 \fBstrtok_r\fP() を利用するループを入れ子にして使用し、 文字列を2階層のトークンに分割するものである。
97 1番目のコマンドライン引き数には、解析対象の文字列を指定する。 2番目の引き数には、文字列を「大きな」トークンに分割するために
98 使用する区切り文字を指定する。 3番目の引き数には、「大きな」トークンを細かく分割するために 使用する区切り文字を指定する。
104 $\fB ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq\fP
123 main(int argc, char *argv[])
125 char *str1, *str2, *token, *subtoken;
126 char *saveptr1, *saveptr2;
130 fprintf(stderr, "Usage: %s string delim subdelim\en",
135 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
136 token = strtok_r(str1, argv[2], &saveptr1);
139 printf("%d: %s\en", j, token);
141 for (str2 = token; ; str2 = NULL) {
142 subtoken = strtok_r(str2, argv[3], &saveptr2);
143 if (subtoken == NULL)
145 printf("\t \-\-> %s\en", subtoken);
153 \fBstrtok\fP() を使った別のプログラム例が \fBgetaddrinfo_a\fP(3) にある。
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)