diff options
author | Chris Lattner <sabre@nondot.org> | 2004-02-19 21:44:41 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2004-02-19 21:44:41 +0000 |
commit | a8032090aa4e7748d573af0769ac912d47ba1eaa (patch) | |
tree | 8af5e9aa5d073e918a52364411c66ad8ae00dce0 /runtime/GCCLibraries/libc/string.c | |
parent | bbdfe40ba747de4c642aaf9e91270482311a426f (diff) |
Add strndup
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11638 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'runtime/GCCLibraries/libc/string.c')
-rw-r--r-- | runtime/GCCLibraries/libc/string.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/runtime/GCCLibraries/libc/string.c b/runtime/GCCLibraries/libc/string.c index 0ed4ced64c..c13b11266c 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 @@ size_t strlen(const char *Str) { } 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++)); |