1 /* vi: set sw=4 ts=4: */
3 * xgetularg* implementations for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #ifdef L_xgetularg_bnd_sfx
33 unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
36 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 larger macro implementaion. */
47 if ((*arg == '-') || (isspace)(*arg)) {
51 /* Since this is a lib function, we're not allowed to reset errno to 0.
52 * Doing so could break an app that is deferring checking of errno.
53 * So, save the old value so that we can restore it if successful. */
56 r = strtoul(arg, &e, base);
57 /* Do the initial validity check. Note: The standards do not
58 * guarantee that errno is set if no digits were found. So we
59 * must test for this explicitly. */
60 if (errno || (arg == e)) { /* error or no digits */
63 errno = old_errno; /* Ok. So restore errno. */
65 /* Do optional suffix parsing. Allow 'empty' suffix tables.
66 * Note that we also all nul suffixes with associated multipliers,
67 * to allow for scaling of the arg by some default multiplier. */
70 while (suffixes->suffix) {
71 if (strcmp(suffixes->suffix, e) == 0) {
72 if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
83 /* Finally, check for illegal trailing chars and range limits. */
84 /* Note: although we allow leading space (via stroul), trailing space
85 * is an error. It would be easy enough to allow though if desired. */
86 if (*e || (r < lower) || (r > upper)) {
94 #ifdef L_xgetlarg_bnd_sfx
96 long bb_xgetlarg_bnd_sfx(const char *arg, int base,
99 const struct suffix_mult *suffixes)
101 unsigned long u = LONG_MAX;
105 if ((*p == '-') && (p[1] != '+')) {
107 #if LONG_MAX == (-(LONG_MIN + 1))
108 ++u; /* two's complement */
112 r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
118 if ((r < lower) || (r > upper)) {
126 #ifdef L_getlarg10_sfx
128 long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
130 return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
134 #ifdef L_xgetularg_bnd
136 unsigned long bb_xgetularg_bnd(const char *arg, int base,
140 return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
144 #ifdef L_xgetularg10_bnd
146 unsigned long bb_xgetularg10_bnd(const char *arg,
150 return bb_xgetularg_bnd(arg, 10, lower, upper);
156 unsigned long bb_xgetularg10(const char *arg)
158 return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);