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 Nicolas Lichtmaier <nick@debian.org>
26 .\" 2005-11-17, mtk: Substantial parts rewritten
28 .\" Japanese Version Copyright (c) 1998 Ishii Tatsuo all rights reserved.
29 .\" Translated 1998-03-27, Ishii Tatsuo <rfun@azusa.shinshu-u.ac.jp>
30 .\" Updated 2000-04-05, Kentaro Shirakata <argrath@ub32.org>
31 .\" Updated 2000-09-21, Kentaro Shirakata
32 .\" Updated 2002-03-28, Kentaro Shirakata
33 .\" Updated 2005-11-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
35 .TH STRTOK 3 2008-10-29 "GNU" "Linux Programmer's Manual"
37 .\"O strtok, strtok_r \- extract tokens from strings
39 strtok, strtok_r \- ʸ»úÎ󤫤é¥È¡¼¥¯¥ó¤ò¼è¤ê½Ð¤¹
43 .B #include <string.h>
45 .BI "char *strtok(char *" str ", const char *" delim );
47 .BI "char *strtok_r(char *" str ", const char *" delim ", char **" saveptr );
51 .\"O Feature Test Macro Requirements for glibc (see
52 .\"O .BR feature_test_macros (7)):
53 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
54 .RB ( feature_test_macros (7)
60 _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE\ >=\ 1 ||
61 _XOPEN_SOURCE || _POSIX_SOURCE
67 .\"O function parses a string into a sequence of tokens.
68 .\"O On the first call to
70 .\"O the string to be parsed should be
71 .\"O specified in \fIstr\fP.
72 .\"O In each subsequent call that should parse the same string,
73 .\"O \fIstr\fP should be NULL.
75 ´Ø¿ô¤Ïʸ»úÎó¤ò²òÀϤ·¤Æ¥È¡¼¥¯¥ó¤Ëʬ³ä¤¹¤ë¡£
77 ¤òºÇ½é¤Ë¸Æ¤Ó½Ð¤¹ºÝ¤Ë¤Ï¡¢²òÀÏÂоݤÎʸ»úÎó¤ò \fIstr\fP ¤Ë
78 »ØÄꤹ¤ë¡£Æ±¤¸Ê¸»úÎó¤Î²òÀϤò¹Ô¤¦¤½¤Î¸å¤Î¸Æ¤Ó½Ð¤·¤Ç¤Ï¡¢
79 \fIstr\fP ¤Ë¤Ï NULL ¤ò»ØÄꤹ¤ë¡£
81 .\"O The \fIdelim\fP argument specifies a set of characters that
82 .\"O delimit the tokens in the parsed string.
83 .\"O The caller may specify different strings in \fIdelim\fP in successive
84 .\"O calls that parse the same string.
85 \fIdelim\fP °ú¤¿ô¤Ë¤Ï¡¢²òÀϤ¹¤ëʸ»úÎó¤ò¥È¡¼¥¯¥ó¤Ë¶èÀÚ¤ëʸ»ú½¸¹ç¤ò
86 »ØÄꤹ¤ë¡£Æ±¤¸Ê¸»úÎó¤ò²òÀϤ¹¤ë°ìÏ¢¤Î¸Æ¤Ó½Ð¤·¤Ë¤ª¤¤¤Æ¡¢
87 \fIdelim\fP ¤Ë°ã¤¦Ê¸»úÎó¤ò»ØÄꤷ¤Æ¤â¤è¤¤¡£
91 .\"O returns a pointer to a
92 .\"O null-terminated string containing the next token.
93 .\"O This string does not include the delimiting character.
94 .\"O If no more tokens are found,
98 ¤Î¤½¤ì¤¾¤ì¤Î¸Æ¤Ó½Ð¤·¤Ç¤Ï¡¢¼¡¤Î¥È¡¼¥¯¥ó¤ò
99 ³ÊǼ¤·¤¿ NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬ÊÖ¤µ¤ì¤ë¡£
100 ¤³¤Îʸ»úÎó¤Ë¤Ï¶èÀÚ¤êʸ»ú¤Ï´Þ¤Þ¤ì¤Ê¤¤¡£
101 ¤³¤ì°Ê¾å¥È¡¼¥¯¥ó¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë¤Ï¡¢NULL ¤¬ÊÖ¤µ¤ì¤ë¡£
103 .\"O A sequence of two or more contiguous delimiter characters in
104 .\"O the parsed string is considered to be a single delimiter.
105 .\"O Delimiter characters at the start or end of the string are ignored.
106 .\"O Put another way: the tokens returned by
108 .\"O are always nonempty strings.
109 ²òÀÏÂоݤÎʸ»úÎó¤Ë2¤Ä°Ê¾å¤Î¶èÀÚ¤êʸ»ú¤¬Ï¢Â³¤·¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¡¢
110 °ì¤Ä¤Î¶èÀÚ¤êʸ»ú¤È¤ß¤Ê¤µ¤ì¤ë¡£
111 ʸ»úÎó¤ÎÀèƬ¤äËöÈø¤Ë¤¢¤ë¶èÀÚ¤êʸ»ú¤Ï̵»ë¤µ¤ì¤ë¡£¸À¤¤´¹¤¨¤ë¤È¡¢
113 ¤¬ÊÖ¤¹¥È¡¼¥¯¥ó¤Ï¾ï¤Ë¶õ¤Ç¤Ê¤¤Ê¸»úÎó¤È¤Ê¤ë¡£
117 .\"O function is a reentrant version
119 .\"O The \fIsaveptr\fP argument is a pointer to a
120 .\"O \fIchar *\fP variable that is used internally by
122 .\"O in order to maintain context between successive calls that parse the
127 ¤Î¥ê¥¨¥ó¥È¥é¥ó¥ÈÈǤǤ¢¤ë¡£
128 \fIsaveptr\fP °ú¤¿ô¤Ï \fIchar *\fP ÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ê¡¢
129 Ʊ¤¸Ê¸»úÎó¤Î²òÀϤò¹Ô¤¦
131 ¤Î¸Æ¤Ó½Ð¤·´Ö¤Ç½èÍý¾õ¶·¤òÊݸ¤¹¤ë¤¿¤á¤Ë
135 .\"O On the first call to
136 .\"O .BR strtok_r (),
138 .\"O should point to the string to be parsed, and the value of
141 .\"O In subsequent calls, \fIstr\fP should be NULL, and
142 .\"O \fIsaveptr\fP should be unchanged since the previous call.
144 ¤òºÇ½é¤Ë¸Æ¤Ó½Ð¤¹ºÝ¤Ë¤Ï¡¢
146 ¤Ï²òÀÏÂоݤÎʸ»úÎó¤ò»Ø¤·¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤º¡¢
148 ¤ÎÃͤÏ̵»ë¤µ¤ì¤ë¡£¤½¤ì°Ê¹ß¤Î¸Æ¤Ó½Ð¤·¤Ç¤Ï¡¢
152 ¤ÏÁ°²ó¤Î¸Æ¤Ó½Ð¤·°Ê¹ßÊѹ¹¤·¤Ê¤¤¤è¤¦¤Ë¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
154 .\"O Different strings may be parsed concurrently using sequences of calls to
156 .\"O that specify different \fIsaveptr\fP arguments.
158 ¤Î¸Æ¤Ó½Ð¤·»þ¤Ë°Û¤Ê¤ë \fIsaveptr\fP °ú¤¿ô¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢
159 °Û¤Ê¤ëʸ»úÎó¤Î²òÀϤòƱ»þ¤Ë¹Ô¤¦¤³¤È¤¬¤Ç¤¤ë¡£
160 .\"O .SH "RETURN VALUE"
166 .\"O functions return a pointer to
167 .\"O the next token, or NULL if there are no more tokens.
171 ¤Ï¼¡¤Î¥È¡¼¥¯¥ó¤Ø¤Î¥Ý¥¤¥ó¥¿¤«¡¢
172 ¥È¡¼¥¯¥ó¤¬¤Ê¤±¤ì¤Ð NULL ¤òÊÖ¤¹¡£
173 .\"O .SH "CONFORMING TO"
177 SVr4, POSIX.1-2001, 4.3BSD, C89, C99.
183 .\"O Be cautious when using these functions.
184 .\"O If you do use them, note that:
185 ¤³¤ì¤é¤Î´Ø¿ô¤ò»È¤¦¤Î¤Ï¿µ½Å¤Ë¶ãÌ£¤¹¤ë¤³¤È¡£
186 »ÈÍѤ¹¤ë¾ì¹ç¤Ï¡¢°Ê²¼¤ÎÅÀ¤ËÃí°Õ¤¬É¬ÍפǤ¢¤ë¡£
188 .\"O These functions modify their first argument.
189 ¤³¤ì¤é¤Î´Ø¿ô¤Ï¤½¤ÎºÇ½é¤Î°ú¿ô¤òÊѹ¹¤¹¤ë¡£
191 .\"O These functions cannot be used on constant strings.
192 ¤³¤ì¤é¤Î´Ø¿ô¤Ï const ¤Êʸ»úÎó¤Ç¤Ï»È¤¨¤Ê¤¤¡£
194 .\"O The identity of the delimiting character is lost.
195 ¶èÀÚ¤êʸ»ú¼«ÂΤϼº¤ï¤ì¤Æ¤·¤Þ¤¦¡£
199 .\"O function uses a static buffer while parsing, so it's not thread safe.
202 .\"O if this matters to you.
204 ´Ø¿ô¤Ïʸ»úÎó¤Î²òÀϤËÀÅŪ¥Ð¥Ã¥Õ¥¡¤òÍѤ¤¤ë¤Î¤Ç¡¢¥¹¥ì¥Ã¥É¥»¡¼¥Õ¤Ç¤Ê¤¤¡£
205 ¤³¤ì¤¬ÌäÂê¤Ë¤Ê¤ë¾ì¹ç¤Ï
210 .\"O The program below uses nested loops that employ
212 .\"O to break a string into a two-level hierarchy of tokens.
213 .\"O The first command-line argument specifies the string to be parsed.
214 .\"O The second argument specifies the delimiter character(s)
215 .\"O to be used to separate that string into "major" tokens.
216 .\"O The third argument specifies the delimiter character(s)
217 .\"O to be used to separate the "major" tokens into subtokens.
220 ¤òÍøÍѤ¹¤ë¥ë¡¼¥×¤òÆþ¤ì»Ò¤Ë¤·¤Æ»ÈÍѤ·¡¢
221 ʸ»úÎó¤ò2³¬ÁؤΥȡ¼¥¯¥ó¤Ëʬ³ä¤¹¤ë¤â¤Î¤Ç¤¢¤ë¡£
222 1ÈÖÌܤΥ³¥Þ¥ó¥É¥é¥¤¥ó°ú¤¿ô¤Ë¤Ï¡¢²òÀÏÂоݤÎʸ»úÎó¤ò»ØÄꤹ¤ë¡£
223 2ÈÖÌܤΰú¤¿ô¤Ë¤Ï¡¢Ê¸»úÎó¤ò¡ÖÂ礤ʡץȡ¼¥¯¥ó¤Ëʬ³ä¤¹¤ë¤¿¤á¤Ë
224 »ÈÍѤ¹¤ë¶èÀÚ¤êʸ»ú¤ò»ØÄꤹ¤ë¡£
225 3ÈÖÌܤΰú¤¿ô¤Ë¤Ï¡¢¡ÖÂ礤ʡץȡ¼¥¯¥ó¤òºÙ¤«¤¯Ê¬³ä¤¹¤ë¤¿¤á¤Ë
226 »ÈÍѤ¹¤ë¶èÀÚ¤êʸ»ú¤ò»ØÄꤹ¤ë¡£
229 .\"O An example of the output produced by this program is the following:
230 ¤³¤Î¥×¥í¥°¥é¥à¤Î½ÐÎÏÎã¤ò°Ê²¼¤Ë¼¨¤¹¡£
234 .RB "$" " ./a.out \(aqa/bbb///cc;xxx:yyy:\(aq \(aq:;\(aq \(aq/\(aq"
245 .\"O .SS Program source
246 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
254 main(int argc, char *argv[])
256 char *str1, *str2, *token, *subtoken;
257 char *saveptr1, *saveptr2;
261 fprintf(stderr, "Usage: %s string delim subdelim\\n",
266 for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
267 token = strtok_r(str1, argv[2], &saveptr1);
270 printf("%d: %s\\n", j, token);
272 for (str2 = token; ; str2 = NULL) {
273 subtoken = strtok_r(str2, argv[3], &saveptr2);
274 if (subtoken == NULL)
276 printf("\t \-\-> %s\\n", subtoken);