Add strndup

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11638 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2004-02-19 21:44:41 +00:00
parent bbdfe40ba7
commit a8032090aa

View File

@ -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++));