diff options
author | Uwe Kleine-König <u.kleine-koenig@pengutronix.de> | 2017-12-20 11:36:44 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2018-01-05 15:49:12 +0100 |
commit | 48e7b8ae0de2e3a7ec08286e764c46768da9b2bf (patch) | |
tree | d9f1e9f48902ee9a3f8ab865fac6ef71a7fd3954 /lib | |
parent | 4797aa72b8e4f7696627f2459a0732d1ca51b37d (diff) | |
download | barebox-48e7b8ae0de2e3a7ec08286e764c46768da9b2bf.tar.gz barebox-48e7b8ae0de2e3a7ec08286e764c46768da9b2bf.tar.xz |
string.h: Implement strndup
I implemented this while trying to import mmc-utils into barebox. While
I didn't finish this import this function might still be useful for
someone?!
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/string.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c index 1d491c9c2f..f588933e81 100644 --- a/lib/string.c +++ b/lib/string.c @@ -323,6 +323,26 @@ char * strdup(const char *s) #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 |