From c4e0ed39e776f1c127942b7c345e336942eb3a65 Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Thu, 3 Jul 2014 10:55:24 +0100 Subject: [PATCH] Add strndup() compat function for OSX The comment in connect_https_socket() suggests that I didn't use strndup() there because Solaris lacks it. I blithely went ahead and used it in the NTLM code though, and Solaris 11 *does* seem to have it. (My Solaris 10 VM no longer boots, and I can't bring myself to care.) But OSX (10.6.8 Snow Leopard) also seems to lack strndup(). So add a compat implementation of it rather than just trying to remember not to use it. Signed-off-by: David Woodhouse --- compat.c | 17 +++++++++++++++++ configure.ac | 1 + openconnect-internal.h | 5 +++++ 3 files changed, 23 insertions(+) diff --git a/compat.c b/compat.c index 6d5ebe87..f70d0af5 100644 --- a/compat.c +++ b/compat.c @@ -165,6 +165,23 @@ char *openconnect__strcasestr(const char *haystack, const char *needle) } #endif +#ifndef HAVE_STRNDUP +char *openconnect__strndup(const char *s, size_t n) +{ + char *r; + + if (n > strlen(s)) + n = strlen(s); + + r = malloc(n + 1); + if (r) { + memcpy(r, s, n); + r[n] = 0; + } + return r; +} +#endif + #ifndef HAVE_SETENV int openconnect__setenv(const char *name, const char *value, int overwrite) { diff --git a/configure.ac b/configure.ac index b38e37dc..a5583a95 100644 --- a/configure.ac +++ b/configure.ac @@ -105,6 +105,7 @@ AC_CHECK_FUNC(fdevname_r, [AC_DEFINE(HAVE_FDEVNAME_R, 1, [Have fdevname_r() func AC_CHECK_FUNC(getline, [AC_DEFINE(HAVE_GETLINE, 1, [Have getline() function])], [symver_getline="openconnect__getline;"]) AC_CHECK_FUNC(strcasestr, [AC_DEFINE(HAVE_STRCASESTR, 1, [Have strcasestr() function])], []) +AC_CHECK_FUNC(strndup, [AC_DEFINE(HAVE_STRNDUP, 1, [Have strndup() function])], []) AC_CHECK_FUNC(asprintf, [AC_DEFINE(HAVE_ASPRINTF, 1, [Have asprintf() function])], []) if test -n "$symver_asprintf"; then AC_MSG_CHECKING([for va_copy]) diff --git a/openconnect-internal.h b/openconnect-internal.h index 330ece23..26c02495 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -444,6 +444,11 @@ ssize_t openconnect__getline(char **lineptr, size_t *n, FILE *stream); #define strcasestr openconnect__strcasestr char *openconnect__strcasestr(const char *haystack, const char *needle); #endif +#ifndef HAVE_STRNDUP +#undef strndup +#define strndup openconnect__strndup +char *openconnect__strndup(const char *s, size_t n); +#endif #ifndef HAVE_SETENV #define setenv openconnect__setenv int openconnect__setenv(const char *name, const char *value, int overwrite);