OSDN Git Service

LDP: Update original to LDP v3.79
[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 2014-04-08 "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 ATTRIBUTES
130 .SS Multithreading (see pthreads(7))
131 The
132 .BR getsubopt ()
133 function is thread-safe.
134 .SH CONFORMING TO
135 POSIX.1-2001.
136 .SH NOTES
137
138 Since
139 .BR getsubopt ()
140 overwrites any commas it finds in the string
141 .IR *optionp ,
142 that string must be writable; it cannot be a string constant.
143 .SH EXAMPLE
144 The following program expects suboptions following a "\-o" option.
145
146 .nf
147 #define _XOPEN_SOURCE 500
148 #include <stdlib.h>
149 #include <assert.h>
150 #include <stdio.h>
151
152 int
153 main(int argc, char **argv)
154 {
155     enum {
156         RO_OPT = 0,
157         RW_OPT,
158         NAME_OPT
159     };
160     char *const token[] = {
161         [RO_OPT]   = "ro",
162         [RW_OPT]   = "rw",
163         [NAME_OPT] = "name",
164         NULL
165     };
166     char *subopts;
167     char *value;
168     int opt;
169
170     int readonly = 0;
171     int readwrite = 0;
172     char *name = NULL;
173     int errfnd = 0;
174
175     while ((opt = getopt(argc, argv, "o:")) != \-1) {
176         switch (opt) {
177         case \(aqo\(aq:
178             subopts = optarg;
179             while (*subopts != \(aq\\0\(aq && !errfnd) {
180
181             switch (getsubopt(&subopts, token, &value)) {
182             case RO_OPT:
183                 readonly = 1;
184                 break;
185
186             case RW_OPT:
187                 readwrite = 1;
188                 break;
189
190             case NAME_OPT:
191                 if (value == NULL) {
192                     fprintf(stderr, "Missing value for "
193                             "suboption \(aq%s\(aq\\n", token[NAME_OPT]);
194                     errfnd = 1;
195                     continue;
196                 }
197
198                 name = value;
199                 break;
200
201             default:
202                 fprintf(stderr, "No match found "
203                         "for token: /%s/\\n", value);
204                 errfnd = 1;
205                 break;
206             }
207         }
208         if (readwrite && readonly) {
209             fprintf(stderr, "Only one of \(aq%s\(aq and \(aq%s\(aq can be "
210                     "specified\\n", token[RO_OPT], token[RW_OPT]);
211             errfnd = 1;
212         }
213         break;
214
215         default:
216             errfnd = 1;
217         }
218     }
219
220     if (errfnd || argc == 1) {
221         fprintf(stderr, "\\nUsage: %s \-o <suboptstring>\\n", argv[0]);
222         fprintf(stderr, "suboptions are \(aqro\(aq, \(aqrw\(aq, "
223                 "and \(aqname=<value>\(aq\\n");
224         exit(EXIT_FAILURE);
225     }
226
227     /* Remainder of program... */
228
229     exit(EXIT_SUCCESS);
230 }
231 .fi
232 .SH SEE ALSO
233 .BR getopt (3)
234 .SH COLOPHON
235 This page is part of release 3.79 of the Linux
236 .I man-pages
237 project.
238 A description of the project,
239 information about reporting bugs,
240 and the latest version of this page,
241 can be found at
242 \%http://www.kernel.org/doc/man\-pages/.