diff options
Diffstat (limited to '')
-rw-r--r-- | strndup.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/strndup.c b/strndup.c new file mode 100644 index 0000000..12c8006 --- /dev/null +++ b/strndup.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" + + +char * +libsimple_strndup(const char *s, size_t n) +{ + void *ret; + if (n == SIZE_MAX) { + errno = ENOMEM; + return NULL; + } + if (!(ret = malloc(n + 1))) + return NULL; + memcpy(ret, s, n); + ((char *)ret)[n] = '\0'; + return ret; +} |