.\" Copyright (C) 2005, 2013 Michael Kerrisk (mtk.manpages@gmail.com) .\" a few fragments from an earlier (1996) version by .\" Andries Brouwer (aeb@cwi.nl) remain. .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" Rewritten old page, 960210, aeb@cwi.nl .\" Updated, added strtok_r. 2000-02-13 Nicolás Lichtmaier .\" 2005-11-17, mtk: Substantial parts rewritten .\" 2013-05-19, mtk: added much further detail on the operation of strtok() .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1998 Ishii Tatsuo all rights reserved. .\" Translated 1998-03-27, Ishii Tatsuo .\" Updated 2000-04-05, Kentaro Shirakata .\" Updated 2000-09-21, Kentaro Shirakata .\" Updated 2002-03-28, Kentaro Shirakata .\" Updated 2005-11-19, Akihiro MOTOKI .\" Updated 2012-05-29, Akihiro MOTOKI .\" Updated 2013-07-22, Akihiro MOTOKI .\" Updated 2013-07-31, Akihiro MOTOKI .\" .TH STRTOK 3 2013\-05\-19 GNU "Linux Programmer's Manual" .SH 名前 strtok, strtok_r \- 文字列からトークンを取り出す .SH 書式 .nf \fB#include \fP .sp \fBchar *strtok(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB);\fP .sp \fBchar *strtok_r(char *\fP\fIstr\fP\fB, const char *\fP\fIdelim\fP\fB, char **\fP\fIsaveptr\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .in .sp .ad l \fBstrtok_r\fP(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE .ad b .SH 説明 \fBstrtok\fP() 関数は文字列を 0 個以上の空でないトークンの列に分割する。 \fBstrtok\fP() を最初に呼び出す際には、解析対象の文字列を \fIstr\fP に 指定する。同じ文字列の解析を行うその後の呼び出しでは、 \fIstr\fP は NULL にしなければならない。 \fIdelim\fP 引き数には、解析対象の文字列をトークンに区切るのに使用する バイト集合を指定する。同じ文字列を解析する一連の呼び出しにおいて、 \fIdelim\fP に違う文字列を指定してもよい。 \fBstrtok\fP() のそれぞれの呼び出しでは、次のトークンを格納したヌル終端 された文字列へのポインターが返される。この文字列には区切りバイトは含まれ ない。これ以上トークンが見つからなかった場合には、NULL が返される。 同じ文字列に対して操作を行う \fBstrtok\fP() を連続して呼び出す場合、 次のトークンを探し始める位置を決めるためのポインターが保持される。 最初の \fBstrtok\fP の呼び出しでは、 このポインターは対象の文字列の最初のバイトにセットされる。 次のトークンの先頭は、 \fIstr\fP 内で次の区切りバイト以外のバイトを前方に検索して決定される。 区切りバイト以外のバイトが見つからなかった場合は、 トークンはこれ以上なく、 \fBstrtok\fP() は NULL を返す (したがって、 空の文字列や区切りバイトだけを含む文字列の場合には、 最初の \fBstrtok\fP() の呼び出しで NULL が返ることになる)。 各トークンの末尾は、次の区切りバイトが見つかるか、終端のヌルバイト (\(aq\e0\(aq) に達するまで文字列を前方に検索することで見つかる。 区切りバイトが見つかった場合には、 現在のトークンの終わりを示すために、 見つかった区切りバイトがヌルバイトで上書きされ、 \fBstrtok\fP() はポインターを次のバイトに設定する。 このポインターは、次のトークンを検索する際の開始点として使用される。 この場合、 \fBstrtok\fP() は見つかったトークンの先頭へのポインターを返す。 上記の説明の通り、 解析対象の文字列に 2 つ以上の区切りバイトが連続している場合には、 一つの区切りバイトとみなされ、 文字列の先頭や末尾にある区切りバイトは無視される。 言い換えると、 \fBstrtok\fP() が返すトークンは必ず空でない文字列となる。 したがって、例えば "\fIaaa;;bbb,\fP" という文字列が与えられたとすると、 区切り文字列 "\fI;,\fP" を指定した一連の \fBstrtok\fP() の呼び出しでは、 "\fIaaa\fP" と \fIbbb\fP" が返り、その次にヌルポインターが返る。 \fBstrtok_r\fP() 関数は \fBstrtok\fP() のリエントラント版である。 \fIsaveptr\fP 引き数は \fIchar\ *\fP 変数へのポインターであり、 同じ文字列の解析を行う \fBstrtok_r\fP() の呼び出し間で処理状況を保存するために \fBstrtok_r\fP() 内部で使用される。 \fBstrtok_r\fP() を最初に呼び出す際には、 \fIstr\fP は解析対象の文字列を指していなければならず、 \fIsaveptr\fP の値は無視される。それ以降の呼び出しでは、 \fIstr\fP は NULL とし、 \fIsaveptr\fP は前回の呼び出し以降変更しないようにしなければならない。 \fBstrtok_r\fP() の呼び出し時に異なる \fIsaveptr\fP 引き数を指定することで、 異なる文字列の解析を同時に行うことができる。 .SH 返り値 \fBstrtok\fP() と \fBstrtok_r\fP() は次のトークンへのポインターか、 トークンがなければ NULL を返す。 .SH 属性 .SS "マルチスレッディング (pthreads(7) 参照)" The \fBstrtok\fP() 関数はスレッドセーフではない。 .LP \fBstrtok_r\fP() 関数はスレッドセーフである。 .SH 準拠 .TP \fBstrtok\fP() SVr4, POSIX.1\-2001, 4.3BSD, C89, C99. .TP \fBstrtok_r\fP() POSIX.1\-2001. .SH バグ これらの関数を使うのは慎重に吟味すること。 使用する場合は、以下の点に注意が必要である。 .IP * 2 これらの関数はその最初の引数を変更する。 .IP * これらの関数は const な文字列では使えない。 .IP * 区切りバイト自体は失われてしまう。 .IP * \fBstrtok\fP() 関数は文字列の解析に静的バッファーを用いるので、スレッドセーフでない。 これが問題になる場合は \fBstrtok_r\fP() を用いること。 .SH 例 以下のプログラムは、 \fBstrtok_r\fP() を利用するループを入れ子にして使用し、 文字列を 2 階層のトークンに分割するものである。 1番目のコマンドライン 引き数には、解析対象の文字列を指定する。 2 番目の引き数には、文字列を 「大きな」トークンに分割するために 使用する区切りバイトを指定する。 3 番目の引き数には、「大きな」トークンを細かく分割するために使用する 区切りバイトを指定する。 .PP このプログラムの出力例を以下に示す。 .PP .in +4n .nf $\fB ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq\fP 1: a/bbb///cc \-\-> a \-\-> bbb \-\-> cc 2: xxx \-\-> xxx 3: yyy \-\-> yyy .fi .in .SS プログラムのソース \& .nf #include #include #include int main(int argc, char *argv[]) { char *str1, *str2, *token, *subtoken; char *saveptr1, *saveptr2; int j; if (argc != 4) { fprintf(stderr, "Usage: %s string delim subdelim\en", argv[0]); exit(EXIT_FAILURE); } for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) { token = strtok_r(str1, argv[2], &saveptr1); if (token == NULL) break; printf("%d: %s\en", j, token); for (str2 = token; ; str2 = NULL) { subtoken = strtok_r(str2, argv[3], &saveptr2); if (subtoken == NULL) break; printf("\t \-\-> %s\en", subtoken); } } exit(EXIT_SUCCESS); } .fi .PP \fBstrtok\fP() を使った別のプログラム例が \fBgetaddrinfo_a\fP(3) にある。 .SH 関連項目 \fBindex\fP(3), \fBmemchr\fP(3), \fBrindex\fP(3), \fBstrchr\fP(3), \fBstring\fP(3), \fBstrpbrk\fP(3), \fBstrsep\fP(3), \fBstrspn\fP(3), \fBstrstr\fP(3), \fBwcstok\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。