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 | |
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')
-rw-r--r-- | src/regress/lib/libc/getopt_long/Makefile | 27 | ||||
-rw-r--r-- | src/regress/lib/libc/getopt_long/getopt_long_test.c | 117 | ||||
-rw-r--r-- | src/regress/lib/libc/getopt_long/test.ok | 60 |
3 files changed, 204 insertions, 0 deletions
diff --git a/src/regress/lib/libc/getopt_long/Makefile b/src/regress/lib/libc/getopt_long/Makefile new file mode 100644 index 0000000000..5237d82dd2 --- /dev/null +++ b/src/regress/lib/libc/getopt_long/Makefile | |||
@@ -0,0 +1,27 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2002/12/05 21:42:17 millert Exp $ | ||
2 | |||
3 | NOMAN= | ||
4 | PROG=getopt_long_test | ||
5 | |||
6 | # test getopt_long and getopt_long_only | ||
7 | run-regress-${PROG}: ${PROG} | ||
8 | @( test -n "$$POSIXLY_CORRECT" && unset POSIXLY_CORRECT; \ | ||
9 | test -n "$$LONG_ONLY" && unset LONG_ONLY; \ | ||
10 | ./${PROG} myfile --force -f infile -9 ; \ | ||
11 | ./${PROG} onefile twofile --best --illiterate -i foo.in threefile ; \ | ||
12 | ./${PROG} --fast --drinking guiness -i foo.in somefile ; \ | ||
13 | export POSIXLY_CORRECT=1 ; \ | ||
14 | ./${PROG} myfile --force -f infile -9 ; \ | ||
15 | ./${PROG} onefile twofile --best --illiterate -i foo.in threefile ; \ | ||
16 | ./${PROG} --fast --drinking guiness -i foo.in somefile ; \ | ||
17 | unset POSIXLY_CORRECT ; export LONG_ONLY=1 ; \ | ||
18 | ./${PROG} myfile -force -f infile -9 ; \ | ||
19 | ./${PROG} onefile twofile --best -illiterate -i foo.in threefile ; \ | ||
20 | ./${PROG} --fast -drinking guiness -i foo.in somefile ; \ | ||
21 | export POSIXLY_CORRECT=1 ; \ | ||
22 | ./${PROG} myfile -force -f infile -9 ; \ | ||
23 | ./${PROG} onefile twofile --best -illiterate -i foo.in threefile ; \ | ||
24 | ./${PROG} --fast -drinking guiness -i foo.in somefile ) >test.out 2>&1 | ||
25 | cmp -s ${.OBJDIR}/test.out ${.CURDIR}/test.ok | ||
26 | |||
27 | .include <bsd.regress.mk> | ||
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 | } | ||
diff --git a/src/regress/lib/libc/getopt_long/test.ok b/src/regress/lib/libc/getopt_long/test.ok new file mode 100644 index 0000000000..0bade064f6 --- /dev/null +++ b/src/regress/lib/libc/getopt_long/test.ok | |||
@@ -0,0 +1,60 @@ | |||
1 | getopt_long: myfile --force -f infile -9 | ||
2 | option force | ||
3 | option f, arg infile | ||
4 | option 9 | ||
5 | remaining ARGV: myfile | ||
6 | |||
7 | getopt_long: onefile twofile --best --illiterate -i foo.in threefile | ||
8 | option best | ||
9 | option illiterate | ||
10 | option i, arg foo.in | ||
11 | remaining ARGV: onefile twofile threefile | ||
12 | |||
13 | getopt_long: --fast --drinking guiness -i foo.in somefile | ||
14 | option fast | ||
15 | option drinking, arg guiness | ||
16 | option i, arg foo.in | ||
17 | remaining ARGV: somefile | ||
18 | |||
19 | getopt_long (POSIXLY_CORRECT): myfile --force -f infile -9 | ||
20 | remaining ARGV: myfile --force -f infile -9 | ||
21 | |||
22 | getopt_long (POSIXLY_CORRECT): onefile twofile --best --illiterate -i foo.in threefile | ||
23 | remaining ARGV: onefile twofile --best --illiterate -i foo.in threefile | ||
24 | |||
25 | getopt_long (POSIXLY_CORRECT): --fast --drinking guiness -i foo.in somefile | ||
26 | option fast | ||
27 | option drinking, arg guiness | ||
28 | option i, arg foo.in | ||
29 | remaining ARGV: somefile | ||
30 | |||
31 | getopt_long_only: myfile -force -f infile -9 | ||
32 | option force | ||
33 | option f, arg infile | ||
34 | option 9 | ||
35 | remaining ARGV: myfile | ||
36 | |||
37 | getopt_long_only: onefile twofile --best -illiterate -i foo.in threefile | ||
38 | option best | ||
39 | option illiterate | ||
40 | option i, arg foo.in | ||
41 | remaining ARGV: onefile twofile threefile | ||
42 | |||
43 | getopt_long_only: --fast -drinking guiness -i foo.in somefile | ||
44 | option fast | ||
45 | option drinking, arg guiness | ||
46 | option i, arg foo.in | ||
47 | remaining ARGV: somefile | ||
48 | |||
49 | getopt_long_only (POSIXLY_CORRECT): myfile -force -f infile -9 | ||
50 | remaining ARGV: myfile -force -f infile -9 | ||
51 | |||
52 | getopt_long_only (POSIXLY_CORRECT): onefile twofile --best -illiterate -i foo.in threefile | ||
53 | remaining ARGV: onefile twofile --best -illiterate -i foo.in threefile | ||
54 | |||
55 | getopt_long_only (POSIXLY_CORRECT): --fast -drinking guiness -i foo.in somefile | ||
56 | option fast | ||
57 | option drinking, arg guiness | ||
58 | option i, arg foo.in | ||
59 | remaining ARGV: somefile | ||
60 | |||