hush/libbb/bb.strtonum.c

120 lines
3.0 KiB
C
Raw Normal View History

2006-11-27 15:12:16 +00:00
/* vi: set sw=4 ts=4: */
/*
* Utility routines.
*
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
2006-11-27 15:12:16 +00:00
*/
#include "libbb.h"
/* On exit: errno = 0 only if there was non-empty, '\0' terminated value
2008-07-23 08:41:08 +00:00
* errno = EINVAL if value was not '\0' terminated, but otherwise ok
2006-11-27 15:12:16 +00:00
* Return value is still valid, caller should just check whether end[0]
* is a valid terminating char for particular case. OTOH, if caller
* requires '\0' terminated input, [s]he can just check errno == 0.
* errno = ERANGE if value had alphanumeric terminating char ("1234abcg").
* errno = ERANGE if value is out of range, missing, etc.
* errno = ERANGE if value had minus sign for strtouXX (even "-0" is not ok )
2006-12-26 18:17:42 +00:00
* return value is all-ones in this case.
*
* Test code:
* char *endptr;
* const char *minus = "-";
* errno = 0;
* bb_strtoi(minus, &endptr, 0); // must set ERANGE
* printf("minus:%p endptr:%p errno:%d EINVAL:%d\n", minus, endptr, errno, EINVAL);
* errno = 0;
* bb_strtoi("-0-", &endptr, 0); // must set EINVAL and point to second '-'
* printf("endptr[0]:%c errno:%d EINVAL:%d\n", endptr[0], errno, EINVAL);
2006-11-27 15:12:16 +00:00
*/
static uintmax_t ret_ERANGE(void)
2006-11-27 15:12:16 +00:00
{
errno = ERANGE; /* this ain't as small as it looks (on glibc) */
return UINTMAX_MAX;
2006-11-27 15:12:16 +00:00
}
static uintmax_t handle_errors(uintmax_t v, char **endp)
2006-11-27 15:12:16 +00:00
{
char next_ch = **endp;
2006-11-27 15:12:16 +00:00
/* errno is already set to ERANGE by strtoXXX if value overflowed */
if (next_ch) {
2006-11-27 15:12:16 +00:00
/* "1234abcg" or out-of-range? */
if (isalnum(next_ch) || errno)
2006-11-27 15:12:16 +00:00
return ret_ERANGE();
/* good number, just suspicious terminator */
errno = EINVAL;
}
return v;
}
unsigned long FAST_FUNC bb_strtoul(const char *arg, char **endp, int base)
2006-11-27 15:12:16 +00:00
{
2006-11-28 10:54:16 +00:00
unsigned long v;
2006-11-27 15:12:16 +00:00
char *endptr;
if (!endp) endp = &endptr;
*endp = (char*) arg;
2006-11-27 15:12:16 +00:00
if (!isalnum(arg[0])) return ret_ERANGE();
errno = 0;
v = strtoul(arg, endp, base);
return handle_errors(v, endp);
2006-11-27 15:12:16 +00:00
}
long FAST_FUNC bb_strtol(const char *arg, char **endp, int base)
2006-11-27 15:12:16 +00:00
{
2006-11-28 10:54:16 +00:00
long v;
2006-11-27 15:12:16 +00:00
char *endptr;
char first;
2006-11-27 15:12:16 +00:00
if (!endp) endp = &endptr;
*endp = (char*) arg;
first = (arg[0] != '-' ? arg[0] : arg[1]);
if (!isalnum(first)) return ret_ERANGE();
2006-11-27 15:12:16 +00:00
errno = 0;
v = strtol(arg, endp, base);
return handle_errors(v, endp);
2006-11-27 15:12:16 +00:00
}
#if UINT_MAX != ULONG_MAX
unsigned FAST_FUNC bb_strtou(const char *arg, char **endp, int base)
2006-11-27 15:12:16 +00:00
{
unsigned long v;
char *endptr;
if (!endp) endp = &endptr;
*endp = (char*) arg;
2006-11-27 15:12:16 +00:00
if (!isalnum(arg[0])) return ret_ERANGE();
errno = 0;
v = strtoul(arg, endp, base);
2006-11-27 15:12:16 +00:00
if (v > UINT_MAX) return ret_ERANGE();
return handle_errors(v, endp);
2006-11-27 15:12:16 +00:00
}
int FAST_FUNC bb_strtoi(const char *arg, char **endp, int base)
2006-11-27 15:12:16 +00:00
{
long v;
char *endptr;
char first;
if (!endp) endp = &endptr;
*endp = (char*) arg;
2006-11-27 15:12:16 +00:00
first = (arg[0] != '-' ? arg[0] : arg[1]);
if (!isalnum(first)) return ret_ERANGE();
2006-11-27 15:12:16 +00:00
errno = 0;
v = strtol(arg, endp, base);
2006-11-27 15:12:16 +00:00
if (v > INT_MAX) return ret_ERANGE();
if (v < INT_MIN) return ret_ERANGE();
return handle_errors(v, endp);
2006-11-27 15:12:16 +00:00
}
#endif