Whamcloud - gitweb
Branch HEAD
authorrcorreia <rcorreia>
Fri, 13 Mar 2009 08:17:27 +0000 (08:17 +0000)
committerrcorreia <rcorreia>
Fri, 13 Mar 2009 08:17:27 +0000 (08:17 +0000)
i=shadow

Add platform-independent convenient libcfs_strdup() function.

libcfs/include/libcfs/libcfs_string.h
libcfs/libcfs/libcfs_string.c

index 72a5ef9..b81ce36 100644 (file)
@@ -48,5 +48,8 @@
 int libcfs_str2mask(const char *str, const char *(*bit2str)(int bit),
                     int *oldmask, int minmask, int allmask);
 
-
+/* Allocate space for and copy an existing string.
+ * Must free with cfs_free().
+ */
+char *libcfs_strdup(const char *str, u_int32_t flags);
 #endif
index 40e5ebb..ff3d268 100644 (file)
@@ -128,5 +128,23 @@ int libcfs_str2mask(const char *str, const char *(*bit2str)(int bit),
         *oldmask = newmask;
         return 0;
 }
-EXPORT_SYMBOL(libcfs_str2mask);
 
+/* Duplicate a string in a platform-independent way */
+char *libcfs_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(libcfs_str2mask);
+EXPORT_SYMBOL(libcfs_strdup);