From 4ec3236fcb2aea3c5fc5e97e4766f1cbfcd6aaef Mon Sep 17 00:00:00 2001 From: Stephen Hurd <deuce@synchro.net> Date: Tue, 10 Nov 2020 15:51:50 -0500 Subject: [PATCH] Add strndup() and strnlen() from FreeBSD (used by RIP support) --- src/xpdev/strwrap.c | 31 +++++++++++++++++++++++++++++++ src/xpdev/strwrap.h | 13 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/xpdev/strwrap.c b/src/xpdev/strwrap.c index e3174a36a9..0af1480511 100644 --- a/src/xpdev/strwrap.c +++ b/src/xpdev/strwrap.c @@ -38,5 +38,36 @@ char* ltoa(long val, char* str, int radix) } return(str); } +#endif + +#ifdef _WIN32 +/* From FreeBSD */ +size_t +strnlen(const char *s, size_t maxlen) +{ + size_t len; + + for (len = 0; len < maxlen; len++, s++) { + if (!*s) + break; + } + return (len); +} + +/* From FreeBSD */ +char * +strndup(const char *str, size_t maxlen) +{ + char *copy; + size_t len; + len = strnlen(str, maxlen); + copy = malloc(len + 1); + if (copy != NULL) { + (void)memcpy(copy, str, len); + copy[len] = '\0'; + } + + return copy; +} #endif diff --git a/src/xpdev/strwrap.h b/src/xpdev/strwrap.h index 1f8a181d14..f30d54e450 100644 --- a/src/xpdev/strwrap.h +++ b/src/xpdev/strwrap.h @@ -18,4 +18,17 @@ char* ltoa(long val, char* str, int radix); #endif +#ifdef _MSVC +#if defined(__cplusplus) +extern "C" { +#endif + +char *strndup(const char *str, size_t maxlen); +size_t strnlen(const char *s, size_t maxlen); + +#if defined(__cplusplus) +} +#endif +#endif + #endif -- GitLab