Whamcloud - gitweb
b=23820 Handle unsent requests with rq_net_err in ptlrpc_check_set()
[fs/lustre-release.git] / libcfs / libcfs / libcfs_string.c
index 40e5ebb..83ad82b 100644 (file)
@@ -26,7 +26,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
@@ -47,7 +47,7 @@
 #include <libcfs/libcfs.h>
 
 /* non-0 = don't match */
-static int libcfs_strncasecmp(const char *s1, const char *s2, size_t n)
+static int cfs_strncasecmp(const char *s1, const char *s2, size_t n)
 {
         if (s1 == NULL || s2 == NULL)
                 return 1;
@@ -66,9 +66,10 @@ static int libcfs_strncasecmp(const char *s1, const char *s2, size_t n)
 }
 
 /* Convert a text string to a bitmask */
-int libcfs_str2mask(const char *str, const char *(*bit2str)(int bit),
-                    int *oldmask, int minmask, int allmask)
+int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
+                 int *oldmask, int minmask, int allmask)
 {
+        const char *debugstr;
         char op = 0;
         int newmask = minmask, i, len, found = 0;
         ENTRY;
@@ -101,7 +102,10 @@ int libcfs_str2mask(const char *str, const char *(*bit2str)(int bit),
                 /* match token */
                 found = 0;
                 for (i = 0; i < 32; i++) {
-                        if (libcfs_strncasecmp(str, bit2str(i), len) == 0) {
+                        debugstr = bit2str(i);
+                        if (debugstr != NULL &&
+                            strlen(debugstr) == len &&
+                            cfs_strncasecmp(str, debugstr, len) == 0) {
                                 if (op == '-')
                                         newmask &= ~(1 << i);
                                 else
@@ -110,7 +114,8 @@ int libcfs_str2mask(const char *str, const char *(*bit2str)(int bit),
                                 break;
                         }
                 }
-                if (!found && (libcfs_strncasecmp(str, "ALL", len) == 0)) {
+                if (!found && len == 3 &&
+                    (cfs_strncasecmp(str, "ALL", len) == 0)) {
                         if (op == '-')
                                 newmask = minmask;
                         else
@@ -128,5 +133,52 @@ int libcfs_str2mask(const char *str, const char *(*bit2str)(int bit),
         *oldmask = newmask;
         return 0;
 }
-EXPORT_SYMBOL(libcfs_str2mask);
+EXPORT_SYMBOL(cfs_str2mask);
 
+/* Duplicate a string in a platform-independent way */
+char *cfs_strdup(const char *str, u_int32_t flags)
+{
+        size_t lenz; /* length of str + zero byte */
+        char *dup_str;
+
+        lenz = strlen(str) + 1;
+
+        dup_str = cfs_alloc(lenz, flags);
+        if (dup_str == NULL)
+                return NULL;
+
+        memcpy(dup_str, str, lenz);
+
+        return dup_str;
+}
+EXPORT_SYMBOL(cfs_strdup);
+
+/**
+ * cfs_{v}snprintf() return the actual size that is printed rather than
+ * the size that would be printed in standard functions.
+ */
+/* safe vsnprintf */
+int cfs_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
+{
+        int i;
+
+        LASSERT(size > 0);
+        i = vsnprintf(buf, size, fmt, args);
+
+        return  (i >= size ? size - 1 : i);
+}
+EXPORT_SYMBOL(cfs_vsnprintf);
+
+/* safe snprintf */
+int cfs_snprintf(char *buf, size_t size, const char *fmt, ...)
+{
+        va_list args;
+        int i;
+
+        va_start(args, fmt);
+        i = cfs_vsnprintf(buf, size, fmt, args);
+        va_end(args);
+
+        return  i;
+}
+EXPORT_SYMBOL(cfs_snprintf);