blob: 893cd28133e9dd55b10e9676b2bf62e5f0664bd7 [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
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <getopt.h>
12#include <errno.h>
13#include <assert.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000014#include <ctype.h>
Eric Andersena9c6bc42003-03-07 17:29:12 +000015
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000016#include "libbb.h"
Eric Andersena9c6bc42003-03-07 17:29:12 +000017
Rob Landleydfba7412006-03-06 20:47:33 +000018long bb_xgetlarg(const char *arg, int base, long lower, long upper)
Eric Andersena9c6bc42003-03-07 17:29:12 +000019{
20 long result;
21 char *endptr;
22 int errno_save = errno;
23
24 assert(arg!=NULL);
Manuel Novoa III cad53642003-03-19 09:13:01 +000025
Mike Frysinger6ebbc742005-08-16 05:39:07 +000026 /* Don't allow leading whitespace.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000027 * Wrap isspace in () to make sure we call the
Mike Frysinger6ebbc742005-08-16 05:39:07 +000028 * function rather than the macro. */
29 if ((isspace)(*arg)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 bb_show_usage();
31 }
32
Eric Andersena9c6bc42003-03-07 17:29:12 +000033 errno = 0;
34 result = strtol(arg, &endptr, base);
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper)
36 bb_show_usage();
Eric Andersena9c6bc42003-03-07 17:29:12 +000037 errno = errno_save;
38 return result;
39}