diff --git a/include/linux/string.h b/include/linux/string.h index 5df8c50..ed4eeb5 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -80,6 +80,9 @@ #ifndef __HAVE_ARCH_STRDUP extern char * strdup(const char *); #endif +#ifndef __HAVE_ARCH_STRNDUP +extern char *strndup(const char *, size_t); +#endif #ifndef __HAVE_ARCH_STRSWAB extern char * strswab(const char *); #endif diff --git a/lib/string.c b/lib/string.c index 1d491c9..f588933 100644 --- a/lib/string.c +++ b/lib/string.c @@ -323,6 +323,26 @@ #endif EXPORT_SYMBOL(strdup); +#ifndef __HAVE_ARCH_STRNDUP +char *strndup(const char *s, size_t n) +{ + char *new; + size_t len = strnlen(s, n); + + if ((s == NULL) || + ((new = malloc(len + 1)) == NULL)) { + return NULL; + } + + memcpy(new, s, len); + new[len] = '\0'; + + return new; +} + +#endif +EXPORT_SYMBOL(strndup); + #ifndef __HAVE_ARCH_STRSPN /** * strspn - Calculate the length of the initial substring of @s which only