1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2007 Justin Pryzby <pryzbyj@justinpryzby.com>
4 .\" Permission is hereby granted, free of charge, to any person obtaining
5 .\" a copy of this software and associated documentation files (the
6 .\" "Software"), to deal in the Software without restriction, including
7 .\" without limitation the rights to use, copy, modify, merge, publish,
8 .\" distribute, sublicense, and/or sell copies of the Software, and to
9 .\" permit persons to whom the Software is furnished to do so, subject to
10 .\" the following conditions:
12 .\" The above copyright notice and this permission notice shall be
13 .\" included in all copies or substantial portions of the Software.
15 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 .\" Japanese Version Copyright (c) 2007 Akihiro MOTOKI
24 .\" all rights reserved.
25 .\" Translated 2007-06-02, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
27 .TH GETSUBOPT 3 2010-09-26 "GNU" "Linux Programmer's Manual"
30 .\"O getsubopt \- parse suboption arguments from a string
31 getsubopt \- 文字列中のサブオプション引き数の解釈を行う
34 .B #include <stdlib.h>
36 .BI "int getsubopt(char **"optionp ", char * const *" tokens \
40 .\"O Feature Test Macro Requirements for glibc (see
41 .\"O .BR feature_test_macros (7)):
43 .RB ( feature_test_macros (7)
51 _XOPEN_SOURCE\ >= 500 ||
52 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
54 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
61 .\"O parses the list of comma-separated suboptions provided in
63 .\"O (Such a suboption list is typically produced when
65 .\"O is used to parse a command line;
66 .\"O see for example the \fI-o\fP option of
68 .\"O Each suboption may include an associated value,
69 .\"O which is separated from the suboption name by an equal sign.
70 .\"O The following is an example of the kind of string
71 .\"O that might be passed in
76 で与えられたカンマ区切りのサブオプション・リストを解析する。
79 を使ってコマンドラインを解釈した場合に現れることが多い。
82 の \fI-o\fP オプションを見るとよい。)
83 それぞれのサブオプションには対応する値を指定することができる。
84 サブオプションの名前と対応する値は等号 (\(aq=\(aq) で区切られる。
95 .\"O argument is a pointer to a NULL-terminated array of pointers to the tokens that
99 .\"O The tokens should be distinct, null-terminated strings containing at
100 .\"O least one character, with no embedded equal signs or commas.
102 引き数はトークンへのポインタの配列へのポインタで、
108 それぞれのトークンは、NULL 終端された 1文字以上の文字列で、
113 .\"O .BR getsubopt ()
114 .\"O returns information about the next unprocessed suboption in
119 中の次の未処理のサブオプションの情報を返す。
120 .\"O The first equal sign in a suboption (if any) is interpreted as a
121 .\"O separator between the name and the value of that suboption.
122 .\"O The value extends to the next comma,
123 .\"O or (for the last suboption) to the end of the string.
124 サブオプション内に等号があった場合、最初の等号は
125 そのサブオプションの名前と値の区切りと解釈される。
126 区切りから次のカンマ (最後のサブオプションの場合、文字列の末尾)
128 .\"O If the name of the suboption matches a known name from
130 .\"O and a value string was found,
131 .\"O .BR getsubopt ()
134 .\"O to the address of that string.
137 内の名前と一致し、値を表す文字列が見つかった場合、
142 .\"O The first comma in
144 .\"O is overwritten with a null byte, so
146 .\"O is precisely the "value string" for that suboption.
148 中の最初のカンマはヌルバイトで上書きされる。そのため、
150 はそのサブオプションの「値の文字列」そのものとなる。
152 .\"O If the suboption is recognized, but no value string was found,
155 サブオプションが認識されたが、値を表す文字列が見つからなかった場合、
160 .\"O .BR getsubopt ()
163 .\"O points to the next suboption,
164 .\"O or to the null byte (\(aq\\0\(aq) at the end of the
165 .\"O string if the last suboption was just processed.
170 ちょうど最後のサブオプションが処理された場合は、
171 文字列末尾のヌルバイト (\(aq\\0\(aq) を指している。
172 .\"O .SH RETURN VALUE
174 .\"O If the first suboption in
177 .\"O .BR getsubopt ()
178 .\"O returns the index of the matching suboption element in
180 .\"O Otherwise, \-1 is returned and
183 .\"O .IB name [= value ]
191 見つからなかった場合、\-1 を返す。この場合、
199 .\"O is changed, the first suboption before the call to
200 .\"O .BR getsubopt ()
201 .\"O is not (necessarily) the same as the first suboption after
202 .\"O .BR getsubopt ().
208 を呼び出し後のサブオプションと必ずしも同じとは限らない。
209 .\"O .SH CONFORMING TO
216 .\"O .BR getsubopt ()
217 .\"O overwrites any commas it finds in the string
219 .\"O that string must be writable; it cannot be a string constant.
223 中に見つけたカンマを上書きするので、文字列
229 .\"O The following program expects suboptions following a "\-o" option.
230 以下のプログラムは "\-o" オプションに続いてサブオプションがあることを
234 #define _XOPEN_SOURCE 500
239 int main(int argc, char **argv)
246 char *const token[] = {
261 while ((opt = getopt(argc, argv, "o:")) != \-1) {
265 while (*subopts != \(aq\\0\(aq && !errfnd) {
267 switch (getsubopt(&subopts, token, &value)) {
278 fprintf(stderr, "Missing value for "
279 "suboption \(aq%s\(aq\\n", token[NAME_OPT]);
288 fprintf(stderr, "No match found "
289 "for token: /%s/\\n", value);
294 if (readwrite && readonly) {
295 fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
296 "specified\\n", token[RO_OPT], token[RW_OPT]);
306 if (errfnd || argc == 1) {
307 fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
308 fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
309 "and \(aqname=<value>\(aq\\n");
313 /* Remainder of program... */