From 581769b72d882fdc05bdbdc588db97187329e5b5 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 24 Oct 2007 00:37:50 -0500 Subject: Add strndup implemention for environments that are missing it Signed-off-by: Dan McGee --- src/pacman/util.c | 22 ++++++++++++++++++++++ src/pacman/util.h | 4 ++++ 2 files changed, 26 insertions(+) (limited to 'src') diff --git a/src/pacman/util.c b/src/pacman/util.c index e182f38f..ecdb2ad8 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -515,4 +515,26 @@ int pm_vfprintf(FILE *stream, pmloglevel_t level, const char *format, va_list ar return(ret); } +#ifndef HAVE_STRNDUP +/* A quick and dirty implementation derived from glibc */ +static size_t strnlen(const char *s, size_t max) +{ + register const char *p; + for(p = s; *p && max--; ++p); + return(p - s); +} + +char *strndup(const char *s, size_t n) +{ + size_t len = strnlen(s, n); + char *new = (char *) malloc(len + 1); + + if (new == NULL) + return NULL; + + new[len] = '\0'; + return (char *) memcpy(new, s, len); +} +#endif + /* vim: set ts=2 sw=2 noet: */ diff --git a/src/pacman/util.h b/src/pacman/util.h index 2814a780..4d17b179 100644 --- a/src/pacman/util.h +++ b/src/pacman/util.h @@ -53,6 +53,10 @@ int pm_printf(pmloglevel_t level, const char *format, ...) __attribute__((format int pm_fprintf(FILE *stream, pmloglevel_t level, const char *format, ...) __attribute__((format(printf,3,4))); int pm_vfprintf(FILE *stream, pmloglevel_t level, const char *format, va_list args) __attribute__((format(printf,3,0))); +#ifndef HAVE_STRNDUP +char *strndup(const char *s, size_t n); +#endif + #endif /* _PM_UTIL_H */ /* vim: set ts=2 sw=2 noet: */ -- cgit v1.2.3-70-g09d2