diff options
author | hasso <hasso> | 2005-09-19 09:53:21 +0000 |
---|---|---|
committer | hasso <hasso> | 2005-09-19 09:53:21 +0000 |
commit | e6a4feb763749ff0c63558db456e617915fd1386 (patch) | |
tree | e348859f9ef281521ad13552e062ea6da44a2248 /lib/str.c | |
parent | 96e30387f164148fe47da6d4481283a61d761c97 (diff) |
* configure.ac: Test existance of strndup.
* lib/str.[ch]: Add strndup() from glibc.
Diffstat (limited to 'lib/str.c')
-rw-r--r-- | lib/str.c | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -11,6 +11,9 @@ Note that these are not terribly efficient, since they make more than one pass over the argument strings. At some point, they should be optimized. + + The implementation of strndup is copied from glibc-2.3.5: + Copyright (C) 1996, 1997, 1998, 2001, 2002 Free Software Foundation, Inc. */ @@ -89,3 +92,18 @@ strnlen(const char *s, size_t maxlen) return (p = (const char *)memchr(s, '\0', maxlen)) ? (size_t)(p-s) : maxlen; } #endif + +#ifndef HAVE_STRNDUP +char * +strndup (const char *s, size_t maxlen) +{ + size_t len = strnlen (s, maxlen); + char *new = (char *) malloc (len + 1); + + if (new == NULL) + return NULL; + + new[len] = '\0'; + return (char *) memcpy (new, s, len); +} +#endif |