1 /* vi: set sw=4 ts=4: */
3 * ascii-to-numbers implementations for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
12 unsigned long long xstrtoull(const char *numstr, int base)
17 if ((*numstr == '-') || (isspace)(*numstr))
18 bb_error_msg_and_die("invalid number '%s'", numstr);
21 r = strtoull(numstr, &e, base);
22 if (errno || (numstr == e) || *e)
23 /* Error / no digits / illegal trailing chars */
24 bb_error_msg_and_die("invalid number '%s'", numstr);
25 /* No error. So restore errno. */
30 unsigned long long xatoull(const char *numstr)
32 return xstrtoull(numstr, 10);
35 unsigned long xstrtoul_range_sfx(const char *numstr, int base,
38 const struct suffix_mult *suffixes)
44 /* Disallow '-' and any leading whitespace. Speed isn't critical here
45 * since we're parsing commandline args. So make sure we get the
46 * actual isspace function rather than a lnumstrer macro implementaion. */
47 if ((*numstr == '-') || (isspace)(*numstr))
50 /* Since this is a lib function, we're not allowed to reset errno to 0.
51 * Doing so could break an app that is deferring checking of errno.
52 * So, save the old value so that we can restore it if successful. */
55 r = strtoul(numstr, &e, base);
56 /* Do the initial validity check. Note: The standards do not
57 * guarantee that errno is set if no digits were found. So we
58 * must test for this explicitly. */
59 if (errno || (numstr == e))
60 goto inval; /* error / no digits / illegal trailing chars */
62 errno = old_errno; /* Ok. So restore errno. */
64 /* Do optional suffix parsing. Allow 'empty' suffix tables.
65 * Note that we also allow nul suffixes with associated multipliers,
66 * to allow for scaling of the numstr by some default multiplier. */
68 while (suffixes->suffix) {
69 if (strcmp(suffixes->suffix, e) == 0) {
70 if (ULONG_MAX / suffixes->mult < r)
71 goto range; /* overflow! */
80 /* Note: trailing space is an error.
81 It would be easy enough to allow though if desired. */
84 /* Finally, check for range limits. */
85 if (r >= lower && r <= upper)
88 bb_error_msg_and_die("number %s is not in %lu..%lu range",
89 numstr, lower, upper);
91 bb_error_msg_and_die("invalid number '%s'", numstr);
94 unsigned long xstrtoul_range(const char *numstr, int base,
98 return xstrtoul_range_sfx(numstr, base, lower, upper, NULL);
101 unsigned long xstrtoul_sfx(const char *numstr, int base,
102 const struct suffix_mult *suffixes)
104 return xstrtoul_range_sfx(numstr, base, 0, ULONG_MAX, suffixes);
107 unsigned long xstrtoul(const char *numstr, int base)
109 return xstrtoul_range_sfx(numstr, base, 0, ULONG_MAX, NULL);
112 unsigned long xatoul_range_sfx(const char *numstr,
115 const struct suffix_mult *suffixes)
117 return xstrtoul_range_sfx(numstr, 10, lower, upper, suffixes);
120 unsigned long xatoul_sfx(const char *numstr,
121 const struct suffix_mult *suffixes)
123 return xstrtoul_range_sfx(numstr, 10, 0, ULONG_MAX, suffixes);
126 unsigned long xatoul_range(const char *numstr,
130 return xstrtoul_range_sfx(numstr, 10, lower, upper, NULL);
133 unsigned long xatoul(const char *numstr)
135 return xatoul_sfx(numstr, NULL);
140 long xstrtol_range_sfx(const char *numstr, int base,
143 const struct suffix_mult *suffixes)
145 unsigned long u = LONG_MAX;
147 const char *p = numstr;
149 if ((p[0] == '-') && (p[1] != '+')) {
151 ++u; /* two's complement */
154 r = xstrtoul_range_sfx(p, base, 0, u, suffixes);
156 if (*numstr == '-') {
160 if (r < lower || r > upper) {
161 bb_error_msg_and_die("number %s is not in %ld..%ld range",
162 numstr, lower, upper);
168 long xstrtol_range(const char *numstr, int base, long lower, long upper)
170 return xstrtol_range_sfx(numstr, base, lower, upper, NULL);
173 long xatol_range_sfx(const char *numstr,
176 const struct suffix_mult *suffixes)
178 return xstrtol_range_sfx(numstr, 10, lower, upper, suffixes);
181 long xatol_range(const char *numstr, long lower, long upper)
183 return xstrtol_range_sfx(numstr, 10, lower, upper, NULL);
186 long xatol_sfx(const char *numstr, const struct suffix_mult *suffixes)
188 return xstrtol_range_sfx(numstr, 10, LONG_MIN, LONG_MAX, suffixes);
191 long xatol(const char *numstr)
193 return xstrtol_range_sfx(numstr, 10, LONG_MIN, LONG_MAX, NULL);
198 unsigned xatou_range(const char *numstr, unsigned lower, unsigned upper)
200 return xstrtoul_range_sfx(numstr, 10, lower, upper, NULL);
203 unsigned xatou_sfx(const char *numstr, const struct suffix_mult *suffixes)
205 return xstrtoul_range_sfx(numstr, 10, 0, UINT_MAX, suffixes);
208 unsigned xatou(const char *numstr)
210 return xatoul_range(numstr, 0, UINT_MAX);
213 int xatoi_range(const char *numstr, int lower, int upper)
215 return xatol_range(numstr, lower, upper);
218 int xatoi(const char *numstr)
220 return xatol_range(numstr, INT_MIN, INT_MAX);
223 int xatoi_u(const char *numstr)
225 return xatoul_range(numstr, 0, INT_MAX);
228 uint32_t xatou32(const char *numstr)
230 return xatoul_range(numstr, 0, 0xffffffff);
233 uint16_t xatou16(const char *numstr)
235 return xatoul_range(numstr, 0, 0xffff);