~emersion/mrsh

mrsh/getopt.c -rw-r--r-- 1.6 KiB
742831faSimon Ser Replace assert(0) with abort() 2 months ago
                                                                                
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
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdio.h>
#include "mrsh_getopt.h"

char *_mrsh_optarg = NULL;
int _mrsh_optind = 1;
int _mrsh_opterr = 1;
int _mrsh_optopt = 0;
int _mrsh_optpos = 1;

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

	if (_mrsh_optind == 0) {
		_mrsh_optind = 1;
		_mrsh_optpos = 1;
	}

	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][_mrsh_optpos];
	for (; *c != '\0'; c++) {
		if (*c != opt) {
			continue;
		}

		if (c[1] != ':') {
			if (argv[_mrsh_optind][_mrsh_optpos + 1] == '\0') {
				_mrsh_optind++;
				_mrsh_optpos = 1;
			} else {
				_mrsh_optpos++;
			}
			return opt;
		}

		if (argv[_mrsh_optind][_mrsh_optpos + 1] != '\0') {
			_mrsh_optarg = &argv[_mrsh_optind][_mrsh_optpos + 1];
		} 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 '?';
}