1/* 2 * Copyright 2009, Axel D��rfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7#include <string.h> 8#include <stdlib.h> 9 10 11extern "C" char* 12strndup(const char* string, size_t size) 13{ 14 // While POSIX does not mention it, we handle NULL pointers gracefully 15 if (string == NULL) 16 return NULL; 17 18 size_t length = strnlen(string, size); 19 char* copied = (char*)malloc(length + 1); 20 if (copied == NULL) 21 return NULL; 22 23 memcpy(copied, string, length); 24 copied[length] = '\0'; 25 26 return copied; 27} 28