OSDN Git Service

(split) LDP: Update original to LDP v3.50.
[linuxjm/LDP_man-pages.git] / original / 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 2010-09-26 "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
31 .BI "int getsubopt(char **"optionp ", char * const *" tokens \
32 ", char **" valuep );
33 .sp
34 .in -4n
35 Feature Test Macro Requirements for glibc (see
36 .BR feature_test_macros (7)):
37 .in
38 .sp
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 .sp
65 .in +4n
66 .B ro,name=xyz
67 .in
68
69 The
70 .I tokens
71 argument is a pointer to a NULL-terminated array of pointers to the tokens that
72 .BR getsubopt ()
73 will look for in
74 .IR optionp .
75 The tokens should be distinct, null-terminated strings containing at
76 least one character, with no embedded equal signs or commas.
77
78 Each call to
79 .BR getsubopt ()
80 returns information about the next unprocessed suboption in
81 .IR optionp .
82 The first equal sign in a suboption (if any) is interpreted as a
83 separator between the name and the value of that suboption.
84 The value extends to the next comma,
85 or (for the last suboption) to the end of the string.
86 If the name of the suboption matches a known name from
87 .IR tokens ,
88 and a value string was found,
89 .BR getsubopt ()
90 sets
91 .I *valuep
92 to the address of that string.
93 The first comma in
94 .I optionp
95 is overwritten with a null byte, so
96 .I *valuep
97 is precisely the "value string" for that suboption.
98
99 If the suboption is recognized, but no value string was found,
100 .I *valuep
101 is set to NULL.
102
103 When
104 .BR getsubopt ()
105 returns,
106 .I optionp
107 points to the next suboption,
108 or to the null byte (\(aq\\0\(aq) at the end of the
109 string if the last suboption was just processed.
110 .SH RETURN VALUE
111 If the first suboption in
112 .I optionp
113 is recognized,
114 .BR getsubopt ()
115 returns the index of the matching suboption element in
116 .IR tokens .
117 Otherwise, \-1 is returned and
118 .I *valuep
119 is the entire
120 .IB name [= value ]
121 string.
122
123 Since
124 .I *optionp
125 is changed, the first suboption before the call to
126 .BR getsubopt ()
127 is not (necessarily) the same as the first suboption after
128 .BR getsubopt ().
129 .SH CONFORMING TO
130 POSIX.1-2001.
131 .SH NOTES
132
133 Since
134 .BR getsubopt ()
135 overwrites any commas it finds in the string
136 .IR *optionp ,
137 that string must be writable; it cannot be a string constant.
138 .SH EXAMPLE
139 The following program expects suboptions following a "\-o" option.
140
141 .nf
142 #define _XOPEN_SOURCE 500
143 #include <stdlib.h>
144 #include <assert.h>
145 #include <stdio.h>
146
147 int main(int argc, char **argv)
148 {
149     enum {
150         RO_OPT = 0,
151         RW_OPT,
152         NAME_OPT
153     };
154     char *const token[] = {
155         [RO_OPT]   = "ro",
156         [RW_OPT]   = "rw",
157         [NAME_OPT] = "name",
158         NULL
159     };
160     char *subopts;
161     char *value;
162     int opt;
163
164     int readonly = 0;
165     int readwrite = 0;
166     char *name = NULL;
167     int errfnd = 0;
168
169     while ((opt = getopt(argc, argv, "o:")) != \-1) {
170         switch (opt) {
171         case \(aqo\(aq:
172             subopts = optarg;
173             while (*subopts != \(aq\\0\(aq && !errfnd) {
174
175             switch (getsubopt(&subopts, token, &value)) {
176             case RO_OPT:
177                 readonly = 1;
178                 break;
179
180             case RW_OPT:
181                 readwrite = 1;
182                 break;
183
184             case NAME_OPT:
185                 if (value == NULL) {
186                     fprintf(stderr, "Missing value for "
187                             "suboption \(aq%s\(aq\\n", token[NAME_OPT]);
188                     errfnd = 1;
189                     continue;
190                 }
191
192                 name = value;
193                 break;
194
195             default:
196                 fprintf(stderr, "No match found "
197                         "for token: /%s/\\n", value);
198                 errfnd = 1;
199                 break;
200             }
201         }
202         if (readwrite && readonly) {
203             fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
204                     "specified\\n", token[RO_OPT], token[RW_OPT]);
205             errfnd = 1;
206         }
207         break;
208
209         default:
210             errfnd = 1;
211         }
212     }
213
214     if (errfnd || argc == 1) {
215         fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
216         fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
217                 "and \(aqname=<value>\(aq\\n");
218         exit(EXIT_FAILURE);
219     }
220
221     /* Remainder of program... */
222
223     exit(EXIT_SUCCESS);
224 }
225 .fi
226 .SH SEE ALSO
227 .BR getopt (3)