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"
29 getsubopt \- 文字列中のサブオプション引き数の解釈を行う
31 .B #include <stdlib.h>
33 .BI "int getsubopt(char **"optionp ", char * const *" tokens \
38 .RB ( feature_test_macros (7)
46 _XOPEN_SOURCE\ >= 500 ||
47 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
49 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
57 で与えられたカンマ区切りのサブオプション・リストを解析する。
60 を使ってコマンドラインを解釈した場合に現れることが多い。
63 の \fI-o\fP オプションを見るとよい。)
64 それぞれのサブオプションには対応する値を指定することができる。
65 サブオプションの名前と対応する値は等号 (\(aq=\(aq) で区切られる。
75 引き数はトークンへのポインタの配列へのポインタで、
81 それぞれのトークンは、NULL 終端された 1文字以上の文字列で、
88 中の次の未処理のサブオプションの情報を返す。
89 サブオプション内に等号があった場合、最初の等号は
90 そのサブオプションの名前と値の区切りと解釈される。
91 区切りから次のカンマ (最後のサブオプションの場合、文字列の末尾)
95 内の名前と一致し、値を表す文字列が見つかった場合、
101 中の最初のカンマはヌルバイトで上書きされる。そのため、
103 はそのサブオプションの「値の文字列」そのものとなる。
105 サブオプションが認識されたが、値を表す文字列が見つからなかった場合、
113 ちょうど最後のサブオプションが処理された場合は、
114 文字列末尾のヌルバイト (\(aq\\0\(aq) を指している。
122 見つからなかった場合、\-1 を返す。この場合、
133 を呼び出し後のサブオプションと必ずしも同じとは限らない。
141 中に見つけたカンマを上書きするので、文字列
146 以下のプログラムは "\-o" オプションに続いてサブオプションがあることを
150 #define _XOPEN_SOURCE 500
155 int main(int argc, char **argv)
162 char *const token[] = {
177 while ((opt = getopt(argc, argv, "o:")) != \-1) {
181 while (*subopts != \(aq\\0\(aq && !errfnd) {
183 switch (getsubopt(&subopts, token, &value)) {
194 fprintf(stderr, "Missing value for "
195 "suboption \(aq%s\(aq\\n", token[NAME_OPT]);
204 fprintf(stderr, "No match found "
205 "for token: /%s/\\n", value);
210 if (readwrite && readonly) {
211 fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
212 "specified\\n", token[RO_OPT], token[RW_OPT]);
222 if (errfnd || argc == 1) {
223 fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
224 fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
225 "and \(aqname=<value>\(aq\\n");
229 /* Remainder of program... */