blob: ed5d3eb4f52e33ae9799bdd743e1e14bc0fe24d6 [file] [log] [blame]
Eric Andersena9c6bc42003-03-07 17:29:12 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersencb81e642003-07-14 21:21:08 +00003 * Copyright (C) 2003 Erik Andersen <andersen@codepoet.org>
Eric Andersena9c6bc42003-03-07 17:29:12 +00004 */
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 cad53642003-03-19 09:13:01 +000012#include <ctype.h>
Eric Andersena9c6bc42003-03-07 17:29:12 +000013
14#include "busybox.h"
15
Eric Andersena3bb3e62003-06-26 09:05:32 +000016extern long bb_xgetlarg(const char *arg, int base, long lower, long upper)
Eric Andersena9c6bc42003-03-07 17:29:12 +000017{
18 long result;
19 char *endptr;
20 int errno_save = errno;
21
22 assert(arg!=NULL);
Manuel Novoa III cad53642003-03-19 09:13:01 +000023
24 /* Don't allow leading whitespace. */
25 if ((isspace)(*arg)) { /* Use an actual funciton call for minimal size. */
26 bb_show_usage();
27 }
28
Eric Andersena9c6bc42003-03-07 17:29:12 +000029 errno = 0;
30 result = strtol(arg, &endptr, base);
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper)
32 bb_show_usage();
Eric Andersena9c6bc42003-03-07 17:29:12 +000033 errno = errno_save;
34 return result;
35}