-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathletopt.c
107 lines (91 loc) · 1.95 KB
/
letopt.c
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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INCLUDED_FROM_LETOPT_C_
#include "letopt.h"
#undef INCLUDED_FROM_LETOPT_C_
extern_letopt_state_init();
extern_letopt_get_number_arg();
extern_letopt_get_string_arg();
extern_letopt_get_long_opt_arg();
#undef extern_letopt_state_init
#undef extern_letopt_get_number_arg
#undef extern_letopt_get_string_arg
#undef extern_letopt_get_long_opt_arg
struct letopt_state
letopt_state_init (int argc,
char **argv)
{
struct letopt_state state = {
.v = argv,
.c = argc
};
if (state.c > 1 &&
!(state.q = calloc((size_t)state.c, sizeof *state.q)))
state.e = errno ? errno : ENOMEM;
return state;
}
bool
letopt_get_number_arg (struct letopt_state *const state,
int64_t *const dest,
const int64_t min,
const int64_t max)
{
if (!*state->p) {
state->e = EINVAL;
return false;
}
errno = 0;
char *end = state->p;
int64_t n = _Generic(n
, long: strtol
, long long: strtoll
)(state->p, &end, 0);
int e = errno;
if (!e) {
if (*end) {
e = EINVAL;
} else if (n < min || n > max) {
e = ERANGE;
} else {
*dest = n;
return true;
}
}
state->e = e;
return false;
}
bool
letopt_get_string_arg (struct letopt_state *const state,
char const **const dest)
{
if (!*state->p) {
state->e = EINVAL;
return false;
}
*dest = state->p;
return true;
}
int
letopt_get_long_opt_arg (struct letopt_state *const state,
const size_t len)
{
do {
if (!state->p[len]) {
if (++state->i >= state->c)
break;
state->p = state->v[state->i];
} else {
if (state->p[len] != '=')
return EAGAIN;
state->p += len + 1U;
}
if (*state->p)
return 0;
} while(0);
state->e = EINVAL;
return EINVAL;
}