]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
lib, vsprintf: introduce strict_strtoul
authorHeiko Schocher <hs@denx.de>
Mon, 2 May 2011 21:33:49 +0000 (21:33 +0000)
committerWolfgang Denk <wd@denx.de>
Thu, 12 May 2011 19:07:06 +0000 (21:07 +0200)
as checkpatch proposes to use strict_strtoul instead of
simple_strtoul, introduce it.

Ported this function from Linux 2.6.38 commit ID:
521cb40b0c44418a4fd36dc633f575813d59a43d

Signed-off-by: Heiko Schocher <hs@denx.de>
cc: Wolfgang Denk <wd@denx.de>
cc: Detlev Zundel <dzu@denx.de>
cc: Valentin Longchamp <valentin.longchamp@keymile.com>
cc: Holger Brunck <holger.brunck@keymile.com>
Signed-off-by: Valentin Longchamp <valentin.longchamp@keymile.com>
include/_exports.h
include/common.h
include/exports.h
lib/vsprintf.c

index d89b65be661772ab84d0209a3a4b29f9efce58eb..349a3c5522ff876f5fba77731a53a56a15371e84 100644 (file)
@@ -19,6 +19,7 @@ EXPORT_FUNC(do_reset)
 EXPORT_FUNC(getenv)
 EXPORT_FUNC(setenv)
 EXPORT_FUNC(simple_strtoul)
+EXPORT_FUNC(strict_strtoul)
 EXPORT_FUNC(simple_strtol)
 EXPORT_FUNC(strcmp)
 EXPORT_FUNC(i2c_write)
index 00e266ed6fe23e84a5cc7bf24eab309ade40d765..1e4a6a5757fe3e46b12d50161ae1834989039a9d 100644 (file)
@@ -650,6 +650,7 @@ void        udelay        (unsigned long);
 
 /* lib/vsprintf.c */
 ulong  simple_strtoul(const char *cp,char **endp,unsigned int base);
+int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
 unsigned long long     simple_strtoull(const char *cp,char **endp,unsigned int base);
 long   simple_strtol(const char *cp,char **endp,unsigned int base);
 void   panic(const char *fmt, ...)
index ddd1bf494605d65f78224daa89684c56f62cf1c0..e14d727ed6dee4197985b3dc7c443deb47202da6 100644 (file)
@@ -20,6 +20,7 @@ void __udelay(unsigned long);
 unsigned long get_timer(unsigned long);
 int vprintf(const char *, va_list);
 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base);
+int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
 char *getenv (char *name);
 int setenv (char *varname, char *varvalue);
 long simple_strtol(const char *cp,char **endp,unsigned int base);
index 61e6f0d2b33b2932e0b2faeeee7ca810b17ec3a0..3b924ec5e0d608ee09dd67faa9ff118886a1fa90 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/types.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
+#include <errno.h>
 
 #include <common.h>
 #if !defined (CONFIG_PANIC_HANG)
@@ -61,6 +62,56 @@ unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
        return result;
 }
 
+/**
+ * strict_strtoul - convert a string to an unsigned long strictly
+ * @cp: The string to be converted
+ * @base: The number base to use
+ * @res: The converted result value
+ *
+ * strict_strtoul converts a string to an unsigned long only if the
+ * string is really an unsigned long string, any string containing
+ * any invalid char at the tail will be rejected and -EINVAL is returned,
+ * only a newline char at the tail is acceptible because people generally
+ * change a module parameter in the following way:
+ *
+ *      echo 1024 > /sys/module/e1000/parameters/copybreak
+ *
+ * echo will append a newline to the tail.
+ *
+ * It returns 0 if conversion is successful and *res is set to the converted
+ * value, otherwise it returns -EINVAL and *res is set to 0.
+ *
+ * simple_strtoul just ignores the successive invalid characters and
+ * return the converted value of prefix part of the string.
+ *
+ * Copied this function from Linux 2.6.38 commit ID:
+ * 521cb40b0c44418a4fd36dc633f575813d59a43d
+ *
+ */
+int 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;
+}
+
 long simple_strtol(const char *cp,char **endp,unsigned int base)
 {
        if(*cp=='-')