Skip to content

Commit

Permalink
Add compatibility for systems lacking setenv() and unsetenv()
Browse files Browse the repository at this point in the history
We'll want this for Windows/MinGW support.
Based on a patch from Nikos Mavrogiannopoulos.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 5, 2014
1 parent 88cb866 commit 04aed63
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions compat.c
Expand Up @@ -24,6 +24,7 @@

#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>

Expand Down Expand Up @@ -168,3 +169,24 @@ char *openconnect__strcasestr(const char *haystack, const char *needle)
return NULL;
}
#endif

#ifndef HAVE_SETENV
int openconnect__setenv(const char *name, const char *value, int overwrite)
{
char *buf = alloca(strlen(name) + strlen(value) + 2);

sprintf(buf, "%s=%s", name, value);
putenv(buf);
return 0;
}
#endif

#ifndef HAVE_UNSETENV
void openconnect__unsetenv(const char *name)
{
char *buf = alloca(strlen(name) + 2);

sprintf(buf, "%s=", name);
putenv(buf);
}
#endif
2 changes: 2 additions & 0 deletions configure.ac
Expand Up @@ -144,6 +144,8 @@ AC_CHECK_FUNC(socket, [], AC_CHECK_LIB(socket, socket, [], AC_ERROR(Cannot find
AC_CHECK_FUNC(inet_aton, [], AC_CHECK_LIB(nsl, inet_aton, [], AC_ERROR(Cannot find inet_aton() function)))
AC_CHECK_FUNC(__android_log_vprint, [], AC_CHECK_LIB(log, __android_log_vprint, [], []))

AC_CHECK_FUNCS(setenv unsetenv)

AC_ENABLE_SHARED
AC_DISABLE_STATIC

Expand Down
8 changes: 8 additions & 0 deletions openconnect-internal.h
Expand Up @@ -346,6 +346,14 @@ 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_SETENV
#define setenv openconnect__setenv
int openconnect__setenv(const char *name, const char *value, int overwrite);
#endif
#ifndef HAVE_UNSETENV
#define unsetenv openconnect__unsetenv
void openconnect__unsetenv(const char *name);
#endif

/* I always coded as if it worked like this. Now it does. */
#define realloc_inplace(p, size) do { \
Expand Down

0 comments on commit 04aed63

Please sign in to comment.