]> git.ipfire.org Git - thirdparty/glibc.git/blob - manual/examples/subopt.c
4a89f6441ef12d34715449ff36253b268079104a
[thirdparty/glibc.git] / manual / examples / subopt.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int do_all;
5 const char *type;
6 int read_size;
7 int write_size;
8 int read_only;
9
10 enum
11 {
12 RO_OPTION = 0,
13 RW_OPTION,
14 READ_SIZE_OPTION,
15 WRITE_SIZE_OPTION
16 };
17
18 const char *mount_opts[] =
19 {
20 [RO_OPTION] = "ro",
21 [RW_OPTION] = "rw",
22 [READ_SIZE_OPTION] = "rsize",
23 [WRITE_SIZE_OPTION] = "wsize"
24 };
25
26 int
27 main (int argc, char *argv[])
28 {
29 char *subopts, *value;
30 int opt;
31
32 while ((opt = getopt (argc, argv, "at:o:")) != EOF)
33 switch (opt)
34 {
35 case 'a':
36 do_all = 1;
37 break;
38 case 't':
39 type = optarg;
40 break;
41 case 'o':
42 subopts = optarg;
43 while (*subopts != '\0')
44 switch (getsubopt (&subopts, mount_opts, &value))
45 {
46 case RO_OPTION:
47 read_only = 1;
48 break;
49 case RW_OPTION:
50 read_only = 0;
51 break;
52 case READ_SIZE_OPTION:
53 if (value == NULL)
54 abort ();
55 read_size = atoi (value);
56 break;
57 case WRITE_SIZE_OPTION:
58 if (value == NULL)
59 abort ();
60 write_size = atoi (value);
61 break;
62 default:
63 /* Unknown suboption. */
64 printf ("Unknown suboption `%s'\n", value);
65 break;
66 }
67 break;
68 default:
69 abort ();
70 }
71
72 /* Do the real work. */
73
74 return 0;
75 }