summaryrefslogtreecommitdiff
path: root/src/regress/lib
diff options
context:
space:
mode:
authormillert <>2002-12-05 21:42:17 +0000
committermillert <>2002-12-05 21:42:17 +0000
commit33cc42bb698271fab5f0273c3b9acc3a530efb59 (patch)
treebc3cf3876859499512cf37e6beb5fa254e9939f9 /src/regress/lib
parent2e23f44ecc2454409cc9334d4501be2d9ee88dd4 (diff)
downloadopenbsd-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/Makefile27
-rw-r--r--src/regress/lib/libc/getopt_long/getopt_long_test.c117
-rw-r--r--src/regress/lib/libc/getopt_long/test.ok60
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
3NOMAN=
4PROG=getopt_long_test
5
6# test getopt_long and getopt_long_only
7run-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
37int
38main(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 @@
1getopt_long: myfile --force -f infile -9
2option force
3option f, arg infile
4option 9
5remaining ARGV: myfile
6
7getopt_long: onefile twofile --best --illiterate -i foo.in threefile
8option best
9option illiterate
10option i, arg foo.in
11remaining ARGV: onefile twofile threefile
12
13getopt_long: --fast --drinking guiness -i foo.in somefile
14option fast
15option drinking, arg guiness
16option i, arg foo.in
17remaining ARGV: somefile
18
19getopt_long (POSIXLY_CORRECT): myfile --force -f infile -9
20remaining ARGV: myfile --force -f infile -9
21
22getopt_long (POSIXLY_CORRECT): onefile twofile --best --illiterate -i foo.in threefile
23remaining ARGV: onefile twofile --best --illiterate -i foo.in threefile
24
25getopt_long (POSIXLY_CORRECT): --fast --drinking guiness -i foo.in somefile
26option fast
27option drinking, arg guiness
28option i, arg foo.in
29remaining ARGV: somefile
30
31getopt_long_only: myfile -force -f infile -9
32option force
33option f, arg infile
34option 9
35remaining ARGV: myfile
36
37getopt_long_only: onefile twofile --best -illiterate -i foo.in threefile
38option best
39option illiterate
40option i, arg foo.in
41remaining ARGV: onefile twofile threefile
42
43getopt_long_only: --fast -drinking guiness -i foo.in somefile
44option fast
45option drinking, arg guiness
46option i, arg foo.in
47remaining ARGV: somefile
48
49getopt_long_only (POSIXLY_CORRECT): myfile -force -f infile -9
50remaining ARGV: myfile -force -f infile -9
51
52getopt_long_only (POSIXLY_CORRECT): onefile twofile --best -illiterate -i foo.in threefile
53remaining ARGV: onefile twofile --best -illiterate -i foo.in threefile
54
55getopt_long_only (POSIXLY_CORRECT): --fast -drinking guiness -i foo.in somefile
56option fast
57option drinking, arg guiness
58option i, arg foo.in
59remaining ARGV: somefile
60