summaryrefslogtreecommitdiff
path: root/posix/tstgetopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'posix/tstgetopt.c')
-rw-r--r--posix/tstgetopt.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/posix/tstgetopt.c b/posix/tstgetopt.c
index 40cf3d9c82..97ae2ef251 100644
--- a/posix/tstgetopt.c
+++ b/posix/tstgetopt.c
@@ -1,6 +1,7 @@
-#include <unistd.h>
-#include <stdio.h>
#include <getopt.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
int
main (int argc, char **argv)
@@ -10,14 +11,19 @@ main (int argc, char **argv)
{"required", required_argument, NULL, 'r'},
{"optional", optional_argument, NULL, 'o'},
{"none", no_argument, NULL, 'n'},
+ {"color", no_argument, NULL, 'C'},
+ {"colour", no_argument, NULL, 'C'},
{NULL, 0, NULL, 0 }
};
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
+ int Cflag = 0;
+ int nflag = 0;
int index;
int c;
+ int result = 0;
while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0)
switch (c)
@@ -31,6 +37,9 @@ main (int argc, char **argv)
case 'c':
cvalue = optarg;
break;
+ case 'C':
+ ++Cflag;
+ break;
case '?':
fputs ("Unknown option.\n", stderr);
return 1;
@@ -40,19 +49,28 @@ main (int argc, char **argv)
case 'r':
printf ("--required %s\n", optarg);
+ result |= strcmp (optarg, "foobar") != 0;
break;
case 'o':
printf ("--optional %s\n", optarg);
+ result |= optarg == NULL || strcmp (optarg, "bazbug") != 0;
break;
case 'n':
puts ("--none");
+ nflag = 1;
break;
}
- printf ("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag, cvalue);
+ printf ("aflag = %d, bflag = %d, cvalue = %s, Cflags = %d, nflag = %d\n",
+ aflag, bflag, cvalue, Cflag, nflag);
+
+ result |= (aflag != 1 || bflag != 1 || cvalue == NULL
+ || strcmp (cvalue, "foobar") != 0 || Cflag != 3 || nflag != 1);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
- return 0;
+ result |= optind + 1 != argc || strcmp (argv[optind], "random") != 0;
+
+ return result;
}