xref: /openbsd-src/regress/lib/libc/getopt/getopt-test.c (revision 81bcd9b255d6a1d2a377f30c041f79e26222c68d)
1*81bcd9b2Sschwarze /* $OpenBSD: getopt-test.c,v 1.1 2020/03/23 03:01:21 schwarze Exp $ */
2*81bcd9b2Sschwarze /*
3*81bcd9b2Sschwarze  * Copyright (c) 2020 Ingo Schwarze <schwarze@openbsd.org>
4*81bcd9b2Sschwarze  *
5*81bcd9b2Sschwarze  * Permission to use, copy, modify, and distribute this software for any
6*81bcd9b2Sschwarze  * purpose with or without fee is hereby granted, provided that the above
7*81bcd9b2Sschwarze  * copyright notice and this permission notice appear in all copies.
8*81bcd9b2Sschwarze  *
9*81bcd9b2Sschwarze  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*81bcd9b2Sschwarze  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*81bcd9b2Sschwarze  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*81bcd9b2Sschwarze  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*81bcd9b2Sschwarze  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*81bcd9b2Sschwarze  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*81bcd9b2Sschwarze  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*81bcd9b2Sschwarze  */
17*81bcd9b2Sschwarze #include <stdio.h>
18*81bcd9b2Sschwarze #include <stdlib.h>
19*81bcd9b2Sschwarze #include <unistd.h>
20*81bcd9b2Sschwarze 
21*81bcd9b2Sschwarze /*
22*81bcd9b2Sschwarze  * Process command line options and arguments according to the
23*81bcd9b2Sschwarze  * optstring in the environment variable OPTS.  Write:
24*81bcd9b2Sschwarze  * OPT(c) for an option "-c" without an option argument
25*81bcd9b2Sschwarze  * OPT(carg) for an option "-c" with an option argument "arg"
26*81bcd9b2Sschwarze  * ARG(arg) for a non-option argument "arg"
27*81bcd9b2Sschwarze  * NONE(arg) for a non-option argument "arg" processed with OPTS =~ ^-
28*81bcd9b2Sschwarze  * ERR(?c) for an invalid option "-c", or one lacking an argument
29*81bcd9b2Sschwarze  * ERR(:c) for an option "-c" lacking an argument while OPTS =~ ^:
30*81bcd9b2Sschwarze  */
31*81bcd9b2Sschwarze int
main(int argc,char * argv[])32*81bcd9b2Sschwarze main(int argc, char *argv[])
33*81bcd9b2Sschwarze {
34*81bcd9b2Sschwarze 	char	*optstring;
35*81bcd9b2Sschwarze 	int	 ch;
36*81bcd9b2Sschwarze 
37*81bcd9b2Sschwarze 	if ((optstring = getenv("OPTS")) == NULL)
38*81bcd9b2Sschwarze 		optstring = "";
39*81bcd9b2Sschwarze 
40*81bcd9b2Sschwarze 	opterr = 0;
41*81bcd9b2Sschwarze 	while ((ch = getopt(argc, argv, optstring)) != -1) {
42*81bcd9b2Sschwarze 		switch (ch) {
43*81bcd9b2Sschwarze 		case '\1':
44*81bcd9b2Sschwarze 			printf("NONE(%s)", optarg);
45*81bcd9b2Sschwarze 			break;
46*81bcd9b2Sschwarze 		case ':':
47*81bcd9b2Sschwarze 		case '?':
48*81bcd9b2Sschwarze 			printf("ERR(%c%c)", ch, optopt);
49*81bcd9b2Sschwarze 			break;
50*81bcd9b2Sschwarze 		default:
51*81bcd9b2Sschwarze 			printf("OPT(%c%s)", ch, optarg == NULL ? "" : optarg);
52*81bcd9b2Sschwarze 			break;
53*81bcd9b2Sschwarze 		}
54*81bcd9b2Sschwarze 	}
55*81bcd9b2Sschwarze 	while (optind < argc)
56*81bcd9b2Sschwarze 		printf("ARG(%s)", argv[optind++]);
57*81bcd9b2Sschwarze 	putchar('\n');
58*81bcd9b2Sschwarze 	return 0;
59*81bcd9b2Sschwarze }
60