]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getsubopt.3
50b392400109c436f7f70fa941bc4e3222128cc3
[thirdparty/man-pages.git] / man3 / getsubopt.3
1 '\" t
2 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" and Copyright (C) 2007 Justin Pryzby <pryzbyj@justinpryzby.com>
4 .\"
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:
13 .\"
14 .\" The above copyright notice and this permission notice shall be
15 .\" included in all copies or substantial portions of the Software.
16 .\"
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.
24 .\" %%%LICENSE_END
25 .\"
26 .TH getsubopt 3 (date) "Linux man-pages (unreleased)"
27 .SH NAME
28 getsubopt \- parse suboption arguments from a string
29 .SH LIBRARY
30 Standard C library
31 .RI ( libc ", " \-lc )
32 .SH SYNOPSIS
33 .nf
34 .B #include <stdlib.h>
35 .PP
36 .BI "int getsubopt(char **restrict " optionp ", char *const *restrict " tokens ,
37 .BI " char **restrict " valuep );
38 .fi
39 .PP
40 .RS -4
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
43 .RE
44 .PP
45 .BR getsubopt ():
46 .nf
47 _XOPEN_SOURCE >= 500
48 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
49 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
50 .fi
51 .SH DESCRIPTION
52 .BR getsubopt ()
53 parses the list of comma-separated suboptions provided in
54 .IR optionp .
55 (Such a suboption list is typically produced when
56 .BR getopt (3)
57 is used to parse a command line;
58 see for example the \fI\-o\fP option of
59 .BR mount (8).)
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
64 .IR optionp :
65 .PP
66 .in +4n
67 .EX
68 .B ro,name=xyz
69 .EE
70 .in
71 .PP
72 The
73 .I tokens
74 argument is a pointer to a NULL-terminated array of pointers to the tokens that
75 .BR getsubopt ()
76 will look for in
77 .IR optionp .
78 The tokens should be distinct, null-terminated strings containing at
79 least one character, with no embedded equal signs or commas.
80 .PP
81 Each call to
82 .BR getsubopt ()
83 returns information about the next unprocessed suboption in
84 .IR optionp .
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
90 .IR tokens ,
91 and a value string was found,
92 .BR getsubopt ()
93 sets
94 .I *valuep
95 to the address of that string.
96 The first comma in
97 .I optionp
98 is overwritten with a null byte, so
99 .I *valuep
100 is precisely the "value string" for that suboption.
101 .PP
102 If the suboption is recognized, but no value string was found,
103 .I *valuep
104 is set to NULL.
105 .PP
106 When
107 .BR getsubopt ()
108 returns,
109 .I optionp
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.
113 .SH RETURN VALUE
114 If the first suboption in
115 .I optionp
116 is recognized,
117 .BR getsubopt ()
118 returns the index of the matching suboption element in
119 .IR tokens .
120 Otherwise, \-1 is returned and
121 .I *valuep
122 is the entire
123 .IB name [= value ]
124 string.
125 .PP
126 Since
127 .I *optionp
128 is changed, the first suboption before the call to
129 .BR getsubopt ()
130 is not (necessarily) the same as the first suboption after
131 .BR getsubopt ().
132 .SH ATTRIBUTES
133 For an explanation of the terms used in this section, see
134 .BR attributes (7).
135 .ad l
136 .nh
137 .TS
138 allbox;
139 lbx lb lb
140 l l l.
141 Interface Attribute Value
142 T{
143 .BR getsubopt ()
144 T} Thread safety MT-Safe
145 .TE
146 .hy
147 .ad
148 .sp 1
149 .SH STANDARDS
150 POSIX.1-2008.
151 .SH HISTORY
152 POSIX.1-2001.
153 .SH NOTES
154 Since
155 .BR getsubopt ()
156 overwrites any commas it finds in the string
157 .IR *optionp ,
158 that string must be writable; it cannot be a string constant.
159 .SH EXAMPLES
160 The following program expects suboptions following a "\-o" option.
161 .PP
162 .\" SRC BEGIN (getsubopt.c)
163 .EX
164 #define _XOPEN_SOURCE 500
165 #include <stdio.h>
166 #include <stdlib.h>
167 \&
168 #include <assert.h>
169 \&
170 int
171 main(int argc, char *argv[])
172 {
173 enum {
174 RO_OPT = 0,
175 RW_OPT,
176 NAME_OPT
177 };
178 char *const token[] = {
179 [RO_OPT] = "ro",
180 [RW_OPT] = "rw",
181 [NAME_OPT] = "name",
182 NULL
183 };
184 char *subopts;
185 char *value;
186 int opt;
187 \&
188 int readonly = 0;
189 int readwrite = 0;
190 char *name = NULL;
191 int errfnd = 0;
192 \&
193 while ((opt = getopt(argc, argv, "o:")) != \-1) {
194 switch (opt) {
195 case \[aq]o\[aq]:
196 subopts = optarg;
197 while (*subopts != \[aq]\e0\[aq] && !errfnd) {
198 \&
199 switch (getsubopt(&subopts, token, &value)) {
200 case RO_OPT:
201 readonly = 1;
202 break;
203 \&
204 case RW_OPT:
205 readwrite = 1;
206 break;
207 \&
208 case NAME_OPT:
209 if (value == NULL) {
210 fprintf(stderr,
211 "Missing value for suboption \[aq]%s\[aq]\en",
212 token[NAME_OPT]);
213 errfnd = 1;
214 continue;
215 }
216 \&
217 name = value;
218 break;
219 \&
220 default:
221 fprintf(stderr,
222 "No match found for token: /%s/\en", value);
223 errfnd = 1;
224 break;
225 }
226 }
227 if (readwrite && readonly) {
228 fprintf(stderr,
229 "Only one of \[aq]%s\[aq] and \[aq]%s\[aq] can be specified\en",
230 token[RO_OPT], token[RW_OPT]);
231 errfnd = 1;
232 }
233 break;
234 \&
235 default:
236 errfnd = 1;
237 }
238 }
239 \&
240 if (errfnd || argc == 1) {
241 fprintf(stderr, "\enUsage: %s \-o <suboptstring>\en", argv[0]);
242 fprintf(stderr,
243 "suboptions are \[aq]ro\[aq], \[aq]rw\[aq], and \[aq]name=<value>\[aq]\en");
244 exit(EXIT_FAILURE);
245 }
246 \&
247 /* Remainder of program... */
248 \&
249 exit(EXIT_SUCCESS);
250 }
251 .EE
252 .\" SRC END
253 .SH SEE ALSO
254 .BR getopt (3)