OSDN Git Service

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