2.9
[glibc/nacl-glibc.git] / manual / examples / testopt.c
blob44ca8e4ad0a2cb985567f91cbb65d0b0dd6a6633
1 /*@group*/
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
7 int
8 main (int argc, char **argv)
10 int aflag = 0;
11 int bflag = 0;
12 char *cvalue = NULL;
13 int index;
14 int c;
16 opterr = 0;
17 /*@end group*/
19 /*@group*/
20 while ((c = getopt (argc, argv, "abc:")) != -1)
21 switch (c)
23 case 'a':
24 aflag = 1;
25 break;
26 case 'b':
27 bflag = 1;
28 break;
29 case 'c':
30 cvalue = optarg;
31 break;
32 case '?':
33 if (optopt == 'c')
34 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
35 else if (isprint (optopt))
36 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
37 else
38 fprintf (stderr,
39 "Unknown option character `\\x%x'.\n",
40 optopt);
41 return 1;
42 default:
43 abort ();
45 /*@end group*/
47 /*@group*/
48 printf ("aflag = %d, bflag = %d, cvalue = %s\n",
49 aflag, bflag, cvalue);
51 for (index = optind; index < argc; index++)
52 printf ("Non-option argument %s\n", argv[index]);
53 return 0;
55 /*@end group*/