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
32 unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
35 const struct suffix_mult *suffixes)
43 /* Disallow '-' and any leading whitespace. Speed isn't critical here
44 * since we're parsing commandline args. So make sure we get the
45 * actual isspace function rather than a larger macro implementaion. */
46 if ((*arg == '-') || (isspace)(*arg)) {
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(arg, &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 || (arg == e)) { /* error or no digits */
62 errno = old_errno; /* Ok. So restore errno. */
64 /* Do optional suffix parsing. Allow 'empty' suffix tables.
65 * Note that we also all nul suffixes with associated multipliers,
66 * to allow for scaling of the arg by some default multiplier. */
69 while (suffixes->suffix) {
70 if (strcmp(suffixes->suffix, e) == 0) {
71 if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
82 /* Finally, check for illegal trailing chars and range limits. */
83 /* Note: although we allow leading space (via stroul), trailing space
84 * is an error. It would be easy enough to allow though if desired. */
85 if (*e || (r < lower) || (r > upper)) {
93 #ifdef L_xgetlarg_bnd_sfx
94 long bb_xgetlarg_bnd_sfx(const char *arg, int base,
97 const struct suffix_mult *suffixes)
99 unsigned long u = LONG_MAX;
103 if ((*p == '-') && (p[1] != '+')) {
105 #if LONG_MAX == (-(LONG_MIN + 1))
106 ++u; /* two's complement */
110 r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
116 if ((r < lower) || (r > upper)) {
124 #ifdef L_getlarg10_sfx
125 long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
127 return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
131 #ifdef L_xgetularg_bnd
132 unsigned long bb_xgetularg_bnd(const char *arg, int base,
136 return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
140 #ifdef L_xgetularg10_bnd
141 unsigned long bb_xgetularg10_bnd(const char *arg,
145 return bb_xgetularg_bnd(arg, 10, lower, upper);
150 unsigned long bb_xgetularg10(const char *arg)
152 return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);