Add strndup


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11638 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/runtime/GCCLibraries/libc/string.c b/runtime/GCCLibraries/libc/string.c
index 0ed4ced..c13b112 100644
--- a/runtime/GCCLibraries/libc/string.c
+++ b/runtime/GCCLibraries/libc/string.c
@@ -6,8 +6,6 @@
 
 #include <stdlib.h>
 #include <string.h>
-void *malloc(size_t);
-void free(void *);
 
 size_t strlen(const char *Str) {
   size_t Count = 0;
@@ -16,12 +14,21 @@
 }
 
 char *strdup(const char *str) {
-  long Len = strlen(str);
+  size_t Len = strlen(str);
   char *Result = (char*)malloc((Len+1)*sizeof(char));
   memcpy(Result, str, Len+1);
   return Result;
 }
 
+char *strndup(const char *str, size_t n) {
+  size_t Len = strlen(str);
+  if (Len > n) Len = n;
+  char *Result = (char*)malloc((Len+1)*sizeof(char));
+  memcpy(Result, str, Len);
+  Result[Len] = 0;
+  return Result;
+}
+
 char *strcpy(char *s1, const char *s2) {
   char *dest = s1;
   while ((*s1++ = *s2++));