headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / string / strndup.cpp
blob94dc45e1dc3a0c1b6f03688ba5218e9c92cff05f
1 /*
2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <string.h>
8 #include <stdlib.h>
11 extern "C" char*
12 strndup(const char* string, size_t size)
14 // While POSIX does not mention it, we handle NULL pointers gracefully
15 if (string == NULL)
16 return NULL;
18 size_t length = strlen(string);
19 if (length > size)
20 length = size;
22 char* copied = (char*)malloc(length + 1);
23 if (copied == NULL)
24 return NULL;
26 memcpy(copied, string, length);
27 copied[length] = '\0';
29 return copied;