2 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" and Copyright (C) 2007 Justin Pryzby <pryzbyj@justinpryzby.com>
5 .\" %%%LICENSE_START(PERMISSIVE_MISC)
6 .\" Permission is hereby granted, free of charge, to any person obtaining
7 .\" a copy of this software and associated documentation files (the
8 .\" "Software"), to deal in the Software without restriction, including
9 .\" without limitation the rights to use, copy, modify, merge, publish,
10 .\" distribute, sublicense, and/or sell copies of the Software, and to
11 .\" permit persons to whom the Software is furnished to do so, subject to
12 .\" the following conditions:
14 .\" The above copyright notice and this permission notice shall be
15 .\" included in all copies or substantial portions of the Software.
17 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 .TH getsubopt 3 (date) "Linux man-pages (unreleased)"
28 getsubopt \- parse suboption arguments from a string
31 .RI ( libc ", " \-lc )
34 .B #include <stdlib.h>
36 .BI "int getsubopt(char **restrict " optionp ", char *const *restrict " tokens ,
37 .BI " char **restrict " valuep );
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
48 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
49 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
53 parses the list of comma-separated suboptions provided in
55 (Such a suboption list is typically produced when
57 is used to parse a command line;
58 see for example the \fI\-o\fP option of
60 Each suboption may include an associated value,
61 which is separated from the suboption name by an equal sign.
62 The following is an example of the kind of string
63 that might be passed in
74 argument is a pointer to a NULL-terminated array of pointers to the tokens that
78 The tokens should be distinct, null-terminated strings containing at
79 least one character, with no embedded equal signs or commas.
83 returns information about the next unprocessed suboption in
85 The first equal sign in a suboption (if any) is interpreted as a
86 separator between the name and the value of that suboption.
87 The value extends to the next comma,
88 or (for the last suboption) to the end of the string.
89 If the name of the suboption matches a known name from
91 and a value string was found,
95 to the address of that string.
98 is overwritten with a null byte, so
100 is precisely the "value string" for that suboption.
102 If the suboption is recognized, but no value string was found,
110 points to the next suboption,
111 or to the null byte (\[aq]\e0\[aq]) at the end of the
112 string if the last suboption was just processed.
114 If the first suboption in
118 returns the index of the matching suboption element in
120 Otherwise, \-1 is returned and
128 is changed, the first suboption before the call to
130 is not (necessarily) the same as the first suboption after
133 For an explanation of the terms used in this section, see
141 Interface Attribute Value
144 T} Thread safety MT-Safe
156 overwrites any commas it finds in the string
158 that string must be writable; it cannot be a string constant.
160 The following program expects suboptions following a "\-o" option.
162 .\" SRC BEGIN (getsubopt.c)
164 #define _XOPEN_SOURCE 500
171 main(int argc, char *argv[])
178 char *const token[] = {
193 while ((opt = getopt(argc, argv, "o:")) != \-1) {
197 while (*subopts != \[aq]\e0\[aq] && !errfnd) {
199 switch (getsubopt(&subopts, token, &value)) {
211 "Missing value for suboption \[aq]%s\[aq]\en",
222 "No match found for token: /%s/\en", value);
227 if (readwrite && readonly) {
229 "Only one of \[aq]%s\[aq] and \[aq]%s\[aq] can be specified\en",
230 token[RO_OPT], token[RW_OPT]);
240 if (errfnd || argc == 1) {
241 fprintf(stderr, "\enUsage: %s \-o <suboptstring>\en", argv[0]);
243 "suboptions are \[aq]ro\[aq], \[aq]rw\[aq], and \[aq]name=<value>\[aq]\en");
247 /* Remainder of program... */