diff options
author | millert <> | 2002-12-05 21:42:17 +0000 |
---|---|---|
committer | millert <> | 2002-12-05 21:42:17 +0000 |
commit | 33cc42bb698271fab5f0273c3b9acc3a530efb59 (patch) | |
tree | bc3cf3876859499512cf37e6beb5fa254e9939f9 /src/regress/lib/libc/getopt_long/getopt_long_test.c | |
parent | 2e23f44ecc2454409cc9334d4501be2d9ee88dd4 (diff) | |
download | openbsd-33cc42bb698271fab5f0273c3b9acc3a530efb59.tar.gz openbsd-33cc42bb698271fab5f0273c3b9acc3a530efb59.tar.bz2 openbsd-33cc42bb698271fab5f0273c3b9acc3a530efb59.zip |
simple regress for getopt_long() and getopt_long_only()
Diffstat (limited to 'src/regress/lib/libc/getopt_long/getopt_long_test.c')
-rw-r--r-- | src/regress/lib/libc/getopt_long/getopt_long_test.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/regress/lib/libc/getopt_long/getopt_long_test.c b/src/regress/lib/libc/getopt_long/getopt_long_test.c new file mode 100644 index 0000000000..7b013e534b --- /dev/null +++ b/src/regress/lib/libc/getopt_long/getopt_long_test.c | |||
@@ -0,0 +1,117 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
15 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
16 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
17 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
22 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
23 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
24 | */ | ||
25 | |||
26 | #include <stdio.h> | ||
27 | #include <stdlib.h> | ||
28 | #include <getopt.h> | ||
29 | |||
30 | /* | ||
31 | * Simple getopt_long() and getopt_long_only() excerciser. | ||
32 | * ENVIRONMENT: | ||
33 | * LONG_ONLY : use getopt_long_only() (default is getopt_long()) | ||
34 | * POSIXLY_CORRECT : don't permute args | ||
35 | */ | ||
36 | |||
37 | int | ||
38 | main(int argc, char **argv) | ||
39 | { | ||
40 | int ch, idx, goggles; | ||
41 | int (*gl)(int, char * const *, const char *, const struct option *, int *); | ||
42 | struct option longopts[] = { | ||
43 | { "force", no_argument, 0, 0 }, | ||
44 | { "fast", no_argument, 0, '1' }, | ||
45 | { "best", no_argument, 0, '9' }, | ||
46 | { "input", required_argument, 0, 'i' }, | ||
47 | { "illiterate", no_argument, 0, 0 }, | ||
48 | { "drinking", required_argument, &goggles, 42 }, | ||
49 | { "help", no_argument, 0, 'h' }, | ||
50 | { 0, 0, 0, 0 }, | ||
51 | }; | ||
52 | |||
53 | if (getenv("LONG_ONLY")) { | ||
54 | gl = getopt_long_only; | ||
55 | printf("getopt_long_only"); | ||
56 | } else { | ||
57 | gl = getopt_long; | ||
58 | printf("getopt_long"); | ||
59 | } | ||
60 | if (getenv("POSIXLY_CORRECT")) | ||
61 | printf(" (POSIXLY_CORRECT)"); | ||
62 | printf(": "); | ||
63 | for (idx = 1; idx < argc; idx++) | ||
64 | printf("%s ", argv[idx]); | ||
65 | printf("\n"); | ||
66 | |||
67 | goggles = 0; | ||
68 | for (;;) { | ||
69 | idx = -1; | ||
70 | ch = gl(argc, argv, "19bf:i:h", longopts, &idx); | ||
71 | if (ch == -1) | ||
72 | break; | ||
73 | switch (ch) { | ||
74 | case 0: | ||
75 | case '1': | ||
76 | case '9': | ||
77 | case 'h': | ||
78 | if (idx != -1) { | ||
79 | if (goggles == 42) | ||
80 | printf("option %s, arg %s\n", | ||
81 | longopts[idx].name, optarg); | ||
82 | else | ||
83 | printf("option %s\n", | ||
84 | longopts[idx].name); | ||
85 | } else | ||
86 | printf("option %c\n", ch); | ||
87 | break; | ||
88 | case 'f': | ||
89 | case 'i': | ||
90 | if (idx != -1) | ||
91 | printf("option %s, arg %s\n", | ||
92 | longopts[idx].name, optarg); | ||
93 | else | ||
94 | printf("option %c, arg %s\n", ch, optarg); | ||
95 | break; | ||
96 | |||
97 | case '?': | ||
98 | break; | ||
99 | |||
100 | default: | ||
101 | printf("unexpected return value: %c\n", ch); | ||
102 | break; | ||
103 | } | ||
104 | } | ||
105 | argc -= optind; | ||
106 | argv += optind; | ||
107 | |||
108 | if (argc > 0) { | ||
109 | printf("remaining ARGV: "); | ||
110 | while (argc--) | ||
111 | printf("%s ", *argv++); | ||
112 | printf("\n"); | ||
113 | } | ||
114 | printf("\n"); | ||
115 | |||
116 | exit (0); | ||
117 | } | ||