summaryrefslogtreecommitdiffstats
path: root/usr/klibc/tests/getopttest.c
blob: 58619d1c8740aaa875e1464c0a026b26024903b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * getopttest.c
 *
 * Simple test for getopt, set the environment variable GETOPTTEST
 * to give the argument string to getopt()
 */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *const *argv)
{
	const char *parser;
	char showchar[] = "\'?\'";
	int c;

	parser = getenv("GETOPTTEST");
	if (!parser)
		parser = "abzf:o:";

	do {
		c = getopt(argc, argv, parser);
		showchar[1] = c;
		printf
		    ("c = %s, optind = %d (%s), optarg = \"%s\", optopt = \'%c\'\n",
		     (c == EOF) ? "EOF" : showchar, optind, argv[optind],
		     optarg, optopt);
	} while (c != -1);

	return 0;
}