The world continually reimplements kstrdup(). Implement an optimal version and export it to the world. By: Robert Love and Rusty Russell. Signed-off-by: Robert Love include/linux/string.h | 1 + lib/string.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff -urN linux-2.6.12-rc3/include/linux/string.h linux/include/linux/string.h --- linux-2.6.12-rc3/include/linux/string.h 2005-03-02 02:38:07.000000000 -0500 +++ linux/include/linux/string.h 2005-04-21 21:23:04.000000000 -0400 @@ -17,6 +17,7 @@ extern char * strsep(char **,const char *); extern __kernel_size_t strspn(const char *,const char *); extern __kernel_size_t strcspn(const char *,const char *); +extern char * kstrdup(const char *,unsigned int __nocast); /* * Include machine specific inline routines diff -urN linux-2.6.12-rc3/lib/string.c linux/lib/string.c --- linux-2.6.12-rc3/lib/string.c 2005-03-02 02:38:25.000000000 -0500 +++ linux/lib/string.c 2005-04-21 21:31:11.000000000 -0400 @@ -22,6 +22,7 @@ #include #include #include +#include #include #ifndef __HAVE_ARCH_STRNICMP @@ -76,6 +77,25 @@ EXPORT_SYMBOL(strcpy); #endif +/* + * kstrdup - allocate space for and then copy an existing string + * + * @str: the string to duplicate + * @gfp: the GFP mask used to allocate the storage for the duplicated string + */ +char * kstrdup(const char *str, unsigned int __nocast flags) +{ + size_t len; + char *buf; + + len = strlen(str) + 1; + buf = kmalloc(len, flags); + if (likely(buf)) + memcpy(buf, str, len); + return buf; +} +EXPORT_SYMBOL(kstrdup); + #ifndef __HAVE_ARCH_STRNCPY /** * strncpy - Copy a length-limited, %NUL-terminated string