diff options
Diffstat (limited to 'src/regress/lib/libc/getopt_long')
| -rw-r--r-- | src/regress/lib/libc/getopt_long/Makefile | 32 | ||||
| -rw-r--r-- | src/regress/lib/libc/getopt_long/getopt_long_test.c | 114 | ||||
| -rw-r--r-- | src/regress/lib/libc/getopt_long/test.ok | 84 |
3 files changed, 230 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..13a187e190 --- /dev/null +++ b/src/regress/lib/libc/getopt_long/Makefile | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.4 2002/12/08 19:25:08 millert Exp $ | ||
| 2 | |||
| 3 | NOMAN= | ||
| 4 | PROG=getopt_long_test | ||
| 5 | CLEANFILES+=test.out | ||
| 6 | |||
| 7 | # test getopt_long and getopt_long_only | ||
| 8 | run-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 new file mode 100644 index 0000000000..9f6f603702 --- /dev/null +++ b/src/regress/lib/libc/getopt_long/getopt_long_test.c | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> | ||
| 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 | |||
| 32 | int | ||
| 33 | main(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 new file mode 100644 index 0000000000..9782087f40 --- /dev/null +++ b/src/regress/lib/libc/getopt_long/test.ok | |||
| @@ -0,0 +1,84 @@ | |||
| 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 -Williterate -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: -1bfast - | ||
| 14 | option 1 | ||
| 15 | option b | ||
| 16 | option f, arg ast | ||
| 17 | option - | ||
| 18 | |||
| 19 | getopt_long: --fast --drinking=guiness -i foo.in somefile | ||
| 20 | option fast | ||
| 21 | option drinking, arg guiness | ||
| 22 | option i, arg foo.in | ||
| 23 | remaining ARGV: somefile | ||
| 24 | |||
| 25 | getopt_long (POSIXLY_CORRECT): myfile --force -f infile -9 | ||
| 26 | remaining ARGV: myfile --force -f infile -9 | ||
| 27 | |||
| 28 | getopt_long (POSIXLY_CORRECT): onefile twofile --best -Williterate -i foo.in threefile | ||
| 29 | remaining ARGV: onefile twofile --best -Williterate -i foo.in threefile | ||
| 30 | |||
| 31 | getopt_long (POSIXLY_CORRECT): -1bfast - | ||
| 32 | option 1 | ||
| 33 | option b | ||
| 34 | option f, arg ast | ||
| 35 | option - | ||
| 36 | |||
| 37 | getopt_long (POSIXLY_CORRECT): --fast --drinking=guiness -i foo.in somefile | ||
| 38 | option fast | ||
| 39 | option drinking, arg guiness | ||
| 40 | option i, arg foo.in | ||
| 41 | remaining ARGV: somefile | ||
| 42 | |||
| 43 | getopt_long_only: myfile -force -f infile -9 | ||
| 44 | option force | ||
| 45 | option f, arg infile | ||
| 46 | option 9 | ||
| 47 | remaining ARGV: myfile | ||
| 48 | |||
| 49 | getopt_long_only: onefile twofile -best -Williterate -i foo.in threefile | ||
| 50 | option best | ||
| 51 | option illiterate | ||
| 52 | option i, arg foo.in | ||
| 53 | remaining ARGV: onefile twofile threefile | ||
| 54 | |||
| 55 | getopt_long_only: -1bfast - | ||
| 56 | option 1 | ||
| 57 | option b | ||
| 58 | option fast | ||
| 59 | option - | ||
| 60 | |||
| 61 | getopt_long_only: --fast -drinking=guiness -i foo.in somefile | ||
| 62 | option fast | ||
| 63 | option drinking, arg guiness | ||
| 64 | option i, arg foo.in | ||
| 65 | remaining ARGV: somefile | ||
| 66 | |||
| 67 | getopt_long_only (POSIXLY_CORRECT): myfile -force -f infile -9 | ||
| 68 | remaining ARGV: myfile -force -f infile -9 | ||
| 69 | |||
| 70 | getopt_long_only (POSIXLY_CORRECT): onefile twofile -best -Williterate -i foo.in threefile | ||
| 71 | remaining ARGV: onefile twofile -best -Williterate -i foo.in threefile | ||
| 72 | |||
| 73 | getopt_long_only (POSIXLY_CORRECT): -1bfast - | ||
| 74 | option 1 | ||
| 75 | option b | ||
| 76 | option fast | ||
| 77 | option - | ||
| 78 | |||
| 79 | getopt_long_only (POSIXLY_CORRECT): --fast -drinking=guiness -i foo.in somefile | ||
| 80 | option fast | ||
| 81 | option drinking, arg guiness | ||
| 82 | option i, arg foo.in | ||
| 83 | remaining ARGV: somefile | ||
| 84 | |||
