1GETSUBOPT(3)               Linux Programmer's Manual              GETSUBOPT(3)
2
3
4

NAME

6       getsubopt - parse suboption arguments from a string
7

SYNOPSIS

9       #include <stdlib.h>
10
11       int getsubopt(char **restrict optionp, char *const *restrict tokens,
12                     char **restrict valuep);
13
14   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16       getsubopt():
17           _XOPEN_SOURCE >= 500
18               || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
19

DESCRIPTION

21       getsubopt()  parses  the list of comma-separated suboptions provided in
22       optionp.  (Such a suboption list is typically produced  when  getopt(3)
23       is  used  to  parse  a  command  line; see for example the -o option of
24       mount(8).)  Each suboption may include an associated  value,  which  is
25       separated  from  the suboption name by an equal sign.  The following is
26       an example of the kind of string that might be passed in optionp:
27
28           ro,name=xyz
29
30       The tokens argument is a pointer to a NULL-terminated array of pointers
31       to  the  tokens  that getsubopt() will look for in optionp.  The tokens
32       should be distinct, null-terminated strings  containing  at  least  one
33       character, with no embedded equal signs or commas.
34
35       Each call to getsubopt() returns information about the next unprocessed
36       suboption in optionp.  The first equal sign in a suboption (if any)  is
37       interpreted  as a separator between the name and the value of that sub‐
38       option.  The value extends to the next comma, or (for the  last  subop‐
39       tion) to the end of the string.  If the name of the suboption matches a
40       known name from tokens, and a value string was found, getsubopt()  sets
41       *valuep  to  the address of that string.  The first comma in optionp is
42       overwritten with a null  byte,  so  *valuep  is  precisely  the  "value
43       string" for that suboption.
44
45       If  the suboption is recognized, but no value string was found, *valuep
46       is set to NULL.
47
48       When getsubopt() returns, optionp points to the next suboption,  or  to
49       the null byte ('\0') at the end of the string if the last suboption was
50       just processed.
51

RETURN VALUE

53       If the first suboption in optionp is  recognized,  getsubopt()  returns
54       the  index  of the matching suboption element in tokens.  Otherwise, -1
55       is returned and *valuep is the entire name[=value] string.
56
57       Since *optionp is changed, the first suboption before the call to  get‐
58       subopt()  is  not  (necessarily)  the same as the first suboption after
59       getsubopt().
60

ATTRIBUTES

62       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
63       tributes(7).
64
65       ┌────────────────────────────────────────────┬───────────────┬─────────┐
66Interface                                   Attribute     Value   
67       ├────────────────────────────────────────────┼───────────────┼─────────┤
68getsubopt()                                 │ Thread safety │ MT-Safe │
69       └────────────────────────────────────────────┴───────────────┴─────────┘
70

CONFORMING TO

72       POSIX.1-2001, POSIX.1-2008.
73

NOTES

75       Since  getsubopt()  overwrites  any  commas it finds in the string *op‐
76       tionp, that string must be writable; it cannot be a string constant.
77

EXAMPLES

79       The following program expects suboptions following a "-o" option.
80
81       #define _XOPEN_SOURCE 500
82       #include <stdlib.h>
83       #include <assert.h>
84       #include <stdio.h>
85
86       int
87       main(int argc, char **argv)
88       {
89           enum {
90               RO_OPT = 0,
91               RW_OPT,
92               NAME_OPT
93           };
94           char *const token[] = {
95               [RO_OPT]   = "ro",
96               [RW_OPT]   = "rw",
97               [NAME_OPT] = "name",
98               NULL
99           };
100           char *subopts;
101           char *value;
102           int opt;
103
104           int readonly = 0;
105           int readwrite = 0;
106           char *name = NULL;
107           int errfnd = 0;
108
109           while ((opt = getopt(argc, argv, "o:")) != -1) {
110               switch (opt) {
111               case 'o':
112                   subopts = optarg;
113                   while (*subopts != '\0' && !errfnd) {
114
115                   switch (getsubopt(&subopts, token, &value)) {
116                   case RO_OPT:
117                       readonly = 1;
118                       break;
119
120                   case RW_OPT:
121                       readwrite = 1;
122                       break;
123
124                   case NAME_OPT:
125                       if (value == NULL) {
126                           fprintf(stderr, "Missing value for "
127                                   "suboption '%s'\n", token[NAME_OPT]);
128                           errfnd = 1;
129                           continue;
130                       }
131
132                       name = value;
133                       break;
134
135                   default:
136                       fprintf(stderr, "No match found "
137                               "for token: /%s/\n", value);
138                       errfnd = 1;
139                       break;
140                   }
141               }
142               if (readwrite && readonly) {
143                   fprintf(stderr, "Only one of '%s' and '%s' can be "
144                           "specified\n", token[RO_OPT], token[RW_OPT]);
145                   errfnd = 1;
146               }
147               break;
148
149               default:
150                   errfnd = 1;
151               }
152           }
153
154           if (errfnd || argc == 1) {
155               fprintf(stderr, "\nUsage: %s -o <suboptstring>\n", argv[0]);
156               fprintf(stderr, "suboptions are 'ro', 'rw', "
157                       "and 'name=<value>'\n");
158               exit(EXIT_FAILURE);
159           }
160
161           /* Remainder of program... */
162
163           exit(EXIT_SUCCESS);
164       }
165

SEE ALSO

167       getopt(3)
168

COLOPHON

170       This page is part of release 5.12 of the Linux  man-pages  project.   A
171       description  of  the project, information about reporting bugs, and the
172       latest    version    of    this    page,    can     be     found     at
173       https://www.kernel.org/doc/man-pages/.
174
175
176
177GNU                               2021-03-22                      GETSUBOPT(3)
Impressum