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 .TH GETSUBOPT 3 2010-09-26 "GNU" "Linux Programmer's Manual"
25 getsubopt \- parse suboption arguments from a string
27 .B #include <stdlib.h>
29 .BI "int getsubopt(char **"optionp ", char * const *" tokens \
33 Feature Test Macro Requirements for glibc (see
34 .BR feature_test_macros (7)):
41 _XOPEN_SOURCE\ >= 500 ||
42 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
44 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
50 parses the list of comma-separated suboptions provided in
52 (Such a suboption list is typically produced when
54 is used to parse a command line;
55 see for example the \fI-o\fP option of
57 Each suboption may include an associated value,
58 which is separated from the suboption name by an equal sign.
59 The following is an example of the kind of string
60 that might be passed in
69 argument is a pointer to a NULL-terminated list of the tokens that
73 The tokens should be distinct, null-terminated strings containing at
74 least one character, with no embedded equal signs or commas.
78 returns information about the next unprocessed suboption in
80 The first equal sign in a suboption (if any) is interpreted as a
81 separator between the name and the value of that suboption.
82 The value extends to the next comma,
83 or (for the last suboption) to the end of the string.
84 If the name of the suboption matches a known name from
86 and a value string was found,
90 to the address of that string.
93 is overwritten with a null byte, so
95 is precisely the "value string" for that suboption.
97 If the suboption is recognized, but no value string was found,
105 points to the next suboption,
106 or to the null byte ('\\0') at the end of the
107 string if the last suboption was just processed.
109 If the first suboption in
113 returns the index of the matching suboption element in
115 Otherwise, \-1 is returned and
123 is changed, the first suboption before the call to
125 is not (necessarily) the same as the first suboption after
133 overwrites any commas it finds in the string
135 that string must be writable; it cannot be a string constant.
137 The following program expects suboptions following a "\-o" option.
140 #define _XOPEN_SOURCE 500
145 int main(int argc, char **argv)
152 char *const token[] = {
167 while ((opt = getopt(argc, argv, "o:")) != \-1) {
171 while (*subopts != \(aq\\0\(aq && !errfnd) {
173 switch (getsubopt(&subopts, token, &value)) {
184 fprintf(stderr, "Missing value for "
185 "suboption \(aq%s\(aq\\n", token[NAME_OPT]);
194 fprintf(stderr, "No match found "
195 "for token: /%s/\\n", value);
200 if (readwrite && readonly) {
201 fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
202 "specified\\n", token[RO_OPT], token[RW_OPT]);
212 if (errfnd || argc == 1) {
213 fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
214 fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
215 "and \(aqname=<value>\(aq\\n");
219 /* Remainder of program... */