File: strtok.3

package info (click to toggle)
manpages-ja 0.5.0.0.20120606-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 25,964 kB
  • sloc: perl: 161; makefile: 116
file content (167 lines) | stat: -rw-r--r-- 7,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
.\" Copyright (C) 1996 Andries Brouwer (aeb@cwi.nl)
.\" and Copyright (C) 2005 Michael Kerrisk (mtk.manpages@gmail.com)
.\"
.\" 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.
.\"
.\" Rewritten old page, 960210, aeb@cwi.nl
.\" Updated, added strtok_r. 2000-02-13 Nicolás Lichtmaier <nick@debian.org>
.\" 2005-11-17, mtk: Substantial parts rewritten
.\"
.\"*******************************************************************
.\"
.\" This file was generated with po4a. Translate the source file.
.\"
.\"*******************************************************************
.TH STRTOK 3 2012\-05\-10 GNU "Linux Programmer's Manual"
.SH 名前
strtok, strtok_r \- 文字列からトークンを取り出す
.SH 書式
.nf
\fB#include <string.h>\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()  関数は文字列を解析してトークンに分割する。 \fBstrtok\fP()  を最初に呼び出す際には、解析対象の文字列を \fIstr\fP
に 指定する。同じ文字列の解析を行うその後の呼び出しでは、 \fIstr\fP には NULL を指定する。

\fIdelim\fP 引き数には、解析対象の文字列をトークンに区切るのに使用する
バイト集合を指定する。同じ文字列を解析する一連の呼び出しにおいて、
\fIdelim\fP に違う文字列を指定してもよい。

\fBstrtok\fP() のそれぞれの呼び出しでは、次のトークンを格納した NULL 終端
された文字列へのポインタが返される。この文字列には区切りバイトは含まれ
ない。これ以上トークンが見つからなかった場合には、NULL が返される。

解析対象の文字列に 2 つ以上の区切りバイトが連続している場合には、
一つの区切りバイトとみなされる。 文字列の先頭や末尾にある区切りバイトは
無視される。言い換えると、 \fBstrtok\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 準拠
.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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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.41 の一部
である。プロジェクトの説明とバグ報告に関する情報は
http://www.kernel.org/doc/man\-pages/ に書かれている。