Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * xgetularg* implementations for busybox |
| 4 | * |
| 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <limits.h> |
| 26 | #include <ctype.h> |
| 27 | #include <errno.h> |
| 28 | #include <assert.h> |
| 29 | #include "libbb.h" |
| 30 | |
| 31 | #ifdef L_xgetularg_bnd_sfx |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base, |
| 33 | unsigned long lower, |
| 34 | unsigned long upper, |
| 35 | const struct suffix_mult *suffixes) |
| 36 | { |
| 37 | unsigned long r; |
| 38 | int old_errno; |
| 39 | char *e; |
| 40 | |
| 41 | assert(arg); |
| 42 | |
| 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)) { |
| 47 | bb_show_usage(); |
| 48 | } |
| 49 | |
| 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. */ |
| 53 | old_errno = errno; |
| 54 | errno = 0; |
| 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 */ |
| 60 | bb_show_usage(); |
| 61 | } |
| 62 | errno = old_errno; /* Ok. So restore errno. */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 63 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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. */ |
| 67 | |
| 68 | if (suffixes) { |
| 69 | while (suffixes->suffix) { |
| 70 | if (strcmp(suffixes->suffix, e) == 0) { |
| 71 | if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */ |
| 72 | bb_show_usage(); |
| 73 | } |
| 74 | ++e; |
| 75 | r *= suffixes->mult; |
| 76 | break; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 77 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | ++suffixes; |
| 79 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 80 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | |
| 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)) { |
| 86 | bb_show_usage(); |
| 87 | } |
| 88 | |
| 89 | return r; |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | #ifdef L_xgetlarg_bnd_sfx |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | long bb_xgetlarg_bnd_sfx(const char *arg, int base, |
| 95 | long lower, |
| 96 | long upper, |
| 97 | const struct suffix_mult *suffixes) |
| 98 | { |
| 99 | unsigned long u = LONG_MAX; |
| 100 | long r; |
| 101 | const char *p = arg; |
| 102 | |
| 103 | if ((*p == '-') && (p[1] != '+')) { |
| 104 | ++p; |
| 105 | #if LONG_MAX == (-(LONG_MIN + 1)) |
| 106 | ++u; /* two's complement */ |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes); |
| 111 | |
| 112 | if (*arg == '-') { |
| 113 | r = -r; |
| 114 | } |
| 115 | |
| 116 | if ((r < lower) || (r > upper)) { |
| 117 | bb_show_usage(); |
| 118 | } |
| 119 | |
| 120 | return r; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | #ifdef L_getlarg10_sfx |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 125 | long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes) |
| 126 | { |
| 127 | return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes); |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | #ifdef L_xgetularg_bnd |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | unsigned long bb_xgetularg_bnd(const char *arg, int base, |
| 133 | unsigned long lower, |
| 134 | unsigned long upper) |
| 135 | { |
| 136 | return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL); |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #ifdef L_xgetularg10_bnd |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 141 | unsigned long bb_xgetularg10_bnd(const char *arg, |
| 142 | unsigned long lower, |
| 143 | unsigned long upper) |
| 144 | { |
| 145 | return bb_xgetularg_bnd(arg, 10, lower, upper); |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | #ifdef L_xgetularg10 |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | unsigned long bb_xgetularg10(const char *arg) |
| 151 | { |
| 152 | return bb_xgetularg10_bnd(arg, 0, ULONG_MAX); |
| 153 | } |
| 154 | #endif |