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> |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <getopt.h> |
| 12 | #include <errno.h> |
| 13 | #include <assert.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 14 | #include <ctype.h> |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 15 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 17 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 18 | long bb_xgetlarg(const char *arg, int base, long lower, long upper) |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 19 | { |
| 20 | long result; |
| 21 | char *endptr; |
| 22 | int errno_save = errno; |
| 23 | |
| 24 | assert(arg!=NULL); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | |
Mike Frysinger | 6ebbc74 | 2005-08-16 05:39:07 +0000 | [diff] [blame] | 26 | /* Don't allow leading whitespace. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 27 | * Wrap isspace in () to make sure we call the |
Mike Frysinger | 6ebbc74 | 2005-08-16 05:39:07 +0000 | [diff] [blame] | 28 | * function rather than the macro. */ |
| 29 | if ((isspace)(*arg)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | bb_show_usage(); |
| 31 | } |
| 32 | |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 33 | errno = 0; |
| 34 | result = strtol(arg, &endptr, base); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper) |
| 36 | bb_show_usage(); |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 37 | errno = errno_save; |
| 38 | return result; |
| 39 | } |