1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2007 Justin Pryzby <pryzbyj@justinpryzby.com>
4 .\" %%%LICENSE_START(PERMISSIVE_MISC)
5 .\" Permission is hereby granted, free of charge, to any person obtaining
6 .\" a copy of this software and associated documentation files (the
7 .\" "Software"), to deal in the Software without restriction, including
8 .\" without limitation the rights to use, copy, modify, merge, publish,
9 .\" distribute, sublicense, and/or sell copies of the Software, and to
10 .\" permit persons to whom the Software is furnished to do so, subject to
11 .\" the following conditions:
13 .\" The above copyright notice and this permission notice shall be
14 .\" included in all copies or substantial portions of the Software.
16 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 .TH getsubopt 3 (date) "Linux man-pages (unreleased)"
27 getsubopt \- parse suboption arguments from a string
30 .RI ( libc ", " \-lc )
33 .B #include <stdlib.h>
35 .BI "int getsubopt(char **restrict " optionp ", char *const *restrict " tokens ,
36 .BI " char **restrict " valuep );
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
47 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
48 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
52 parses the list of comma-separated suboptions provided in
54 (Such a suboption list is typically produced when
56 is used to parse a command line;
57 see for example the \fI\-o\fP option of
59 Each suboption may include an associated value,
60 which is separated from the suboption name by an equal sign.
61 The following is an example of the kind of string
62 that might be passed in
73 argument is a pointer to a NULL-terminated array of pointers to the tokens that
77 The tokens should be distinct, null-terminated strings containing at
78 least one character, with no embedded equal signs or commas.
82 returns information about the next unprocessed suboption in
84 The first equal sign in a suboption (if any) is interpreted as a
85 separator between the name and the value of that suboption.
86 The value extends to the next comma,
87 or (for the last suboption) to the end of the string.
88 If the name of the suboption matches a known name from
90 and a value string was found,
94 to the address of that string.
97 is overwritten with a null byte, so
99 is precisely the "value string" for that suboption.
101 If the suboption is recognized, but no value string was found,
109 points to the next suboption,
110 or to the null byte (\(aq\e0\(aq) at the end of the
111 string if the last suboption was just processed.
113 If the first suboption in
117 returns the index of the matching suboption element in
119 Otherwise, \-1 is returned and
127 is changed, the first suboption before the call to
129 is not (necessarily) the same as the first suboption after
132 For an explanation of the terms used in this section, see
140 Interface Attribute Value
143 T} Thread safety MT-Safe
149 POSIX.1-2001, POSIX.1-2008.
153 overwrites any commas it finds in the string
155 that string must be writable; it cannot be a string constant.
157 The following program expects suboptions following a "\-o" option.
159 .\" SRC BEGIN (getsubopt.c)
161 #define _XOPEN_SOURCE 500
168 main(int argc, char *argv[])
175 char *const token[] = {
190 while ((opt = getopt(argc, argv, "o:")) != \-1) {
194 while (*subopts != \(aq\e0\(aq && !errfnd) {
196 switch (getsubopt(&subopts, token, &value)) {
208 "Missing value for suboption \(aq%s\(aq\en",
219 "No match found for token: /%s/\en", value);
224 if (readwrite && readonly) {
226 "Only one of \(aq%s\(aq and \(aq%s\(aq can be specified\en",
227 token[RO_OPT], token[RW_OPT]);
237 if (errfnd || argc == 1) {
238 fprintf(stderr, "\enUsage: %s \-o <suboptstring>\en", argv[0]);
240 "suboptions are \(aqro\(aq, \(aqrw\(aq, and \(aqname=<value>\(aq\en");
244 /* Remainder of program... */