Skip to content

Commit

Permalink
Add strndup() compat function for OSX
Browse files Browse the repository at this point in the history
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 <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Jul 3, 2014
1 parent a4df343 commit c4e0ed3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions compat.c
Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -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])
Expand Down
5 changes: 5 additions & 0 deletions openconnect-internal.h
Expand Up @@ -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);
Expand Down

0 comments on commit c4e0ed3

Please sign in to comment.