blob: 5b1e7b9d5890f2fdcbd2656d8ff2ad93b2c1dbaf [file] [log] [blame]
Eric Andersena9c6bc42003-03-07 17:29:12 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * Copyright (C) 2003-2004 Erik Andersen <andersen@codepoet.org>
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00004 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersena9c6bc42003-03-07 17:29:12 +00006 */
7
Eric Andersena9c6bc42003-03-07 17:29:12 +00008#include <stdio.h>
9#include <stdlib.h>
10#include <getopt.h>
11#include <errno.h>
12#include <assert.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000013#include <ctype.h>
Eric Andersena9c6bc42003-03-07 17:29:12 +000014
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000015#include "libbb.h"
Eric Andersena9c6bc42003-03-07 17:29:12 +000016
Rob Landleydfba7412006-03-06 20:47:33 +000017long bb_xgetlarg(const char *arg, int base, long lower, long upper)
Eric Andersena9c6bc42003-03-07 17:29:12 +000018{
19 long result;
20 char *endptr;
21 int errno_save = errno;
22
Rob Landley1f9145c2006-07-12 21:31:16 +000023 if (ENABLE_DEBUG && arg==NULL)
24 bb_error_msg_and_die("Null in xgetlarg.");
Manuel Novoa III cad53642003-03-19 09:13:01 +000025
Eric Andersena9c6bc42003-03-07 17:29:12 +000026 errno = 0;
27 result = strtol(arg, &endptr, base);
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper)
29 bb_show_usage();
Eric Andersena9c6bc42003-03-07 17:29:12 +000030 errno = errno_save;
31 return result;
32}