diff options
Diffstat (limited to '')
-rw-r--r-- | strndup.c | 44 |
1 files changed, 41 insertions, 3 deletions
@@ -3,9 +3,11 @@ char * -libsimple_strndup(const char *s, size_t n) +libsimple_strndup(const char *s, size_t n) /* FIXME */ { - void *ret; + char *ret; + size_t m = strlen(s); + n = MIN(n, m); if (n == SIZE_MAX) { errno = ENOMEM; return NULL; @@ -13,6 +15,42 @@ libsimple_strndup(const char *s, size_t n) if (!(ret = malloc(n + 1))) return NULL; memcpy(ret, s, n); - ((char *)ret)[n] = '\0'; + ret[n] = '\0'; return ret; } + + +#ifdef TEST +#include <assert.h> + +int +main(void) +{ + const char *s = "test"; + void *p; + + p = libsimple_memdup(s, 5); + assert(p && p != s); + assert(!strcmp(p, "test")); + memset(p, 0, 5); + assert(!strcmp(s, "test")); + free(p); + + p = libsimple_memdup(s, 3); + assert(p && p != s); + assert(!strcmp(p, "tes")); + memset(p, 0, 5); + assert(!strcmp(s, "test")); + free(p); + + p = libsimple_memdup(s, 0); + assert(p && p != s); + assert(!strcmp(p, "")); + memset(p, 0, 5); + assert(!strcmp(s, "test")); + free(p); + + return 0; +} + +#endif |