]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getsubopt.3
man*/: ffix (un-bracket tables)
[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 .TS
136 allbox;
137 lbx lb lb
138 l l l.
139 Interface Attribute Value
140 T{
141 .na
142 .nh
143 .BR getsubopt ()
144 T} Thread safety MT-Safe
145 .TE
146 .sp 1
147 .SH STANDARDS
148 POSIX.1-2008.
149 .SH HISTORY
150 POSIX.1-2001.
151 .SH NOTES
152 Since
153 .BR getsubopt ()
154 overwrites any commas it finds in the string
155 .IR *optionp ,
156 that string must be writable; it cannot be a string constant.
157 .SH EXAMPLES
158 The following program expects suboptions following a "\-o" option.
159 .PP
160 .\" SRC BEGIN (getsubopt.c)
161 .EX
162 #define _XOPEN_SOURCE 500
163 #include <stdio.h>
164 #include <stdlib.h>
165 \&
166 #include <assert.h>
167 \&
168 int
169 main(int argc, char *argv[])
170 {
171 enum {
172 RO_OPT = 0,
173 RW_OPT,
174 NAME_OPT
175 };
176 char *const token[] = {
177 [RO_OPT] = "ro",
178 [RW_OPT] = "rw",
179 [NAME_OPT] = "name",
180 NULL
181 };
182 char *subopts;
183 char *value;
184 int opt;
185 \&
186 int readonly = 0;
187 int readwrite = 0;
188 char *name = NULL;
189 int errfnd = 0;
190 \&
191 while ((opt = getopt(argc, argv, "o:")) != \-1) {
192 switch (opt) {
193 case \[aq]o\[aq]:
194 subopts = optarg;
195 while (*subopts != \[aq]\e0\[aq] && !errfnd) {
196 \&
197 switch (getsubopt(&subopts, token, &value)) {
198 case RO_OPT:
199 readonly = 1;
200 break;
201 \&
202 case RW_OPT:
203 readwrite = 1;
204 break;
205 \&
206 case NAME_OPT:
207 if (value == NULL) {
208 fprintf(stderr,
209 "Missing value for suboption \[aq]%s\[aq]\en",
210 token[NAME_OPT]);
211 errfnd = 1;
212 continue;
213 }
214 \&
215 name = value;
216 break;
217 \&
218 default:
219 fprintf(stderr,
220 "No match found for token: /%s/\en", value);
221 errfnd = 1;
222 break;
223 }
224 }
225 if (readwrite && readonly) {
226 fprintf(stderr,
227 "Only one of \[aq]%s\[aq] and \[aq]%s\[aq] can be specified\en",
228 token[RO_OPT], token[RW_OPT]);
229 errfnd = 1;
230 }
231 break;
232 \&
233 default:
234 errfnd = 1;
235 }
236 }
237 \&
238 if (errfnd || argc == 1) {
239 fprintf(stderr, "\enUsage: %s \-o <suboptstring>\en", argv[0]);
240 fprintf(stderr,
241 "suboptions are \[aq]ro\[aq], \[aq]rw\[aq], and \[aq]name=<value>\[aq]\en");
242 exit(EXIT_FAILURE);
243 }
244 \&
245 /* Remainder of program... */
246 \&
247 exit(EXIT_SUCCESS);
248 }
249 .EE
250 .\" SRC END
251 .SH SEE ALSO
252 .BR getopt (3)