diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/compat.c | 22 | ||||
-rw-r--r-- | common/compat.h | 7 |
2 files changed, 29 insertions, 0 deletions
diff --git a/common/compat.c b/common/compat.c index af28e1d..ff8ee08 100644 --- a/common/compat.c +++ b/common/compat.c @@ -424,6 +424,28 @@ memdup (const void *data, #endif /* HAVE_MEMDUP */ +#ifndef HAVE_STRNDUP + +char * +strndup (const char *data, + size_t length) +{ + char *ret; + size_t len; + + len = strlen (data); + if (length > len) + length = len; + + ret = memdup (data, length + 1); + if (ret != NULL) + ret[length] = 0; + + return ret; +} + +#endif /* HAVE_STRDUP */ + #ifndef HAVE_STRCONCAT #include <stdarg.h> diff --git a/common/compat.h b/common/compat.h index ad80ca5..27e4403 100644 --- a/common/compat.h +++ b/common/compat.h @@ -223,6 +223,13 @@ void * memdup (const void *data, #endif /* HAVE_MEMDUP */ +#ifndef HAVE_STRNDUP + +char * strndup (const char *data, + size_t length); + +#endif /* HAVE_STRDUP */ + #ifdef HAVE_STDBOOL_H #include <stdbool.h> #else |