1GETSUBOPT(3P) POSIX Programmer's Manual GETSUBOPT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 getsubopt — parse suboption arguments from a string
14
16 #include <stdlib.h>
17
18 int getsubopt(char **optionp, char * const *keylistp, char **valuep);
19
21 The getsubopt() function shall parse suboption arguments in a flag
22 argument. Such options often result from the use of getopt().
23
24 The getsubopt() argument optionp is a pointer to a pointer to the
25 option argument string. The suboption arguments shall be separated by
26 <comma> characters and each may consist of either a single token, or a
27 token-value pair separated by an <equals-sign>.
28
29 The keylistp argument shall be a pointer to a vector of strings. The
30 end of the vector is identified by a null pointer. Each entry in the
31 vector is one of the possible tokens that might be found in *optionp.
32 Since <comma> characters delimit suboption arguments in optionp, they
33 should not appear in any of the strings pointed to by keylistp. Simi‐
34 larly, because an <equals-sign> separates a token from its value, the
35 application should not include an <equals-sign> in any of the strings
36 pointed to by keylistp. The getsubopt() function shall not modify the
37 keylistp vector.
38
39 The valuep argument is the address of a value string pointer.
40
41 If a <comma> appears in optionp, it shall be interpreted as a suboption
42 separator. After <comma> characters have been processed, if there are
43 one or more <equals-sign> characters in a suboption string, the first
44 <equals-sign> in any suboption string shall be interpreted as a separa‐
45 tor between a token and a value. Subsequent <equals-sign> characters in
46 a suboption string shall be interpreted as part of the value.
47
48 If the string at *optionp contains only one suboption argument (equiva‐
49 lently, no <comma> characters), getsubopt() shall update *optionp to
50 point to the null character at the end of the string. Otherwise, it
51 shall isolate the suboption argument by replacing the <comma> separator
52 with a null character, and shall update *optionp to point to the start
53 of the next suboption argument. If the suboption argument has an asso‐
54 ciated value (equivalently, contains an <equals-sign>), getsubopt()
55 shall update *valuep to point to the value's first character. Other‐
56 wise, it shall set *valuep to a null pointer. The calling application
57 may use this information to determine whether the presence or absence
58 of a value for the suboption is an error.
59
60 Additionally, when getsubopt() fails to match the suboption argument
61 with a token in the keylistp array, the calling application should
62 decide if this is an error, or if the unrecognized option should be
63 processed in another way.
64
66 The getsubopt() function shall return the index of the matched token
67 string, or −1 if no token strings were matched.
68
70 No errors are defined.
71
72 The following sections are informative.
73
75 Parsing Suboptions
76 The following example uses the getsubopt() function to parse a value
77 argument in the optarg external variable returned by a call to
78 getopt().
79
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <unistd.h>
83
84 int do_all;
85 const char *type;
86 int read_size;
87 int write_size;
88 int read_only;
89
90 enum
91 {
92 RO_OPTION = 0,
93 RW_OPTION,
94 READ_SIZE_OPTION,
95 WRITE_SIZE_OPTION
96 };
97
98 const char *mount_opts[] =
99 {
100 [RO_OPTION] = "ro",
101 [RW_OPTION] = "rw",
102 [READ_SIZE_OPTION] = "rsize",
103 [WRITE_SIZE_OPTION] = "wsize",
104 NULL
105 };
106
107 int
108 main(int argc, char *argv[])
109 {
110 char *subopts, *value;
111 int opt;
112
113 while ((opt = getopt(argc, argv, "at:o:")) != -1)
114 switch(opt)
115 {
116 case 'a':
117 do_all = 1;
118 break;
119 case 't':
120 type = optarg;
121 break;
122 case 'o':
123 subopts = optarg;
124 while (*subopts != ' ')
125 {
126 char *saved = subopts;
127 switch(getsubopt(&subopts, (char **)mount_opts,
128 &value))
129 {
130 case RO_OPTION:
131 read_only = 1;
132 break;
133 case RW_OPTION:
134 read_only = 0;
135 break;
136 case READ_SIZE_OPTION:
137 if (value == NULL)
138 abort();
139 read_size = atoi(value);
140 break;
141 case WRITE_SIZE_OPTION:
142 if (value == NULL)
143 abort();
144 write_size = atoi(value);
145 break;
146 default:
147 /* Unknown suboption. */
148 printf("Unknown suboption `%s'\n", saved);
149 abort();
150 }
151 }
152 break;
153 default:
154 abort();
155 }
156
157 /* Do the real work. */
158
159 return 0;
160 }
161
162 If the above example is invoked with:
163
164 program -o ro,rsize=512
165
166 then after option parsing, the variable do_all will be 0, type will be
167 a null pointer, read_size will be 512, write_size will be 0, and
168 read_only will be 1. If it is invoked with:
169
170 program -o oops
171
172 it will print:
173
174 "Unknown suboption `oops'"
175
176 before aborting.
177
179 The value of *valuep when getsubopt() returns −1 is unspecified. His‐
180 torical implementations provide various incompatible extensions to
181 allow an application to access the suboption text that was not found in
182 the keylistp array.
183
185 The keylistp argument of getsubopt() is typed as char * const * to
186 match historical practice. However, the standard is clear that imple‐
187 mentations will not modify either the array or the strings contained in
188 the array, as if the argument had been typed const char * const *.
189
191 None.
192
194 getopt()
195
196 The Base Definitions volume of POSIX.1‐2008, <stdlib.h>
197
199 Portions of this text are reprinted and reproduced in electronic form
200 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
201 -- Portable Operating System Interface (POSIX), The Open Group Base
202 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
203 cal and Electronics Engineers, Inc and The Open Group. (This is
204 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
205 event of any discrepancy between this version and the original IEEE and
206 The Open Group Standard, the original IEEE and The Open Group Standard
207 is the referee document. The original Standard can be obtained online
208 at http://www.unix.org/online.html .
209
210 Any typographical or formatting errors that appear in this page are
211 most likely to have been introduced during the conversion of the source
212 files to man page format. To report such errors, see https://www.ker‐
213 nel.org/doc/man-pages/reporting_bugs.html .
214
215
216
217IEEE/The Open Group 2013 GETSUBOPT(3P)