diff options
author | Mattias Andrée <maandree@kth.se> | 2018-10-27 12:12:01 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-10-27 12:12:01 +0200 |
commit | 1f7da5379b80254d6c54b4ae5b7de4686af5bba5 (patch) | |
tree | 860d07aae73f535f5bbaeb10733dac77bef45c23 | |
parent | Add man pages for strn[case]chr[nul] and strrn[case]chr (diff) | |
download | libsimple-1f7da5379b80254d6c54b4ae5b7de4686af5bba5.tar.gz libsimple-1f7da5379b80254d6c54b4ae5b7de4686af5bba5.tar.bz2 libsimple-1f7da5379b80254d6c54b4ae5b7de4686af5bba5.tar.xz |
Remove strndup, it is in POSIX
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | README | 3 | ||||
-rw-r--r-- | libsimple/strndup.h | 6 | ||||
-rw-r--r-- | strndup.c | 70 | ||||
-rw-r--r-- | test.c | 11 |
5 files changed, 9 insertions, 82 deletions
@@ -102,7 +102,6 @@ OBJ =\ strncasestr.o\ strnchr.o\ strnchrnul.o\ - strndup.o\ strnend.o\ strnends.o\ strneqlen.o\ @@ -106,9 +106,6 @@ The following functions are defined (some as inline functions): void *libsimple_memdup(const void *, size_t) Duplicate a memory segment. - char *libsimple_strndup(const char *, size_t) - Duplicate a substring. - void *libsimple_mempcpy(void *, const void *, size_t) Like memcpy, except returns the byte after the last written byte. diff --git a/libsimple/strndup.h b/libsimple/strndup.h index 28ec293..90fbfa1 100644 --- a/libsimple/strndup.h +++ b/libsimple/strndup.h @@ -18,12 +18,6 @@ # endif #endif -_LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __assume_aligned__(1), __nonnull__, __warn_unused_result__))) -char *libsimple_strndup(const char *, size_t); -#ifndef strndup -# define strndup libsimple_strndup -#endif - _LIBSIMPLE_GCC_ONLY(__attribute__((__malloc__, __assume_aligned__(1), __nonnull__, __warn_unused_result__, __returns_nonnull__))) char *libsimple_enstrndup(int, const char *, size_t); #ifndef enstrndup diff --git a/strndup.c b/strndup.c deleted file mode 100644 index d4c681f..0000000 --- a/strndup.c +++ /dev/null @@ -1,70 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "libsimple.h" -#ifndef TEST - - -char * -libsimple_strndup(const char *s, size_t n) -{ - char *ret; - size_t m = strlen(s); - n = MIN(n, m); - if (n == SIZE_MAX) { - errno = ENOMEM; - return NULL; - } - if (!(ret = aligned_alloc(1, n + 1))) - return NULL; - memcpy(ret, s, n); - ret[n] = '\0'; - return ret; -} - - -#else -#include "test.h" - -int -main(void) -{ - struct allocinfo *info; - const char *s = "test"; - void *p; - - p = libsimple_strndup(s, 5); - assert(p && p != s); - assert(!strcmpnul(p, "test")); - memset(p, 0, 5); - assert(!strcmpnul(s, "test")); - if (have_custom_malloc()) { - assert((info = get_allocinfo(p))); - assert(info->alignment == 1); - } - free(p); - - p = libsimple_strndup(s, 3); - assert(p && p != s); - assert(!strcmpnul(p, "tes")); - memset(p, 0, 4); - assert(!strcmpnul(s, "test")); - if (have_custom_malloc()) { - assert((info = get_allocinfo(p))); - assert(info->alignment == 1); - } - free(p); - - p = libsimple_strndup(s, 0); - assert(p && p != s); - assert(!strcmpnul(p, "")); - memset(p, 0, 1); - assert(!strcmpnul(s, "test")); - if (have_custom_malloc()) { - assert((info = get_allocinfo(p))); - assert(info->alignment == 1); - } - free(p); - - return 0; -} - -#endif @@ -216,9 +216,16 @@ strdup(const char *s) char * -strndup(const char *s, size_t size) +strndup(const char *s, size_t n) { - return libsimple_strndup(s, size); + char *ret; + size_t m = strlen(s); + n = MIN(n, m); + if (!(ret = aligned_alloc(1, n + 1))) + return NULL; + memcpy(ret, s, n); + ret[n] = '\0'; + return ret; } |