Update copyright notices with scripts/update-copyrights
[glibc.git] / manual / examples / subopt.c
blobbe6bf98f7bb8d576ecd7998f030b6e0a972bdbc6
1 /* Parsing of Suboptions Example
2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
22 int do_all;
23 const char *type;
24 int read_size;
25 int write_size;
26 int read_only;
28 enum
30 RO_OPTION = 0,
31 RW_OPTION,
32 READ_SIZE_OPTION,
33 WRITE_SIZE_OPTION,
34 THE_END
37 const char *mount_opts[] =
39 [RO_OPTION] = "ro",
40 [RW_OPTION] = "rw",
41 [READ_SIZE_OPTION] = "rsize",
42 [WRITE_SIZE_OPTION] = "wsize",
43 [THE_END] = NULL
46 int
47 main (int argc, char **argv)
49 char *subopts, *value;
50 int opt;
52 while ((opt = getopt (argc, argv, "at:o:")) != -1)
53 switch (opt)
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))
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;
87 break;
88 default:
89 abort ();
92 /* Do the real work. */
94 return 0;