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)):
42 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
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 ¤ÇÍ¿¤¨¤é¤ì¤¿¥«¥ó¥Þ¶èÀÚ¤ê¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¡¦¥ê¥¹¥È¤ò²òÀϤ¹¤ë¡£
77 (¤³¤Î¤è¤¦¤Ê¥µ¥Ö¥ª¥×¥·¥ç¥ó¡¦¥ê¥¹¥È¤Ï
79 ¤ò»È¤Ã¤Æ¥³¥Þ¥ó¥É¥é¥¤¥ó¤ò²ò¼á¤·¤¿¾ì¹ç¤Ë¸½¤ì¤ë¤³¤È¤¬Â¿¤¤¡£
82 ¤Î \fI-o\fP ¥ª¥×¥·¥ç¥ó¤ò¸«¤ë¤È¤è¤¤¡£)
83 ¤½¤ì¤¾¤ì¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Ë¤ÏÂбþ¤¹¤ëÃͤò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤ë¡£
84 ¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î̾Á°¤ÈÂбþ¤¹¤ëÃͤÏÅù¹æ (\(aq=\(aq) ¤Ç¶èÀÚ¤é¤ì¤ë¡£
85 Î㤨¤Ð¡¢°Ê²¼¤Î¤è¤¦¤Êʸ»úÎó¤ò
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 °ú¤¿ô¤Ï¥È¡¼¥¯¥ó¤Ø¤Î¥Ý¥¤¥ó¥¿¤ÎÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¡¢
103 ÇÛÎó¤Ï NULL ¤Ç½ªÃ¼¤µ¤ì¤ë¡£
108 ¤½¤ì¤¾¤ì¤Î¥È¡¼¥¯¥ó¤Ï¡¢NULL ½ªÃ¼¤µ¤ì¤¿ 1ʸ»ú°Ê¾å¤Îʸ»úÎó¤Ç¡¢
109 ¾¤Î¥È¡¼¥¯¥ó¤È¶èÊ̤Ǥ¤ëɬÍפ¬¤¢¤ë¡£
110 ¤Þ¤¿¡¢Åù¹æ¤È¥«¥ó¥Þ¤ò´Þ¤ó¤Ç¤Ï¤Ê¤é¤Ê¤¤¡£
113 .\"O .BR getsubopt ()
114 .\"O returns information about the next unprocessed suboption in
117 ¤Ï¸Æ¤Ó½Ð¤µ¤ì¤ë¤¿¤Ó¤Ë¡¢
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 ¶èÀڤ꤫¤é¼¡¤Î¥«¥ó¥Þ (ºÇ¸å¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î¾ì¹ç¡¢Ê¸»úÎó¤ÎËöÈø)
127 ¤Þ¤Ç¤¬¡¢¥µ¥Ö¥ª¥×¥·¥ç¥ó¤ÎÃͤȤʤ롣
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.
135 ¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Î̾Á°¤¬
137 Æâ¤Î̾Á°¤È°ìÃפ·¡¢Ãͤòɽ¤¹Ê¸»úÎ󤬸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
141 ¤òÃͤòɽ¤¹Ê¸»úÎó¤Î¥¢¥É¥ì¥¹¤ËÀßÄꤹ¤ë¡£
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 ¥µ¥Ö¥ª¥×¥·¥ç¥ó¤¬Ç§¼±¤µ¤ì¤¿¤¬¡¢Ãͤòɽ¤¹Ê¸»úÎ󤬸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
157 ¤Ï NULL ¤ËÀßÄꤵ¤ì¤ë¡£
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.
169 ¤Ï¼¡¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤ò»Ø¤·¤Æ¤¤¤ë¡£
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 ]
186 Æâ¤Ç¥µ¥Ö¥ª¥×¥·¥ç¥ó¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
188 ¤ÏºÇ½é¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Ë¥Þ¥Ã¥Á¤¹¤ë
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 ().
206 ¤ò¸Æ¤Ó½Ð¤¹Á°¤ÎºÇ½é¤Î¥µ¥Ö¥ª¥×¥·¥ç¥ó¤Ï
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 Ãæ¤Ë¸«¤Ä¤±¤¿¥«¥ó¥Þ¤ò¾å½ñ¤¤¹¤ë¤Î¤Ç¡¢Ê¸»úÎó
225 ¤Ï½ñ¤¹þ¤ß²Äǽ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤º¡¢
226 ʸ»úÎóÄê¿ô¤Ë¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£
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... */