1 .\" Copyright (C) 2007 Michael Kerrisk <mtk-manpages@gmx.net>
2 .\" and (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 2007-05-05 "GNU" "Linux Programmer's Manual"
25 getsubopt \- parse suboption arguments from a string
27 .B #define _XOPEN_SOURCE 500
29 .B #include <stdlib.h>
39 parses the list of comma-separated suboptions provided in
41 (Such a suboption list is typically produced when
43 is used to parse a command line;
44 see for example the \fI-o\fP option of
46 Each suboption may include an associated value,
47 which is separated from the suboption name by an equal sign.
48 The following is an example of the kind of string
49 that might be passed in
58 argument is a pointer to a NULL-terminated list of the tokens that
62 The tokens should be distinct, null-terminated strings containing at
63 least one character, with no embedded equal signs or commas.
67 returns information about the next unprocessed suboption in
69 The first equal sign in a suboption (if any) is interpreted as a
70 separator between the name and the value of that suboption.
71 The value extends to the next comma,
72 or (for the last suboption) to the end of the string.
73 If the name of the suboption matches a known name from
75 and a value string was found,
79 to the address of that string.
82 is overwritten with a null byte, so
84 is precisely the "value string" for that suboption.
86 If the suboption is recognized, but no value string was found,
94 points to the next suboption, or to the null character at the end of the
95 string if the last suboption was just processed.
97 If the first suboption in
101 returns the index of the matching suboption element in
103 Otherwise, \-1 is returned and
111 is changed, the first suboption before the call to
113 is not (necessarily) the same as the first suboption after
121 overwrites any commas it finds in the string
123 that string must be writable; it cannot be a string constant.
125 The following program excepts suboptions following a "\-o" option.
128 #define _XOPEN_SOURCE 500
133 int main(int argc, char **argv)
140 char *const token[] = {
155 while ((opt = getopt(argc, argv, "o:")) != \-1) {
159 while (*subopts != '\\0' && !errfnd) {
161 switch (getsubopt(&subopts, token, &value)) {
172 fprintf(stderr, "Missing value for "
173 "suboption '%s'\\n", token[NAME_OPT]);
182 fprintf(stderr, "No match found "
183 "for token: /%s/\\n", value);
188 if (readwrite && readonly) {
189 fprintf(stderr, "Only one of '%s' and '%s' can be "
190 "specified\\n", token[RO_OPT], token[RW_OPT]);
200 if (errfnd || argc == 1) {
201 fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
202 fprintf(stderr, "suboptions are 'ro', 'rw', "
203 "and 'name=<value>'\\n");
207 /* Remainder of program... */
214 .BR feature_test_macros (7)