1 /* Parsing of Suboptions Example
2    Copyright (C) 1991-2021 Free Software Foundation, Inc.
3 
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License
6    as published by the Free Software Foundation; either version 2
7    of the License, or (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 
22 int do_all;
23 const char *type;
24 int read_size;
25 int write_size;
26 int read_only;
27 
28 enum
29 {
30   RO_OPTION = 0,
31   RW_OPTION,
32   READ_SIZE_OPTION,
33   WRITE_SIZE_OPTION,
34   THE_END
35 };
36 
37 const char *mount_opts[] =
38 {
39   [RO_OPTION] = "ro",
40   [RW_OPTION] = "rw",
41   [READ_SIZE_OPTION] = "rsize",
42   [WRITE_SIZE_OPTION] = "wsize",
43   [THE_END] = NULL
44 };
45 
46 int
main(int argc,char ** argv)47 main (int argc, char **argv)
48 {
49   char *subopts, *value;
50   int opt;
51 
52   while ((opt = getopt (argc, argv, "at:o:")) != -1)
53     switch (opt)
54       {
55       case 'a':
56         do_all = 1;
57         break;
58       case 't':
59         type = optarg;
60         break;
61       case 'o':
62         subopts = optarg;
63         while (*subopts != '\0')
64           switch (getsubopt (&subopts, mount_opts, &value))
65             {
66             case RO_OPTION:
67               read_only = 1;
68               break;
69             case RW_OPTION:
70               read_only = 0;
71               break;
72             case READ_SIZE_OPTION:
73               if (value == NULL)
74                 abort ();
75               read_size = atoi (value);
76               break;
77             case WRITE_SIZE_OPTION:
78               if (value == NULL)
79                 abort ();
80               write_size = atoi (value);
81               break;
82             default:
83               /* Unknown suboption.  */
84               printf ("Unknown suboption `%s'\n", value);
85               break;
86             }
87         break;
88       default:
89         abort ();
90       }
91 
92   /* Do the real work.  */
93 
94   return 0;
95 }
96