From f3df767f76e579f10a569fcf2a45bb18664c84c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 12 Jun 2003 19:35:33 +0000 Subject: [PATCH] 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 --- src/kernel/libroot/posix/string/strdup.c | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/kernel/libroot/posix/string/strdup.c diff --git a/src/kernel/libroot/posix/string/strdup.c b/src/kernel/libroot/posix/string/strdup.c new file mode 100644 index 0000000000..7ab28a6baa --- /dev/null +++ b/src/kernel/libroot/posix/string/strdup.c @@ -0,0 +1,30 @@ +/* +** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the OpenBeOS License. +*/ + + +#include +#include + + +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; +} +