aboutsummaryrefslogtreecommitdiff
path: root/libbb/xgetlarg.c
blob: cadb83e085abdf440f48b70e3bfeb94fedf3d5f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* vi: set sw=4 ts=4: */
/*
 * Copyright (C) 2003-2004 Erik Andersen <andersen@codepoet.org>
 *
 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>

#include "libbb.h"

long bb_xgetlarg(const char *arg, int base, long lower, long upper)
{
	long result;
	char *endptr;
	int errno_save = errno;

	assert(arg!=NULL);

	/* Don't allow leading whitespace.
	 * Wrap isspace in () to make sure we call the
	 * function rather than the macro. */
	if ((isspace)(*arg)) {
		bb_show_usage();
	}

	errno = 0;
	result = strtol(arg, &endptr, base);
	if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper)
		bb_show_usage();
	errno = errno_save;
	return result;
}