Debian Codegraph @

upstream/debian-trixie/man3/getsubopt.3

raw
1 '\" t
2 .\" Copyright (C) 2007 Michael Kerrisk <[email protected]>
3 .\" and Copyright (C) 2007 Justin Pryzby <[email protected]>
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 2024-06-15 "Linux man-pages 6.9.1"
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 .P
36 .BI "int getsubopt(char **restrict " optionp ", char *const *restrict " tokens ,
37 .BI " char **restrict " valuep );
38 .fi
39 .P
40 .RS -4
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
43 .RE
44 .P
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 .P
66 .in +4n
67 .EX
68 .B ro,name=xyz
69 .EE
70 .in
71 .P
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 .P
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 .P
102 If the suboption is recognized, but no value string was found,
103 .I *valuep
104 is set to NULL.
105 .P
106 When
107 .BR getsubopt ()
108 returns,
109 .I optionp
110 points to the next suboption,
111 or to the null byte (\[aq]\[rs]0\[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 .P
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 .SH STANDARDS
147 POSIX.1-2008.
148 .SH HISTORY
149 POSIX.1-2001.
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 .P
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 \[aq]o\[aq]:
193 subopts = optarg;
194 while (*subopts != \[aq]\[rs]0\[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]\[rs]n",
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/\[rs]n", 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\[rs]n",
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, "\[rs]nUsage: %s \-o <suboptstring>\[rs]n", argv[0]);
239 fprintf(stderr,
240 "suboptions are \[aq]ro\[aq], \[aq]rw\[aq], and \[aq]name=<value>\[aq]\[rs]n");
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)