]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getsubopt.3
dlinfo.3: tfix
[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 2017-09-15 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 getsubopt \- parse suboption arguments from a string
28 .SH SYNOPSIS
29 .B #include <stdlib.h>
30 .PP
31 .BI "int getsubopt(char **"optionp ", char * const *" tokens \
32 ", char **" valuep );
33 .PP
34 .in -4n
35 Feature Test Macro Requirements for glibc (see
36 .BR feature_test_macros (7)):
37 .in
38 .PP
39 .BR getsubopt ():
40 .ad l
41 .RS 4
42 .PD 0
43 _XOPEN_SOURCE\ >= 500
44 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
45 .br
46 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
47 .PD
48 .RE
49 .ad
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\\0\(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 .TS
135 allbox;
136 lb lb lb
137 l l l.
138 Interface Attribute Value
139 T{
140 .BR getsubopt ()
141 T} Thread safety MT-Safe
142 .TE
143 .SH CONFORMING TO
144 POSIX.1-2001, POSIX.1-2008.
145 .SH NOTES
146 .PP
147 Since
148 .BR getsubopt ()
149 overwrites any commas it finds in the string
150 .IR *optionp ,
151 that string must be writable; it cannot be a string constant.
152 .SH EXAMPLE
153 The following program expects suboptions following a "\-o" option.
154 .PP
155 .EX
156 #define _XOPEN_SOURCE 500
157 #include <stdlib.h>
158 #include <assert.h>
159 #include <stdio.h>
160
161 int
162 main(int argc, char **argv)
163 {
164 enum {
165 RO_OPT = 0,
166 RW_OPT,
167 NAME_OPT
168 };
169 char *const token[] = {
170 [RO_OPT] = "ro",
171 [RW_OPT] = "rw",
172 [NAME_OPT] = "name",
173 NULL
174 };
175 char *subopts;
176 char *value;
177 int opt;
178
179 int readonly = 0;
180 int readwrite = 0;
181 char *name = NULL;
182 int errfnd = 0;
183
184 while ((opt = getopt(argc, argv, "o:")) != \-1) {
185 switch (opt) {
186 case \(aqo\(aq:
187 subopts = optarg;
188 while (*subopts != \(aq\\0\(aq && !errfnd) {
189
190 switch (getsubopt(&subopts, token, &value)) {
191 case RO_OPT:
192 readonly = 1;
193 break;
194
195 case RW_OPT:
196 readwrite = 1;
197 break;
198
199 case NAME_OPT:
200 if (value == NULL) {
201 fprintf(stderr, "Missing value for "
202 "suboption \(aq%s\(aq\\n", token[NAME_OPT]);
203 errfnd = 1;
204 continue;
205 }
206
207 name = value;
208 break;
209
210 default:
211 fprintf(stderr, "No match found "
212 "for token: /%s/\\n", value);
213 errfnd = 1;
214 break;
215 }
216 }
217 if (readwrite && readonly) {
218 fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
219 "specified\\n", token[RO_OPT], token[RW_OPT]);
220 errfnd = 1;
221 }
222 break;
223
224 default:
225 errfnd = 1;
226 }
227 }
228
229 if (errfnd || argc == 1) {
230 fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
231 fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
232 "and \(aqname=<value>\(aq\\n");
233 exit(EXIT_FAILURE);
234 }
235
236 /* Remainder of program... */
237
238 exit(EXIT_SUCCESS);
239 }
240 .EE
241 .SH SEE ALSO
242 .BR getopt (3)