1 /* vi: set sw=4 ts=4: */
3 * xgetularg* implementations for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
18 #ifdef L_xgetularg_bnd_sfx
19 unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
22 const struct suffix_mult *suffixes)
30 /* Disallow '-' and any leading whitespace. Speed isn't critical here
31 * since we're parsing commandline args. So make sure we get the
32 * actual isspace function rather than a larger macro implementaion. */
33 if ((*arg == '-') || (isspace)(*arg)) {
37 /* Since this is a lib function, we're not allowed to reset errno to 0.
38 * Doing so could break an app that is deferring checking of errno.
39 * So, save the old value so that we can restore it if successful. */
42 r = strtoul(arg, &e, base);
43 /* Do the initial validity check. Note: The standards do not
44 * guarantee that errno is set if no digits were found. So we
45 * must test for this explicitly. */
46 if (errno || (arg == e)) { /* error or no digits */
49 errno = old_errno; /* Ok. So restore errno. */
51 /* Do optional suffix parsing. Allow 'empty' suffix tables.
52 * Note that we also all nul suffixes with associated multipliers,
53 * to allow for scaling of the arg by some default multiplier. */
56 while (suffixes->suffix) {
57 if (strcmp(suffixes->suffix, e) == 0) {
58 if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
69 /* Finally, check for illegal trailing chars and range limits. */
70 /* Note: although we allow leading space (via stroul), trailing space
71 * is an error. It would be easy enough to allow though if desired. */
72 if (*e || (r < lower) || (r > upper)) {
80 #ifdef L_xgetlarg_bnd_sfx
81 long bb_xgetlarg_bnd_sfx(const char *arg, int base,
84 const struct suffix_mult *suffixes)
86 unsigned long u = LONG_MAX;
90 if ((*p == '-') && (p[1] != '+')) {
92 #if LONG_MAX == (-(LONG_MIN + 1))
93 ++u; /* two's complement */
97 r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
103 if ((r < lower) || (r > upper)) {
111 #ifdef L_getlarg10_sfx
112 long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
114 return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
118 #ifdef L_xgetularg_bnd
119 unsigned long bb_xgetularg_bnd(const char *arg, int base,
123 return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
127 #ifdef L_xgetularg10_bnd
128 unsigned long bb_xgetularg10_bnd(const char *arg,
132 return bb_xgetularg_bnd(arg, 10, lower, upper);
137 unsigned long bb_xgetularg10(const char *arg)
139 return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);