1 /* vi: set sw=4 ts=4: */
3 * bb_xparse_number implementation 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.
17 unsigned long bb_xparse_number(const char *numstr,
18 const struct suffix_mult *suffixes)
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. */
29 r = strtoul(numstr, &e, 10);
31 if ((numstr != e) && !errno) {
32 errno = old_errno; /* Ok. So restore errno. */
37 assert(suffixes->suffix); /* No nul suffixes. */
39 if (strcmp(suffixes->suffix, e) == 0) {
40 if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
43 return r * suffixes->mult;
46 } while (suffixes->suffix);
49 bb_error_msg_and_die("invalid number `%s'", numstr);