summaryrefslogtreecommitdiff
path: root/lib/libc/unistd/getopt.c
blob: 8ef18d37e269974ace2b99a97027dcaf7ee389b4 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/uio.h>

char *optarg;
int optind = 1, opterr = 1, optopt;

int getopt(int argc, char *const argv[], const char *optstring)
{
	char *optchar;
	char c, d;
	int i;
	static int optpos;

	if (optind == 0) {
		optpos = 0;
		optind = 1;
	}

	if (optind >= argc || !argv[optind]) {
		return -1;
	}

	if (argv[optind][0] != '-') {
		if (optstring[0] == '-') {
			optarg = argv[optind++];
			return 1;
		}

		return -1;
	}

	if (argv[optind][1] == 0) {
		return -1;
	}

	if (argv[optind][1] == '-' && argv[optind][2] == 0) {
		return optind++, -1;
	}

	if (optpos == 0)
		optpos++;

	c = argv[optind][optpos];
	optchar = argv[optind] + optpos++;

	if (argv[optind][optpos] == 0) {
		optind++;
		optpos = 0;
	}

	i = 0;

	do {
		d = optstring[i++];
	} while (d && d != c);

	if (d != c || c == ':') {
		optopt = c;
		if (optstring[0] != ':' && opterr) {
			struct iovec iov[4];
			char opt_char[2] = { *optchar, '\0' };

			iov[0].iov_base = argv[0];
			iov[0].iov_len = strlen(argv[0]);
			iov[1].iov_base = ": invalid option -- ";
			iov[1].iov_len = 20;
			iov[2].iov_base = opt_char;
			iov[2].iov_len = 1;
			iov[3].iov_base = "\n";
			iov[3].iov_len = 1;

			writev(STDERR_FILENO, iov, 4);
		}
		return '?';
	}

	if (optstring[i] == ':') {
		optarg = 0;
		if (optstring[i + 1] != ':' || optpos) {
			optarg = argv[optind++];
			if (optpos) {
				optarg += optpos;
			}
			optpos = 0;
		}
		if (optind > argc) {
			optopt = c;
			if (optstring[0] == ':') {
				return ':';
			}
			if (opterr) {
				struct iovec iov[4];
				char opt_char[2] = { *optchar, '\0' };

				iov[0].iov_base = argv[0];
				iov[0].iov_len = strlen(argv[0]);
				iov[1].iov_base =
					": option requires an argument -- ";
				iov[1].iov_len = 33;
				iov[2].iov_base = opt_char;
				iov[2].iov_len = 1;
				iov[3].iov_base = "\n";
				iov[3].iov_len = 1;

				writev(STDERR_FILENO, iov, 4);
			}
			return '?';
		}
	}

	return c;
}