ref: ulimit-print mrsh/getopt.c -rw-r--r-- 1.3 KiB View raw
                                                                                
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
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <mrsh/getopt.h>
#include <stdio.h>

char *mrsh_optarg = NULL;
int mrsh_optind = 1;
int mrsh_opterr = 1;
int mrsh_optopt = 0;

int mrsh_getopt(int argc, char *const argv[], const char *optstring) {
	assert(argv[argc] == NULL);
	mrsh_optarg = NULL;

	if (mrsh_optind >= argc) {
		return -1;
	}

	if (argv[mrsh_optind][0] != '-') {
		return -1;
	}

	if (argv[mrsh_optind][1] == '\0') {
		return -1;
	}

	if (argv[mrsh_optind][1] == '-') {
		mrsh_optind++;
		return -1;
	}

	const char *c = optstring;
	if (*c == ':') {
		c++;
	}

	mrsh_optopt = 0;
	int opt = argv[mrsh_optind][1];
	for (; *c != '\0'; c++) {
		if (*c != opt) {
			continue;
		}

		if (c[1] != ':') {
			mrsh_optind++;
			return opt;
		}

		if (argv[mrsh_optind][2] != '\0') {
			mrsh_optarg = &argv[mrsh_optind][2]; 
		} else {
			if (mrsh_optind + 2 > argc) {
				mrsh_optopt = opt;
				if (mrsh_opterr != 0 && optstring[0] != ':') {
					fprintf(stderr, "%s: Option '%c' requires an argument.\n",
						argv[0], mrsh_optopt);
				}

				return optstring[0] == ':' ? ':' : '?';
			}

			mrsh_optarg = argv[++mrsh_optind];
		}

		mrsh_optind++;
		return opt;
	}

	if (mrsh_opterr != 0 && optstring[0] != ':') {
		fprintf(stderr, "%s: Option '%c' not found.\n", argv[0], opt);
	}

	return '?';
}