* Added POSIX function strndup(), closing ticket #3309.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28939 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
2ea5e5e8e5
commit
18f27bf19f
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2004, Haiku Inc. All Rights Reserved.
|
||||
* Copyright 2004-2009, Haiku Inc. All Rights Reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _STRING_H_
|
||||
@ -58,6 +58,7 @@ extern int strncasecmp(const char *string1, const char *string2, size_t length)
|
||||
extern char *strcasestr(const char *string, const char *searchString);
|
||||
|
||||
extern char *strdup(const char *string);
|
||||
extern char *strndup(const char* string, size_t size);
|
||||
extern char *stpcpy(char *dest, const char *source);
|
||||
|
||||
extern size_t strlcat(char *dest, const char *source, size_t length);
|
||||
|
@ -27,6 +27,7 @@ MergeObject posix_string.o :
|
||||
strncat.c
|
||||
strncmp.c
|
||||
strncpy.c
|
||||
strndup.cpp
|
||||
strnlen.c
|
||||
strpbrk.c
|
||||
strrchr.c
|
||||
|
30
src/system/libroot/posix/string/strndup.cpp
Normal file
30
src/system/libroot/posix/string/strndup.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
extern "C" char*
|
||||
strndup(const char* string, size_t size)
|
||||
{
|
||||
// While POSIX does not mention it, we handle NULL pointers gracefully
|
||||
if (string == NULL)
|
||||
return NULL;
|
||||
|
||||
size_t length = strlen(string);
|
||||
if (length > size)
|
||||
length = size;
|
||||
|
||||
char* copied = (char*)malloc(length + 1);
|
||||
if (copied == NULL)
|
||||
return NULL;
|
||||
|
||||
memcpy(copied, string, length);
|
||||
copied[length] = '\0';
|
||||
|
||||
return copied;
|
||||
}
|
Loading…
Reference in New Issue
Block a user