1 #include "git-compat-util.h"
5 static uintmax_t get_unit_factor(const char *end
)
9 else if (!strcasecmp(end
, "k"))
11 else if (!strcasecmp(end
, "m"))
13 else if (!strcasecmp(end
, "g"))
14 return 1024 * 1024 * 1024;
18 int git_parse_signed(const char *value
, intmax_t *ret
, intmax_t max
)
20 if (value
&& *value
) {
26 BUG("max must be a positive integer");
29 val
= strtoimax(value
, &end
, 0);
36 factor
= get_unit_factor(end
);
41 if ((val
< 0 && -max
/ factor
> val
) ||
42 (val
> 0 && max
/ factor
< val
)) {
54 static int git_parse_unsigned(const char *value
, uintmax_t *ret
, uintmax_t max
)
56 if (value
&& *value
) {
61 /* negative values would be accepted by strtoumax */
62 if (strchr(value
, '-')) {
67 val
= strtoumax(value
, &end
, 0);
74 factor
= get_unit_factor(end
);
79 if (unsigned_mult_overflows(factor
, val
) ||
92 int git_parse_int(const char *value
, int *ret
)
95 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(int)))
101 int git_parse_int64(const char *value
, int64_t *ret
)
104 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(int64_t)))
110 int git_parse_ulong(const char *value
, unsigned long *ret
)
113 if (!git_parse_unsigned(value
, &tmp
, maximum_unsigned_value_of_type(long)))
119 int git_parse_ssize_t(const char *value
, ssize_t
*ret
)
122 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(ssize_t
)))
128 int git_parse_maybe_bool_text(const char *value
)
134 if (!strcasecmp(value
, "true")
135 || !strcasecmp(value
, "yes")
136 || !strcasecmp(value
, "on"))
138 if (!strcasecmp(value
, "false")
139 || !strcasecmp(value
, "no")
140 || !strcasecmp(value
, "off"))
145 int git_parse_maybe_bool(const char *value
)
147 int v
= git_parse_maybe_bool_text(value
);
150 if (git_parse_int(value
, &v
))
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
);
165 val
= git_parse_maybe_bool(v
);
167 die(_("bad boolean environment value '%s' for '%s'"),
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
);