2006-11-25 14:44:13 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* ascii-to-numbers implementations for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2006-11-25 14:44:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
#define type long
|
2014-11-04 02:17:51 +00:00
|
|
|
#define xstrtou xstrtoul
|
|
|
|
#define xstrto xstrtol
|
|
|
|
#define xatou xatoul
|
|
|
|
#define xato xatol
|
|
|
|
#define xstrtou_(rest) xstrtoul_##rest
|
|
|
|
#define xstrto_(rest) xstrtol_##rest
|
|
|
|
#define xatou_(rest) xatoul_##rest
|
|
|
|
#define xato_(rest) xatol_##rest
|
2006-11-25 14:44:13 +00:00
|
|
|
#define XSTR_UTYPE_MAX ULONG_MAX
|
|
|
|
#define XSTR_TYPE_MAX LONG_MAX
|
|
|
|
#define XSTR_TYPE_MIN LONG_MIN
|
|
|
|
#define XSTR_STRTOU strtoul
|
2014-11-03 04:11:07 +00:00
|
|
|
#include "xatonum.tmplt.c"
|
2006-11-25 14:44:13 +00:00
|
|
|
|
|
|
|
#if UINT_MAX != ULONG_MAX
|
2007-06-12 20:54:54 +00:00
|
|
|
static ALWAYS_INLINE
|
2006-12-16 23:48:13 +00:00
|
|
|
unsigned bb_strtoui(const char *str, char **end, int b)
|
2006-11-25 14:49:04 +00:00
|
|
|
{
|
|
|
|
unsigned long v = strtoul(str, end, b);
|
|
|
|
if (v > UINT_MAX) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
2006-11-25 14:44:13 +00:00
|
|
|
#define type int
|
2014-11-04 02:17:51 +00:00
|
|
|
#define xstrtou xstrtou
|
|
|
|
#define xstrto xstrtoi
|
|
|
|
#define xatou xatou
|
|
|
|
#define xato xatoi
|
|
|
|
#define xstrtou_(rest) xstrtou_##rest
|
|
|
|
#define xstrto_(rest) xstrtoi_##rest
|
|
|
|
#define xatou_(rest) xatou_##rest
|
|
|
|
#define xato_(rest) xatoi_##rest
|
2006-11-25 14:44:13 +00:00
|
|
|
#define XSTR_UTYPE_MAX UINT_MAX
|
|
|
|
#define XSTR_TYPE_MAX INT_MAX
|
|
|
|
#define XSTR_TYPE_MIN INT_MIN
|
2006-11-25 14:49:04 +00:00
|
|
|
/* libc has no strtoui, so we need to create/use our own */
|
|
|
|
#define XSTR_STRTOU bb_strtoui
|
2014-11-03 04:11:07 +00:00
|
|
|
#include "xatonum.tmplt.c"
|
2006-11-25 14:44:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* A few special cases */
|
|
|
|
|
2010-08-12 12:14:45 +00:00
|
|
|
int FAST_FUNC xatoi_positive(const char *numstr)
|
2006-11-25 14:44:13 +00:00
|
|
|
{
|
2006-11-25 14:49:04 +00:00
|
|
|
return xatou_range(numstr, 0, INT_MAX);
|
2006-11-25 14:44:13 +00:00
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
uint16_t FAST_FUNC xatou16(const char *numstr)
|
2006-11-25 14:44:13 +00:00
|
|
|
{
|
2006-11-25 14:49:04 +00:00
|
|
|
return xatou_range(numstr, 0, 0xffff);
|
2006-11-25 14:44:13 +00:00
|
|
|
}
|
2013-07-13 21:49:45 +00:00
|
|
|
|
|
|
|
const struct suffix_mult bkm_suffixes[] = {
|
|
|
|
{ "b", 512 },
|
|
|
|
{ "k", 1024 },
|
|
|
|
{ "m", 1024*1024 },
|
|
|
|
{ "", 0 }
|
|
|
|
};
|