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