[U-Boot] [PATCH] lib/string: added strndup

Grant Erickson marathon96 at gmail.com
Thu Dec 22 19:16:56 CET 2011


This patch adds optional support for strndup.

Signed-off-by: Grant Erickson <marathon96 at gmail.com>
---
 include/linux/string.h |    3 +++
 lib/string.c           |   21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index 6239039..5668290 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -59,6 +59,9 @@ extern __kernel_size_t strnlen(const char *,__kernel_size_t);
 #ifndef __HAVE_ARCH_STRDUP
 extern char * strdup(const char *);
 #endif
+#ifndef __HAVE_ARCH_STRNDUP
+extern char * strndup(const char *,__kernel_size_t);
+#endif
 #ifndef __HAVE_ARCH_STRSWAB
 extern char * strswab(const char *);
 #endif
diff --git a/lib/string.c b/lib/string.c
index 2c4f0ec..5fb6693 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -260,6 +260,27 @@ char * strdup(const char *s)
 }
 #endif
 
+#ifndef __HAVE_ARCH_STRNDUP
+char * strndup(const char *s, size_t n)
+{
+	size_t len;
+	char *new;
+
+	len = strnlen(s, n);
+
+	if ((s == NULL) ||
+		((new = malloc (len + 1)) == NULL) ) {
+		return NULL;
+	}
+
+	strncpy (new, s, len);
+
+	new[len] = '\0';
+
+	return new;
+}
+#endif
+
 #ifndef __HAVE_ARCH_STRSPN
 /**
  * strspn - Calculate the length of the initial substring of @s which only
-- 
1.7.7.3



More information about the U-Boot mailing list