Added a BeOS compatible strdup() implementation; unlike other strdup()
implementations (and against its standard behaviour), it does now handle a NULL string parameter gracefully. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3485 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
** Copyright 2003, Axel Dörfler, [email protected]. All rights reserved.
|
||||||
|
** Distributed under the terms of the OpenBeOS License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
char *
|
||||||
|
strdup(const char *string)
|
||||||
|
{
|
||||||
|
char *copied;
|
||||||
|
size_t length;
|
||||||
|
|
||||||
|
// unlike the standard strdup() function, the BeOS implementation
|
||||||
|
// handles NULL strings gracefully
|
||||||
|
if (string == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
length = strlen(string) + 1;
|
||||||
|
|
||||||
|
if ((copied = (char *)malloc(length)) == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memcpy(copied, string, length);
|
||||||
|
return copied;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user