From 5088c53a88cc4853d822e710dee8fad2b6fd29af Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Mon, 11 May 2009 10:15:12 +0000 Subject: [PATCH] Add strict_strtoX() compatibilty functions for kernel < 2.6.25. Reported by labrat . --- include/linux/kernel.h | 97 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 46f78af1..693a71c1 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -31,4 +31,101 @@ #endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) +/* Add missing strict_strtox functions. */ +#include +#include + +static inline int comedi_strict_strtoul(const char *cp, unsigned int base, + unsigned long *res) +{ + char *tail; + unsigned long val; + size_t len; + + *res = 0; + len = strlen(cp); + if (len == 0) + return -EINVAL; + + val = simple_strtoul(cp, &tail, base); + if (tail == cp) + return -EINVAL; + if ((*tail == '\0') || + ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { + *res = val; + return 0; + } + + return -EINVAL; +} + +#undef strict_strtoul +#define strict_strtoul(cp, base, res) comedi_strict_strtoul(cp, base, res) + +static inline int comedi_strict_strtol(const char *cp, unsigned int base, + long *res) +{ + int ret; + if (*cp == '-') { + ret = comedi_strict_strtoul(cp + 1, base, (unsigned long *)res); + if (!ret) + *res = -(*res); + } else { + ret = comedi_strict_strtoul(cp, base, (unsigned long *)res); + } + + return ret; +} + +#undef strict_strtol +#define strict_strtol(cp, base, res) comedi_strict_strtoul(cp, base, res) + +static inline int comedi_strict_strtoull(const char *cp, unsigned int base, + unsigned long long *res) +{ + char *tail; + unsigned long long val; + size_t len; + + *res = 0; + len = strlen(cp); + if (len == 0) + return -EINVAL; + + val = simple_strtoull(cp, &tail, base); + if (tail == cp) + return -EINVAL; + if ((*tail == '\0') || + ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { + *res = val; + return 0; + } + + return -EINVAL; +} + +#undef strict_strtoull +#define strict_strtoull(cp, base, res) comedi_strict_strtoul(cp, base, res) + +static inline int comedi_strict_strtoll(const char *cp, unsigned int base, + long long *res) +{ + int ret; + if (*cp == '-') { + ret = comedi_strict_strtoull(cp + 1, base, (unsigned long long *)res); + if (!ret) + *res = -(*res); + } else { + ret = comedi_strict_strtoull(cp, base, (unsigned long long *)res); + } + + return ret; +} + +#undef strict_strtoll +#define strict_strtoll(cp, base, res) comedi_strict_strtoul(cp, base, res) + +#endif + #endif // _KERNEL_COMPAT_H -- 2.26.2