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(const char *numstr, int base)
103 return xstrtoul_range_sfx(numstr, base, 0, ULONG_MAX, NULL);
106 unsigned long xatoul_range_sfx(const char *numstr,
109 const struct suffix_mult *suffixes)
111 return xstrtoul_range_sfx(numstr, 10, lower, upper, suffixes);
114 unsigned long xatoul_sfx(const char *numstr,
115 const struct suffix_mult *suffixes)
117 return xstrtoul_range_sfx(numstr, 10, 0, ULONG_MAX, suffixes);
120 unsigned long xatoul_range(const char *numstr,
124 return xstrtol_range_sfx(numstr, 10, lower, upper, NULL);
127 unsigned long xatoul(const char *numstr)
129 return xatoul_sfx(numstr, NULL);
134 long xstrtol_range_sfx(const char *numstr, int base,
137 const struct suffix_mult *suffixes)
139 unsigned long u = LONG_MAX;
141 const char *p = numstr;
143 if ((p[0] == '-') && (p[1] != '+')) {
145 ++u; /* two's complement */
148 r = xstrtoul_range_sfx(p, base, 0, u, suffixes);
150 if (*numstr == '-') {
154 if (r < lower || r > upper) {
155 bb_error_msg_and_die("number %s is not in %ld..%ld range",
156 numstr, lower, upper);
162 long xstrtol_range(const char *numstr, int base, long lower, long upper)
164 return xstrtol_range_sfx(numstr, base, lower, upper, NULL);
167 long xatol_range_sfx(const char *numstr,
170 const struct suffix_mult *suffixes)
172 return xstrtol_range_sfx(numstr, 10, lower, upper, suffixes);
175 long xatol_range(const char *numstr, long lower, long upper)
177 return xstrtol_range_sfx(numstr, 10, lower, upper, NULL);
180 long xatol_sfx(const char *numstr, const struct suffix_mult *suffixes)
182 return xstrtol_range_sfx(numstr, 10, LONG_MIN, LONG_MAX, suffixes);
185 long xatol(const char *numstr)
187 return xstrtol_range_sfx(numstr, 10, LONG_MIN, LONG_MAX, NULL);
192 unsigned xatou(const char *numstr)
194 return xatoul_range(numstr, 0, UINT_MAX);
197 int xatoi_range(const char *numstr, int lower, int upper)
199 return xatol_range(numstr, lower, upper);
202 int xatoi(const char *numstr)
204 return xatol_range(numstr, INT_MIN, INT_MAX);
207 int xatoi_u(const char *numstr)
209 return xatoul_range(numstr, 0, INT_MAX);
212 uint32_t xatou32(const char *numstr)
214 return xatoul_range(numstr, 0, 0xffffffff);
217 uint16_t xatou16(const char *numstr)
219 return xatoul_range(numstr, 0, 0xffff);