blob: 6d3c4d1db41ecd173e28a41ca0e73ba8ae68b520 [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
Rob Landleydfba7412006-03-06 20:47:33 +000016long 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.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000025 * Wrap isspace in () to make sure we call the
Mike Frysinger6ebbc742005-08-16 05:39:07 +000026 * 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}