]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getsubopt.3
7d87cb0cb02aa3e50d616e2d80e8137a466cd4cb
[thirdparty/man-pages.git] / man3 / getsubopt.3
1 .\" Copyright (C) 2007 Michael Kerrisk <mtk-manpages@gmx.net>
2 .\" and (C) 2007 Justin Pryzby <pryzbyj@justinpryzby.com>
3 .\"
4 .\" Permission is hereby granted, free of charge, to any person obtaining
5 .\" a copy of this software and associated documentation files (the
6 .\" "Software"), to deal in the Software without restriction, including
7 .\" without limitation the rights to use, copy, modify, merge, publish,
8 .\" distribute, sublicense, and/or sell copies of the Software, and to
9 .\" permit persons to whom the Software is furnished to do so, subject to
10 .\" the following conditions:
11 .\"
12 .\" The above copyright notice and this permission notice shall be
13 .\" included in all copies or substantial portions of the Software.
14 .\"
15 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 .\"
23 .TH GETSUBOPT 3 2007-05-05 "GNU" "Linux Programmer's Manual"
24 .SH NAME
25 getsubopt \- parse suboption arguments from a string
26 .SH SYNOPSIS
27 .B #define _XOPEN_SOURCE 500
28 .br
29 .B #include <stdlib.h>
30
31 .B int getsubopt(char
32 .BI ** optionp ,
33 .B char *const
34 .BI * tokens ,
35 .B char
36 .IB valuep );
37 .SH DESCRIPTION
38 .BR getsubopt ()
39 parses the list of comma-separated suboptions provided in
40 .IR optionp .
41 (Such a suboption list is typically produced when
42 .BR getopt (3)
43 is used to parse a command line;
44 see for example the \fI-o\fP option of
45 .BR mount (8).)
46 Each suboption may include an associated value,
47 which is separated from the suboption name by an equal sign.
48 The following is an example of the kind of string
49 that might be passed in
50 .IR optionp :
51 .sp
52 .RS
53 .B ro,name=xyz
54 .RE
55
56 The
57 .I tokens
58 argument is a pointer to a NULL-terminated list of the tokens that
59 .BR getsubopt ()
60 will look for in
61 .IR optionp .
62 The tokens should be distinct, null-terminated strings containing at
63 least one character, with no embedded equal signs or commas.
64
65 Each call to
66 .BR getsubopt ()
67 returns information about the next unprocessed suboption in
68 .IR optionp .
69 The first equal sign in a suboption (if any) is interpreted as a
70 separator between the name and the value of that suboption.
71 The value extends to the next comma,
72 or (for the last suboption) to the end of the string.
73 If the name of the suboption matches a known name from
74 .IR tokens ,
75 and a value string was found,
76 .BR getsubopt ()
77 sets
78 .RI * valuep
79 to the address of that string.
80 The first comma in
81 .I optionp
82 is overwritten with a null byte, so
83 .RI * valuep
84 is precisely the "value string" for that suboption.
85
86 If the suboption is recognized, but no value string was found,
87 .RI * valuep
88 is set to NULL.
89
90 When
91 .BR getsubopt ()
92 returns,
93 .I optionp
94 points to the next suboption, or to the null character at the end of the
95 string if the last suboption was just processed.
96 .SH RETURN VALUE
97 If the first suboption in
98 .I optionp
99 is recognized,
100 .BR getsubopt ()
101 returns the index of the matching suboption element in
102 .I tokens .
103 Otherwise, \-1 is returned and
104 .RI * valuep
105 is the entire
106 .IB name [= value ]
107 string.
108
109 Since
110 .RI * optionp
111 is changed, the first suboption before the call to
112 .BR getsubopt ()
113 is not (necessarily) the same as the first suboption after
114 .BR getsubopt ().
115 .SH CONFORMING TO
116 POSIX.1-2001.
117 .SH NOTES
118
119 Since
120 .BR getsubopt ()
121 overwrites any commas it finds in the string
122 .RI * optionp ,
123 that string must be writable; it cannot be a string constant.
124 .SH EXAMPLE
125 The following program excepts suboptions following a "\-o" option.
126
127 .nf
128 #define _XOPEN_SOURCE 500
129 #include <stdlib.h>
130 #include <assert.h>
131 #include <stdio.h>
132
133 int main(int argc, char **argv)
134 {
135 enum {
136 RO_OPT = 0,
137 RW_OPT,
138 NAME_OPT
139 };
140 char *const token[] = {
141 [RO_OPT] = "ro",
142 [RW_OPT] = "rw",
143 [NAME_OPT] = "name",
144 NULL
145 };
146 char *subopts;
147 char *value;
148 int opt;
149
150 int readonly = 0;
151 int readwrite = 0;
152 char *name = NULL;
153 int errfnd = 0;
154
155 while ((opt = getopt(argc, argv, "o:")) != \-1) {
156 switch (opt) {
157 case 'o':
158 subopts = optarg;
159 while (*subopts != '\\0' && !errfnd) {
160
161 switch (getsubopt(&subopts, token, &value)) {
162 case RO_OPT:
163 readonly = 1;
164 break;
165
166 case RW_OPT:
167 readwrite = 1;
168 break;
169
170 case NAME_OPT:
171 if (value == NULL) {
172 fprintf(stderr, "Missing value for "
173 "suboption '%s'\\n", token[NAME_OPT]);
174 errfnd = 1;
175 continue;
176 }
177
178 name = value;
179 break;
180
181 default:
182 fprintf(stderr, "No match found "
183 "for token: /%s/\\n", value);
184 errfnd = 1;
185 break;
186 }
187 }
188 if (readwrite && readonly) {
189 fprintf(stderr, "Only one of '%s' and '%s' can be "
190 "specified\\n", token[RO_OPT], token[RW_OPT]);
191 errfnd = 1;
192 }
193 break;
194
195 default:
196 errfnd = 1;
197 }
198 }
199
200 if (errfnd || argc == 1) {
201 fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
202 fprintf(stderr, "suboptions are 'ro', 'rw', "
203 "and 'name=<value>'\\n");
204 exit(EXIT_FAILURE);
205 }
206
207 /* Remainder of program... */
208
209 exit(EXIT_SUCCESS);
210 }
211 .fi
212 .SH SEE ALSO
213 .BR getopt (3),
214 .BR feature_test_macros (7)