1GETSUBOPT(3P)              POSIX Programmer's Manual             GETSUBOPT(3P)
2
3
4

PROLOG

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

NAME

12       getsubopt — parse suboption arguments from a string
13

SYNOPSIS

15       #include <stdlib.h>
16
17       int getsubopt(char **optionp, char * const *keylistp, char **valuep);
18

DESCRIPTION

20       The getsubopt() function shall parse  suboption  arguments  in  a  flag
21       argument. Such options often result from the use of getopt().
22
23       The  getsubopt()  argument  optionp  is  a  pointer to a pointer to the
24       option argument string. The suboption arguments shall be  separated  by
25       <comma>  characters and each may consist of either a single token, or a
26       token-value pair separated by an <equals-sign>.
27
28       The keylistp argument shall be a pointer to a vector  of  strings.  The
29       end  of  the  vector is identified by a null pointer. Each entry in the
30       vector is one of the possible tokens that might be found  in  *optionp.
31       Since  <comma>  characters delimit suboption arguments in optionp, they
32       should not appear in any of the strings pointed to by keylistp.   Simi‐
33       larly,  because  an <equals-sign> separates a token from its value, the
34       application should not include an <equals-sign> in any of  the  strings
35       pointed  to by keylistp.  The getsubopt() function shall not modify the
36       keylistp vector.
37
38       The valuep argument is the address of a value string pointer.
39
40       If a <comma> appears in optionp, it shall be interpreted as a suboption
41       separator.  After  <comma> characters have been processed, if there are
42       one or more <equals-sign> characters in a suboption string,  the  first
43       <equals-sign> in any suboption string shall be interpreted as a separa‐
44       tor between a token and a value. Subsequent <equals-sign> characters in
45       a suboption string shall be interpreted as part of the value.
46
47       If the string at *optionp contains only one suboption argument (equiva‐
48       lently, no <comma> characters), getsubopt() shall  update  *optionp  to
49       point  to  the  null  character at the end of the string. Otherwise, it
50       shall isolate the suboption argument by replacing the <comma> separator
51       with  a null character, and shall update *optionp to point to the start
52       of the next suboption argument. If the suboption argument has an  asso‐
53       ciated  value  (equivalently,  contains  an <equals-sign>), getsubopt()
54       shall update *valuep to point to the value's first  character.   Other‐
55       wise,  it  shall set *valuep to a null pointer. The calling application
56       may use this information to determine whether the presence  or  absence
57       of a value for the suboption is an error.
58
59       Additionally,  when  getsubopt()  fails to match the suboption argument
60       with a token in the keylistp  array,  the  calling  application  should
61       decide  if  this  is  an error, or if the unrecognized option should be
62       processed in another way.
63

RETURN VALUE

65       The getsubopt() function shall return the index of  the  matched  token
66       string, or -1 if no token strings were matched.
67

ERRORS

69       No errors are defined.
70
71       The following sections are informative.
72

EXAMPLES

74   Parsing Suboptions
75       The  following  example  uses the getsubopt() function to parse a value
76       argument in  the  optarg  external  variable  returned  by  a  call  to
77       getopt().
78
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
165           program -o ro,rsize=512
166
167       then  after option parsing, the variable do_all will be 0, type will be
168       a null pointer, read_size will  be  512,  write_size  will  be  0,  and
169       read_only will be 1. If it is invoked with:
170
171
172           program -o oops
173
174       it will print:
175
176
177           "Unknown suboption `oops'"
178
179       before aborting.
180

APPLICATION USAGE

182       The  value  of *valuep when getsubopt() returns -1 is unspecified. His‐
183       torical implementations  provide  various  incompatible  extensions  to
184       allow an application to access the suboption text that was not found in
185       the keylistp array.
186

RATIONALE

188       The keylistp argument of getsubopt() is typed as  char  *  const  *  to
189       match  historical  practice. However, the standard is clear that imple‐
190       mentations will not modify either the array or the strings contained in
191       the array, as if the argument had been typed const char * const *.
192

FUTURE DIRECTIONS

194       None.
195

SEE ALSO

197       getopt()
198
199       The Base Definitions volume of POSIX.1‐2017, <stdlib.h>
200
202       Portions  of  this text are reprinted and reproduced in electronic form
203       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
204       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
205       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
206       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
207       event of any discrepancy between this version and the original IEEE and
208       The  Open Group Standard, the original IEEE and The Open Group Standard
209       is the referee document. The original Standard can be  obtained  online
210       at http://www.opengroup.org/unix/online.html .
211
212       Any  typographical  or  formatting  errors that appear in this page are
213       most likely to have been introduced during the conversion of the source
214       files  to  man page format. To report such errors, see https://www.ker
215       nel.org/doc/man-pages/reporting_bugs.html .
216
217
218
219IEEE/The Open Group                  2017                        GETSUBOPT(3P)
Impressum