Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ascii-to-numbers implementations for busybox |
| 4 | * |
| 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
| 10 | #include "libbb.h" |
| 11 | |
| 12 | #define type long long |
| 13 | #define xstrtou(rest) xstrtoull##rest |
| 14 | #define xstrto(rest) xstrtoll##rest |
| 15 | #define xatou(rest) xatoull##rest |
| 16 | #define xato(rest) xatoll##rest |
| 17 | #define XSTR_UTYPE_MAX ULLONG_MAX |
| 18 | #define XSTR_TYPE_MAX LLONG_MAX |
| 19 | #define XSTR_TYPE_MIN LLONG_MIN |
| 20 | #define XSTR_STRTOU strtoull |
| 21 | #include "xatonum_template.c" |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 22 | |
| 23 | #if ULONG_MAX != ULLONG_MAX |
| 24 | #define type long |
| 25 | #define xstrtou(rest) xstrtoul##rest |
| 26 | #define xstrto(rest) xstrtol##rest |
| 27 | #define xatou(rest) xatoul##rest |
| 28 | #define xato(rest) xatol##rest |
| 29 | #define XSTR_UTYPE_MAX ULONG_MAX |
| 30 | #define XSTR_TYPE_MAX LONG_MAX |
| 31 | #define XSTR_TYPE_MIN LONG_MIN |
| 32 | #define XSTR_STRTOU strtoul |
| 33 | #include "xatonum_template.c" |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #if UINT_MAX != ULONG_MAX |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 37 | extern inline unsigned bb_strtoui(const char *str, char **end, int b) |
| 38 | { |
| 39 | unsigned long v = strtoul(str, end, b); |
| 40 | if (v > UINT_MAX) { |
| 41 | errno = ERANGE; |
| 42 | return UINT_MAX; |
| 43 | } |
| 44 | return v; |
| 45 | } |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 46 | #define type int |
| 47 | #define xstrtou(rest) xstrtou##rest |
| 48 | #define xstrto(rest) xstrtoi##rest |
| 49 | #define xatou(rest) xatou##rest |
| 50 | #define xato(rest) xatoi##rest |
| 51 | #define XSTR_UTYPE_MAX UINT_MAX |
| 52 | #define XSTR_TYPE_MAX INT_MAX |
| 53 | #define XSTR_TYPE_MIN INT_MIN |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 54 | /* libc has no strtoui, so we need to create/use our own */ |
| 55 | #define XSTR_STRTOU bb_strtoui |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 56 | #include "xatonum_template.c" |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | /* A few special cases */ |
| 60 | |
| 61 | int xatoi_u(const char *numstr) |
| 62 | { |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 63 | return xatou_range(numstr, 0, INT_MAX); |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 66 | uint16_t xatou16(const char *numstr) |
| 67 | { |
Denis Vlasenko | 43bddf3 | 2006-11-25 14:49:04 +0000 | [diff] [blame] | 68 | return xatou_range(numstr, 0, 0xffff); |
Denis Vlasenko | ed836cd | 2006-11-25 14:44:13 +0000 | [diff] [blame] | 69 | } |