]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - lib/string.c
vsprintf: Update %pI6c to not compress a single 0
[linux-2.6.git] / lib / string.c
index 765566a6a0887b3054670b6b59090d4edb3430c3..01fad9b203e192eb8a3299d669c29e11e4cf2e04 100644 (file)
@@ -36,25 +36,21 @@ int strnicmp(const char *s1, const char *s2, size_t len)
        /* Yes, Virginia, it had better be unsigned */
        unsigned char c1, c2;
 
-       c1 = c2 = 0;
-       if (len) {
-               do {
-                       c1 = *s1;
-                       c2 = *s2;
-                       s1++;
-                       s2++;
-                       if (!c1)
-                               break;
-                       if (!c2)
-                               break;
-                       if (c1 == c2)
-                               continue;
-                       c1 = tolower(c1);
-                       c2 = tolower(c2);
-                       if (c1 != c2)
-                               break;
-               } while (--len);
-       }
+       if (!len)
+               return 0;
+
+       do {
+               c1 = *s1++;
+               c2 = *s2++;
+               if (!c1 || !c2)
+                       break;
+               if (c1 == c2)
+                       continue;
+               c1 = tolower(c1);
+               c2 = tolower(c2);
+               if (c1 != c2)
+                       break;
+       } while (--len);
        return (int)c1 - (int)c2;
 }
 EXPORT_SYMBOL(strnicmp);
@@ -338,10 +334,10 @@ EXPORT_SYMBOL(strnchr);
 #endif
 
 /**
- * skip_spaces - Removes leading whitespace from @s.
- * @s: The string to be stripped.
+ * skip_spaces - Removes leading whitespace from @str.
+ * @str: The string to be stripped.
  *
- * Returns a pointer to the first non-whitespace character in @s.
+ * Returns a pointer to the first non-whitespace character in @str.
  */
 char *skip_spaces(const char *str)
 {
@@ -352,14 +348,14 @@ char *skip_spaces(const char *str)
 EXPORT_SYMBOL(skip_spaces);
 
 /**
- * strstrip - Removes leading and trailing whitespace from @s.
+ * strim - Removes leading and trailing whitespace from @s.
  * @s: The string to be stripped.
  *
  * Note that the first trailing whitespace is replaced with a %NUL-terminator
  * in the given string @s. Returns a pointer to the first non-whitespace
  * character in @s.
  */
-char *strstrip(char *s)
+char *strim(char *s)
 {
        size_t size;
        char *end;
@@ -376,7 +372,7 @@ char *strstrip(char *s)
 
        return s;
 }
-EXPORT_SYMBOL(strstrip);
+EXPORT_SYMBOL(strim);
 
 #ifndef __HAVE_ARCH_STRLEN
 /**
@@ -539,6 +535,35 @@ bool sysfs_streq(const char *s1, const char *s2)
 }
 EXPORT_SYMBOL(sysfs_streq);
 
+/**
+ * strtobool - convert common user inputs into boolean values
+ * @s: input string
+ * @res: result
+ *
+ * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
+ * Otherwise it will return -EINVAL.  Value pointed to by res is
+ * updated upon finding a match.
+ */
+int strtobool(const char *s, bool *res)
+{
+       switch (s[0]) {
+       case 'y':
+       case 'Y':
+       case '1':
+               *res = true;
+               break;
+       case 'n':
+       case 'N':
+       case '0':
+               *res = false;
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+EXPORT_SYMBOL(strtobool);
+
 #ifndef __HAVE_ARCH_MEMSET
 /**
  * memset - Fill a region of memory with the given value
@@ -667,7 +692,7 @@ EXPORT_SYMBOL(memscan);
  */
 char *strstr(const char *s1, const char *s2)
 {
-       int l1, l2;
+       size_t l1, l2;
 
        l2 = strlen(s2);
        if (!l2)
@@ -684,6 +709,31 @@ char *strstr(const char *s1, const char *s2)
 EXPORT_SYMBOL(strstr);
 #endif
 
+#ifndef __HAVE_ARCH_STRNSTR
+/**
+ * strnstr - Find the first substring in a length-limited string
+ * @s1: The string to be searched
+ * @s2: The string to search for
+ * @len: the maximum number of characters to search
+ */
+char *strnstr(const char *s1, const char *s2, size_t len)
+{
+       size_t l2;
+
+       l2 = strlen(s2);
+       if (!l2)
+               return (char *)s1;
+       while (len >= l2) {
+               len--;
+               if (!memcmp(s1, s2, l2))
+                       return (char *)s1;
+               s1++;
+       }
+       return NULL;
+}
+EXPORT_SYMBOL(strnstr);
+#endif
+
 #ifndef __HAVE_ARCH_MEMCHR
 /**
  * memchr - Find a character in an area of memory.