OSDN Git Service

(split) LDP: Update original to LDP v3.63
[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
148 main(int argc, char **argv)
149 {
150     enum {
151         RO_OPT = 0,
152         RW_OPT,
153         NAME_OPT
154     };
155     char *const token[] = {
156         [RO_OPT]   = "ro",
157         [RW_OPT]   = "rw",
158         [NAME_OPT] = "name",
159         NULL
160     };
161     char *subopts;
162     char *value;
163     int opt;
164
165     int readonly = 0;
166     int readwrite = 0;
167     char *name = NULL;
168     int errfnd = 0;
169
170     while ((opt = getopt(argc, argv, "o:")) != \-1) {
171         switch (opt) {
172         case \(aqo\(aq:
173             subopts = optarg;
174             while (*subopts != \(aq\\0\(aq && !errfnd) {
175
176             switch (getsubopt(&subopts, token, &value)) {
177             case RO_OPT:
178                 readonly = 1;
179                 break;
180
181             case RW_OPT:
182                 readwrite = 1;
183                 break;
184
185             case NAME_OPT:
186                 if (value == NULL) {
187                     fprintf(stderr, "Missing value for "
188                             "suboption \(aq%s\(aq\\n", token[NAME_OPT]);
189                     errfnd = 1;
190                     continue;
191                 }
192
193                 name = value;
194                 break;
195
196             default:
197                 fprintf(stderr, "No match found "
198                         "for token: /%s/\\n", value);
199                 errfnd = 1;
200                 break;
201             }
202         }
203         if (readwrite && readonly) {
204             fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
205                     "specified\\n", token[RO_OPT], token[RW_OPT]);
206             errfnd = 1;
207         }
208         break;
209
210         default:
211             errfnd = 1;
212         }
213     }
214
215     if (errfnd || argc == 1) {
216         fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
217         fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
218                 "and \(aqname=<value>\(aq\\n");
219         exit(EXIT_FAILURE);
220     }
221
222     /* Remainder of program... */
223
224     exit(EXIT_SUCCESS);
225 }
226 .fi
227 .SH SEE ALSO
228 .BR getopt (3)