Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * bb_xparse_number implementation for busybox |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <limits.h> |
| 13 | #include <errno.h> |
| 14 | #include <assert.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
| 16 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | unsigned long bb_xparse_number(const char *numstr, |
| 18 | const struct suffix_mult *suffixes) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 19 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | unsigned long int r; |
| 21 | char *e; |
| 22 | int old_errno; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 23 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 24 | /* Since this is a lib function, we're not allowed to reset errno to 0. |
| 25 | * Doing so could break an app that is deferring checking of errno. |
| 26 | * So, save the old value so that we can restore it if successful. */ |
| 27 | old_errno = errno; |
| 28 | errno = 0; |
| 29 | r = strtoul(numstr, &e, 10); |
| 30 | |
| 31 | if ((numstr != e) && !errno) { |
| 32 | errno = old_errno; /* Ok. So restore errno. */ |
| 33 | if (!*e) { |
| 34 | return r; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 35 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | if (suffixes) { |
| 37 | assert(suffixes->suffix); /* No nul suffixes. */ |
| 38 | do { |
| 39 | if (strcmp(suffixes->suffix, e) == 0) { |
| 40 | if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */ |
| 41 | break; |
| 42 | } |
| 43 | return r * suffixes->mult; |
| 44 | } |
| 45 | ++suffixes; |
| 46 | } while (suffixes->suffix); |
| 47 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 48 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_error_msg_and_die("invalid number `%s'", numstr); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | } |