Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2004 Erik Andersen <andersen@codepoet.org> |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <getopt.h> |
| 10 | #include <errno.h> |
| 11 | #include <assert.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 12 | #include <ctype.h> |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 13 | |
| 14 | #include "busybox.h" |
| 15 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 16 | long bb_xgetlarg(const char *arg, int base, long lower, long upper) |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 17 | { |
| 18 | long result; |
| 19 | char *endptr; |
| 20 | int errno_save = errno; |
| 21 | |
| 22 | assert(arg!=NULL); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | |
Mike Frysinger | 6ebbc74 | 2005-08-16 05:39:07 +0000 | [diff] [blame] | 24 | /* Don't allow leading whitespace. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 25 | * Wrap isspace in () to make sure we call the |
Mike Frysinger | 6ebbc74 | 2005-08-16 05:39:07 +0000 | [diff] [blame] | 26 | * function rather than the macro. */ |
| 27 | if ((isspace)(*arg)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | bb_show_usage(); |
| 29 | } |
| 30 | |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 31 | errno = 0; |
| 32 | result = strtol(arg, &endptr, base); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper) |
| 34 | bb_show_usage(); |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 35 | errno = errno_save; |
| 36 | return result; |
| 37 | } |