]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/unistd/tstgetopt.c
97ae2ef25115849d5dd4ff0ea8a06dd7f998b42d
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / unistd / tstgetopt.c
1 #include <getopt.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 int
7 main (int argc, char **argv)
8 {
9   static const struct option options[] =
10     {
11       {"required", required_argument, NULL, 'r'},
12       {"optional", optional_argument, NULL, 'o'},
13       {"none",     no_argument,       NULL, 'n'},
14       {"color",    no_argument,       NULL, 'C'},
15       {"colour",   no_argument,       NULL, 'C'},
16       {NULL,       0,                 NULL, 0 }
17     };
18
19   int aflag = 0;
20   int bflag = 0;
21   char *cvalue = NULL;
22   int Cflag = 0;
23   int nflag = 0;
24   int index;
25   int c;
26   int result = 0;
27
28   while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0)
29     switch (c)
30       {
31       case 'a':
32         aflag = 1;
33         break;
34       case 'b':
35         bflag = 1;
36         break;
37       case 'c':
38         cvalue = optarg;
39         break;
40       case 'C':
41         ++Cflag;
42         break;
43       case '?':
44         fputs ("Unknown option.\n", stderr);
45         return 1;
46       default:
47         fprintf (stderr, "This should never happen!\n");
48         return 1;
49
50       case 'r':
51         printf ("--required %s\n", optarg);
52         result |= strcmp (optarg, "foobar") != 0;
53         break;
54       case 'o':
55         printf ("--optional %s\n", optarg);
56         result |= optarg == NULL || strcmp (optarg, "bazbug") != 0;
57         break;
58       case 'n':
59         puts ("--none");
60         nflag = 1;
61         break;
62       }
63
64   printf ("aflag = %d, bflag = %d, cvalue = %s, Cflags = %d, nflag = %d\n",
65           aflag, bflag, cvalue, Cflag, nflag);
66
67   result |= (aflag != 1 || bflag != 1 || cvalue == NULL
68              || strcmp (cvalue, "foobar") != 0 || Cflag != 3 || nflag != 1);
69
70   for (index = optind; index < argc; index++)
71     printf ("Non-option argument %s\n", argv[index]);
72
73   result |= optind + 1 != argc || strcmp (argv[optind], "random") != 0;
74
75   return result;
76 }