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 | |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <getopt.h> |
| 11 | #include <errno.h> |
| 12 | #include <assert.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | #include <ctype.h> |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 14 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 16 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 17 | long bb_xgetlarg(const char *arg, int base, long lower, long upper) |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 18 | { |
| 19 | long result; |
| 20 | char *endptr; |
| 21 | int errno_save = errno; |
| 22 | |
Rob Landley | 1f9145c | 2006-07-12 21:31:16 +0000 | [diff] [blame] | 23 | if (ENABLE_DEBUG && arg==NULL) |
| 24 | bb_error_msg_and_die("Null in xgetlarg."); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 26 | errno = 0; |
| 27 | result = strtol(arg, &endptr, base); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper) |
| 29 | bb_show_usage(); |
Eric Andersen | a9c6bc4 | 2003-03-07 17:29:12 +0000 | [diff] [blame] | 30 | errno = errno_save; |
| 31 | return result; |
| 32 | } |