summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/getopt
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
committercvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
commiteb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch)
treeedb6da6af7e865d488dc1a29309f1e1ec226e603 /src/regress/lib/libc/getopt
parent247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff)
downloadopenbsd-tb_20250414.tar.gz
openbsd-tb_20250414.tar.bz2
openbsd-tb_20250414.zip
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to '')
-rw-r--r--src/regress/lib/libc/getopt/Makefile10
-rw-r--r--src/regress/lib/libc/getopt/getopt-test.c59
-rw-r--r--src/regress/lib/libc/getopt/getopt.sh136
-rw-r--r--src/regress/lib/libc/getopt_long/Makefile32
-rw-r--r--src/regress/lib/libc/getopt_long/getopt_long_test.c114
-rw-r--r--src/regress/lib/libc/getopt_long/test.ok84
6 files changed, 0 insertions, 435 deletions
diff --git a/src/regress/lib/libc/getopt/Makefile b/src/regress/lib/libc/getopt/Makefile
deleted file mode 100644
index bdafc965fe..0000000000
--- a/src/regress/lib/libc/getopt/Makefile
+++ /dev/null
@@ -1,10 +0,0 @@
1# $OpenBSD: Makefile,v 1.1 2020/03/23 03:01:21 schwarze Exp $
2
3REGRESS_TARGETS = getopt
4PROG = getopt-test
5MAN =
6
7getopt: ${PROG}
8 sh ${.CURDIR}/getopt.sh
9
10.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/getopt/getopt-test.c b/src/regress/lib/libc/getopt/getopt-test.c
deleted file mode 100644
index 40901dcfcf..0000000000
--- a/src/regress/lib/libc/getopt/getopt-test.c
+++ /dev/null
@@ -1,59 +0,0 @@
1/* $OpenBSD: getopt-test.c,v 1.1 2020/03/23 03:01:21 schwarze Exp $ */
2/*
3 * Copyright (c) 2020 Ingo Schwarze <schwarze@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20
21/*
22 * Process command line options and arguments according to the
23 * optstring in the environment variable OPTS. Write:
24 * OPT(c) for an option "-c" without an option argument
25 * OPT(carg) for an option "-c" with an option argument "arg"
26 * ARG(arg) for a non-option argument "arg"
27 * NONE(arg) for a non-option argument "arg" processed with OPTS =~ ^-
28 * ERR(?c) for an invalid option "-c", or one lacking an argument
29 * ERR(:c) for an option "-c" lacking an argument while OPTS =~ ^:
30 */
31int
32main(int argc, char *argv[])
33{
34 char *optstring;
35 int ch;
36
37 if ((optstring = getenv("OPTS")) == NULL)
38 optstring = "";
39
40 opterr = 0;
41 while ((ch = getopt(argc, argv, optstring)) != -1) {
42 switch (ch) {
43 case '\1':
44 printf("NONE(%s)", optarg);
45 break;
46 case ':':
47 case '?':
48 printf("ERR(%c%c)", ch, optopt);
49 break;
50 default:
51 printf("OPT(%c%s)", ch, optarg == NULL ? "" : optarg);
52 break;
53 }
54 }
55 while (optind < argc)
56 printf("ARG(%s)", argv[optind++]);
57 putchar('\n');
58 return 0;
59}
diff --git a/src/regress/lib/libc/getopt/getopt.sh b/src/regress/lib/libc/getopt/getopt.sh
deleted file mode 100644
index 6e02163d5b..0000000000
--- a/src/regress/lib/libc/getopt/getopt.sh
+++ /dev/null
@@ -1,136 +0,0 @@
1#!/bin/sh
2#
3# $OpenBSD: getopt.sh,v 1.2 2020/05/27 22:32:22 schwarze Exp $
4#
5# Copyright (c) 2020 Ingo Schwarze <schwarze@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19# Run ./getopt-test once.
20# Function arguments:
21# 1. optstring argument for getopt(3)
22# 2. space-separated command line arguments
23# 3. expected output from ./getopt-test
24test1_getopt()
25{
26 result=$(OPTS=$1 ./getopt-test $2)
27 test "$result" == "$3" && return
28 echo "OPTS=$1 ./getopt-test $2"
29 echo "expected: $3"
30 echo "result: $result"
31 irc=1
32}
33
34# Test both without and with the optstring modifier "+",
35# verifying that it makes no difference.
36test2_getopt()
37{
38 test1_getopt "$1" "$2" "$3"
39 test1_getopt "+$1" "$2" "$3"
40}
41
42# Also test with the GNU "-" optstring modifier,
43# veryfying that it only changes ARG() to NONE().
44# This test function is inadequate in two situations:
45# a) options follow non-option arguments that terminate option processing
46# b) or any arguments follow explicit "--".
47# In these cases, use test2_getopt() plus a separate test1_getopt(-...).
48test3_getopt()
49{
50 test2_getopt "$1" "$2" "$3"
51 test1_getopt "-$1" "$2" $(echo $3 | sed s/ARG/NONE/g)
52}
53
54irc=0
55
56# valid isolated options without arguments
57test3_getopt ax '-a -x arg' 'OPT(a)OPT(x)ARG(arg)'
58test3_getopt x- '- -x arg' 'OPT(-)OPT(x)ARG(arg)'
59
60# invalid isolated options without arguments
61test3_getopt ax '-a -y arg' 'OPT(a)ERR(?y)ARG(arg)'
62test1_getopt :ax '-a -y arg' 'OPT(a)ERR(?y)ARG(arg)'
63test2_getopt x '- -x arg' 'ARG(-)ARG(-x)ARG(arg)'
64test1_getopt -x '- -x arg' 'NONE(-)OPT(x)NONE(arg)'
65test3_getopt a- '-a - -x arg' 'OPT(a)OPT(-)ERR(?x)ARG(arg)'
66test1_getopt :a- '-a - -x arg' 'OPT(a)OPT(-)ERR(?x)ARG(arg)'
67
68# valid grouped options without arguments
69test3_getopt ax '-ax arg' 'OPT(a)OPT(x)ARG(arg)'
70test3_getopt ax- '-a- -x arg' 'OPT(a)OPT(-)OPT(x)ARG(arg)'
71test3_getopt abx- '-a-b -x arg' 'OPT(a)OPT(-)OPT(b)OPT(x)ARG(arg)'
72test3_getopt ax- '--a -x arg' 'OPT(-)OPT(a)OPT(x)ARG(arg)'
73
74# invalid grouped options without arguments
75test3_getopt ax '-ay arg' 'OPT(a)ERR(?y)ARG(arg)'
76test1_getopt :ax '-ay arg' 'OPT(a)ERR(?y)ARG(arg)'
77test3_getopt ax '-a- -x arg' 'OPT(a)ERR(?-)OPT(x)ARG(arg)'
78test1_getopt :ax '-a- -x arg' 'OPT(a)ERR(?-)OPT(x)ARG(arg)'
79test3_getopt abx '-a-b -x arg' 'OPT(a)ERR(?-)OPT(b)OPT(x)ARG(arg)'
80test1_getopt :abx '-a-b -x arg' 'OPT(a)ERR(?-)OPT(b)OPT(x)ARG(arg)'
81test3_getopt ax '--a -x arg' 'ERR(?-)OPT(a)OPT(x)ARG(arg)'
82test1_getopt :ax '--a -x arg' 'ERR(?-)OPT(a)OPT(x)ARG(arg)'
83
84# non-option arguments terminating option processing
85test2_getopt ax '-a arg -x' 'OPT(a)ARG(arg)ARG(-x)'
86test1_getopt -ax '-a arg1 -x arg2' 'OPT(a)NONE(arg1)OPT(x)NONE(arg2)'
87test2_getopt ax '-a -- -x' 'OPT(a)ARG(-x)'
88test1_getopt -ax '-a -- -x' 'OPT(a)ARG(-x)'
89test2_getopt ax '-a - -x' 'OPT(a)ARG(-)ARG(-x)'
90test1_getopt -ax '-a - -x arg' 'OPT(a)NONE(-)OPT(x)NONE(arg)'
91
92# the ':' option never works
93test1_getopt ::a '-:a arg' 'ERR(?:)OPT(a)ARG(arg)'
94test1_getopt :::a '-: arg -a' 'ERR(?:)ARG(arg)ARG(-a)'
95
96# isolated options with arguments
97test3_getopt o: '-o' 'ERR(?o)'
98test1_getopt :o: '-o' 'ERR(:o)'
99test3_getopt o-: '-' 'ERR(?-)'
100test1_getopt :-: '-' 'ERR(:-)'
101test3_getopt o:x '-o arg -x arg' 'OPT(oarg)OPT(x)ARG(arg)'
102test3_getopt o:x '-oarg -x arg' 'OPT(oarg)OPT(x)ARG(arg)'
103test3_getopt o::x '-oarg -x arg' 'OPT(oarg)OPT(x)ARG(arg)'
104test2_getopt o::x '-o arg -x' 'OPT(o)ARG(arg)ARG(-x)'
105test1_getopt -o::x '-o arg1 -x arg2' 'OPT(o)NONE(arg1)OPT(x)NONE(arg2)'
106test3_getopt o:x '-o -x arg' 'OPT(o-x)ARG(arg)'
107test3_getopt o:x '-o -- -x arg' 'OPT(o--)OPT(x)ARG(arg)'
108test3_getopt x-: '- arg -x arg' 'OPT(-arg)OPT(x)ARG(arg)'
109test3_getopt x-: '--arg -x arg' 'OPT(-arg)OPT(x)ARG(arg)'
110test3_getopt x-:: '--arg -x arg' 'OPT(-arg)OPT(x)ARG(arg)'
111test2_getopt x-:: '- arg -x' 'OPT(-)ARG(arg)ARG(-x)'
112test1_getopt --::x '- arg1 -x arg2' 'OPT(-)NONE(arg1)OPT(x)NONE(arg2)'
113test3_getopt x-: '- -x arg' 'OPT(--x)ARG(arg)'
114test3_getopt x-: '- -- -x arg' 'OPT(---)OPT(x)ARG(arg)'
115
116# grouped options with arguments
117test3_getopt ao: '-ao' 'OPT(a)ERR(?o)'
118test1_getopt :ao: '-ao' 'OPT(a)ERR(:o)'
119test3_getopt a-: '-a-' 'OPT(a)ERR(?-)'
120test1_getopt :a-: '-a-' 'OPT(a)ERR(:-)'
121test3_getopt ao:x '-ao arg -x arg' 'OPT(a)OPT(oarg)OPT(x)ARG(arg)'
122test3_getopt ao:x '-aoarg -x arg' 'OPT(a)OPT(oarg)OPT(x)ARG(arg)'
123test3_getopt ao::x '-aoarg -x arg' 'OPT(a)OPT(oarg)OPT(x)ARG(arg)'
124test2_getopt ao::x '-ao arg -x' 'OPT(a)OPT(o)ARG(arg)ARG(-x)'
125test1_getopt -ao::x '-ao arg1 -x arg2' 'OPT(a)OPT(o)NONE(arg1)OPT(x)NONE(arg2)'
126test3_getopt ao:x '-ao -x arg' 'OPT(a)OPT(o-x)ARG(arg)'
127test3_getopt ao:x '-ao -- -x arg' 'OPT(a)OPT(o--)OPT(x)ARG(arg)'
128test3_getopt a-:x '-a- arg -x arg' 'OPT(a)OPT(-arg)OPT(x)ARG(arg)'
129test3_getopt a-:x '-a-arg -x arg' 'OPT(a)OPT(-arg)OPT(x)ARG(arg)'
130test3_getopt a-::x '-a-arg -x arg' 'OPT(a)OPT(-arg)OPT(x)ARG(arg)'
131test2_getopt a-::x '-a- arg -x' 'OPT(a)OPT(-)ARG(arg)ARG(-x)'
132test1_getopt -a-::x '-a- arg1 -x arg2' 'OPT(a)OPT(-)NONE(arg1)OPT(x)NONE(arg2)'
133test3_getopt a-:x '-a- -x arg' 'OPT(a)OPT(--x)ARG(arg)'
134test3_getopt a-:x '-a- -- -x arg' 'OPT(a)OPT(---)OPT(x)ARG(arg)'
135
136exit $irc
diff --git a/src/regress/lib/libc/getopt_long/Makefile b/src/regress/lib/libc/getopt_long/Makefile
deleted file mode 100644
index 13a187e190..0000000000
--- a/src/regress/lib/libc/getopt_long/Makefile
+++ /dev/null
@@ -1,32 +0,0 @@
1# $OpenBSD: Makefile,v 1.4 2002/12/08 19:25:08 millert Exp $
2
3NOMAN=
4PROG=getopt_long_test
5CLEANFILES+=test.out
6
7# test getopt_long and getopt_long_only
8run-regress-${PROG}: ${PROG}
9 @( test -n "$$POSIXLY_CORRECT" && unset POSIXLY_CORRECT; \
10 test -n "$$LONG_ONLY" && unset LONG_ONLY; \
11 ./${PROG} myfile --force -f infile -9 ; \
12 ./${PROG} onefile twofile --best -Williterate -i foo.in threefile ; \
13 ./${PROG} -1bfast - ; \
14 ./${PROG} --fast --drinking=guiness -i foo.in somefile ; \
15 export POSIXLY_CORRECT=1 ; \
16 ./${PROG} myfile --force -f infile -9 ; \
17 ./${PROG} onefile twofile --best -Williterate -i foo.in threefile ; \
18 ./${PROG} -1bfast - ; \
19 ./${PROG} --fast --drinking=guiness -i foo.in somefile ; \
20 unset POSIXLY_CORRECT ; export LONG_ONLY=1 ; \
21 ./${PROG} myfile -force -f infile -9 ; \
22 ./${PROG} onefile twofile -best -Williterate -i foo.in threefile ; \
23 ./${PROG} -1bfast - ; \
24 ./${PROG} --fast -drinking=guiness -i foo.in somefile ; \
25 export POSIXLY_CORRECT=1 ; \
26 ./${PROG} myfile -force -f infile -9 ; \
27 ./${PROG} onefile twofile -best -Williterate -i foo.in threefile ; \
28 ./${PROG} -1bfast - ; \
29 ./${PROG} --fast -drinking=guiness -i foo.in somefile ) >test.out 2>&1
30 cmp -s ${.OBJDIR}/test.out ${.CURDIR}/test.ok
31
32.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
deleted file mode 100644
index 9e32f94501..0000000000
--- a/src/regress/lib/libc/getopt_long/getopt_long_test.c
+++ /dev/null
@@ -1,114 +0,0 @@
1/*
2 * Copyright (c) 2002 Todd C. Miller <millert@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <getopt.h>
24
25/*
26 * Simple getopt_long() and getopt_long_only() excerciser.
27 * ENVIRONMENT:
28 * LONG_ONLY : use getopt_long_only() (default is getopt_long())
29 * POSIXLY_CORRECT : don't permute args
30 */
31
32int
33main(int argc, char **argv)
34{
35 int ch, idx, goggles;
36 int (*gl)(int, char * const *, const char *, const struct option *, int *);
37 struct option longopts[] = {
38 { "force", no_argument, 0, 0 },
39 { "fast", no_argument, 0, '1' },
40 { "best", no_argument, 0, '9' },
41 { "input", required_argument, 0, 'i' },
42 { "illiterate", no_argument, 0, 0 },
43 { "drinking", required_argument, &goggles, 42 },
44 { "help", no_argument, 0, 'h' },
45 { 0, 0, 0, 0 },
46 };
47
48 if (getenv("LONG_ONLY")) {
49 gl = getopt_long_only;
50 printf("getopt_long_only");
51 } else {
52 gl = getopt_long;
53 printf("getopt_long");
54 }
55 if (getenv("POSIXLY_CORRECT"))
56 printf(" (POSIXLY_CORRECT)");
57 printf(": ");
58 for (idx = 1; idx < argc; idx++)
59 printf("%s ", argv[idx]);
60 printf("\n");
61
62 goggles = 0;
63 for (;;) {
64 idx = -1;
65 ch = gl(argc, argv, "19bf:i:hW;-", longopts, &idx);
66 if (ch == -1)
67 break;
68 switch (ch) {
69 case 0:
70 case '1':
71 case '9':
72 case 'h':
73 case 'b':
74 case '-':
75 if (idx != -1) {
76 if (goggles == 42)
77 printf("option %s, arg %s\n",
78 longopts[idx].name, optarg);
79 else
80 printf("option %s\n",
81 longopts[idx].name);
82 } else
83 printf("option %c\n", ch);
84 break;
85 case 'f':
86 case 'i':
87 if (idx != -1)
88 printf("option %s, arg %s\n",
89 longopts[idx].name, optarg);
90 else
91 printf("option %c, arg %s\n", ch, optarg);
92 break;
93
94 case '?':
95 break;
96
97 default:
98 printf("unexpected return value: %c\n", ch);
99 break;
100 }
101 }
102 argc -= optind;
103 argv += optind;
104
105 if (argc > 0) {
106 printf("remaining ARGV: ");
107 while (argc--)
108 printf("%s ", *argv++);
109 printf("\n");
110 }
111 printf("\n");
112
113 exit (0);
114}
diff --git a/src/regress/lib/libc/getopt_long/test.ok b/src/regress/lib/libc/getopt_long/test.ok
deleted file mode 100644
index 9782087f40..0000000000
--- a/src/regress/lib/libc/getopt_long/test.ok
+++ /dev/null
@@ -1,84 +0,0 @@
1getopt_long: myfile --force -f infile -9
2option force
3option f, arg infile
4option 9
5remaining ARGV: myfile
6
7getopt_long: onefile twofile --best -Williterate -i foo.in threefile
8option best
9option illiterate
10option i, arg foo.in
11remaining ARGV: onefile twofile threefile
12
13getopt_long: -1bfast -
14option 1
15option b
16option f, arg ast
17option -
18
19getopt_long: --fast --drinking=guiness -i foo.in somefile
20option fast
21option drinking, arg guiness
22option i, arg foo.in
23remaining ARGV: somefile
24
25getopt_long (POSIXLY_CORRECT): myfile --force -f infile -9
26remaining ARGV: myfile --force -f infile -9
27
28getopt_long (POSIXLY_CORRECT): onefile twofile --best -Williterate -i foo.in threefile
29remaining ARGV: onefile twofile --best -Williterate -i foo.in threefile
30
31getopt_long (POSIXLY_CORRECT): -1bfast -
32option 1
33option b
34option f, arg ast
35option -
36
37getopt_long (POSIXLY_CORRECT): --fast --drinking=guiness -i foo.in somefile
38option fast
39option drinking, arg guiness
40option i, arg foo.in
41remaining ARGV: somefile
42
43getopt_long_only: myfile -force -f infile -9
44option force
45option f, arg infile
46option 9
47remaining ARGV: myfile
48
49getopt_long_only: onefile twofile -best -Williterate -i foo.in threefile
50option best
51option illiterate
52option i, arg foo.in
53remaining ARGV: onefile twofile threefile
54
55getopt_long_only: -1bfast -
56option 1
57option b
58option fast
59option -
60
61getopt_long_only: --fast -drinking=guiness -i foo.in somefile
62option fast
63option drinking, arg guiness
64option i, arg foo.in
65remaining ARGV: somefile
66
67getopt_long_only (POSIXLY_CORRECT): myfile -force -f infile -9
68remaining ARGV: myfile -force -f infile -9
69
70getopt_long_only (POSIXLY_CORRECT): onefile twofile -best -Williterate -i foo.in threefile
71remaining ARGV: onefile twofile -best -Williterate -i foo.in threefile
72
73getopt_long_only (POSIXLY_CORRECT): -1bfast -
74option 1
75option b
76option fast
77option -
78
79getopt_long_only (POSIXLY_CORRECT): --fast -drinking=guiness -i foo.in somefile
80option fast
81option drinking, arg guiness
82option i, arg foo.in
83remaining ARGV: somefile
84