add-patch: plug a leak handling the '/' command
[alt-git.git] / parse.c
blob42d691a0fbb42d62a9f8c1aa7f9cd5033dcb464a
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "parse.h"
5 static uintmax_t get_unit_factor(const char *end)
7 if (!*end)
8 return 1;
9 else if (!strcasecmp(end, "k"))
10 return 1024;
11 else if (!strcasecmp(end, "m"))
12 return 1024 * 1024;
13 else if (!strcasecmp(end, "g"))
14 return 1024 * 1024 * 1024;
15 return 0;
18 int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
20 if (value && *value) {
21 char *end;
22 intmax_t val;
23 intmax_t factor;
25 if (max < 0)
26 BUG("max must be a positive integer");
28 errno = 0;
29 val = strtoimax(value, &end, 0);
30 if (errno == ERANGE)
31 return 0;
32 if (end == value) {
33 errno = EINVAL;
34 return 0;
36 factor = get_unit_factor(end);
37 if (!factor) {
38 errno = EINVAL;
39 return 0;
41 if ((val < 0 && -max / factor > val) ||
42 (val > 0 && max / factor < val)) {
43 errno = ERANGE;
44 return 0;
46 val *= factor;
47 *ret = val;
48 return 1;
50 errno = EINVAL;
51 return 0;
54 static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
56 if (value && *value) {
57 char *end;
58 uintmax_t val;
59 uintmax_t factor;
61 /* negative values would be accepted by strtoumax */
62 if (strchr(value, '-')) {
63 errno = EINVAL;
64 return 0;
66 errno = 0;
67 val = strtoumax(value, &end, 0);
68 if (errno == ERANGE)
69 return 0;
70 if (end == value) {
71 errno = EINVAL;
72 return 0;
74 factor = get_unit_factor(end);
75 if (!factor) {
76 errno = EINVAL;
77 return 0;
79 if (unsigned_mult_overflows(factor, val) ||
80 factor * val > max) {
81 errno = ERANGE;
82 return 0;
84 val *= factor;
85 *ret = val;
86 return 1;
88 errno = EINVAL;
89 return 0;
92 int git_parse_int(const char *value, int *ret)
94 intmax_t tmp;
95 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
96 return 0;
97 *ret = tmp;
98 return 1;
101 int git_parse_int64(const char *value, int64_t *ret)
103 intmax_t tmp;
104 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
105 return 0;
106 *ret = tmp;
107 return 1;
110 int git_parse_ulong(const char *value, unsigned long *ret)
112 uintmax_t tmp;
113 if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
114 return 0;
115 *ret = tmp;
116 return 1;
119 int git_parse_ssize_t(const char *value, ssize_t *ret)
121 intmax_t tmp;
122 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t)))
123 return 0;
124 *ret = tmp;
125 return 1;
128 int git_parse_maybe_bool_text(const char *value)
130 if (!value)
131 return 1;
132 if (!*value)
133 return 0;
134 if (!strcasecmp(value, "true")
135 || !strcasecmp(value, "yes")
136 || !strcasecmp(value, "on"))
137 return 1;
138 if (!strcasecmp(value, "false")
139 || !strcasecmp(value, "no")
140 || !strcasecmp(value, "off"))
141 return 0;
142 return -1;
145 int git_parse_maybe_bool(const char *value)
147 int v = git_parse_maybe_bool_text(value);
148 if (0 <= v)
149 return v;
150 if (git_parse_int(value, &v))
151 return !!v;
152 return -1;
156 * Parse environment variable 'k' as a boolean (in various
157 * possible spellings); if missing, use the default value 'def'.
159 int git_env_bool(const char *k, int def)
161 const char *v = getenv(k);
162 int val;
163 if (!v)
164 return def;
165 val = git_parse_maybe_bool(v);
166 if (val < 0)
167 die(_("bad boolean environment value '%s' for '%s'"),
168 v, k);
169 return val;
173 * Parse environment variable 'k' as ulong with possibly a unit
174 * suffix; if missing, use the default value 'val'.
176 unsigned long git_env_ulong(const char *k, unsigned long val)
178 const char *v = getenv(k);
179 if (v && !git_parse_ulong(v, &val))
180 die(_("failed to parse %s"), k);
181 return val;