From c06ffb6998e5512d6589ef3ec4cc748371a07c77 Mon Sep 17 00:00:00 2001 From: rcorreia Date: Fri, 13 Mar 2009 08:17:27 +0000 Subject: [PATCH] Branch HEAD i=shadow Add platform-independent convenient libcfs_strdup() function. --- libcfs/include/libcfs/libcfs_string.h | 5 ++++- libcfs/libcfs/libcfs_string.c | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/libcfs/include/libcfs/libcfs_string.h b/libcfs/include/libcfs/libcfs_string.h index 72a5ef9..b81ce36 100644 --- a/libcfs/include/libcfs/libcfs_string.h +++ b/libcfs/include/libcfs/libcfs_string.h @@ -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 diff --git a/libcfs/libcfs/libcfs_string.c b/libcfs/libcfs/libcfs_string.c index 40e5ebb..ff3d268 100644 --- a/libcfs/libcfs/libcfs_string.c +++ b/libcfs/libcfs/libcfs_string.c @@ -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); -- 1.8.3.1