blob: 06e776dc9bbb993eb472c700a35b940346f9a3b2 [file] [log] [blame]
Eric Andersena9c6bc42003-03-07 17:29:12 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2003 Erik Andersen <andersee@debian.org>
4 */
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
16extern long bb_xgetlarg(char *arg, int base, long lower, long upper)
17{
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}