blob: f24492beef15267675911ac3c76732f133598104 [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>
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
Mike Frysinger6ebbc742005-08-16 05:39:07 +000024 /* Don't allow leading whitespace.
25 * Wrap isspace in () to make sure we call the
26 * function rather than the macro. */
27 if ((isspace)(*arg)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 bb_show_usage();
29 }
30
Eric Andersena9c6bc42003-03-07 17:29:12 +000031 errno = 0;
32 result = strtol(arg, &endptr, base);
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper)
34 bb_show_usage();
Eric Andersena9c6bc42003-03-07 17:29:12 +000035 errno = errno_save;
36 return result;
37}