Skip to content

Commit

Permalink
add openconnect__win32_setenv function to compat.c
Browse files Browse the repository at this point in the history
Based on:

* POSIX-compatible `setenv` implementation for Windows: https://stackoverflow.com/a/23616164
* Enabling Windows "secure API" getenv_s and _putenv_s functions in MinGW:
  * Using -DMINGW_HAS_SECURE_API: https://stackoverflow.com/a/51977723
  * By manually defining their prototypes: https://stackoverflow.com/a/51977723
  * Apparently, only newer versions of MinGW follow the MINGW_HAS_SECURE_API flag, and
    autodetecting them is quite hard.

Signed-off-by: Daniel Lenski <dlenski@gmail.com>
  • Loading branch information
dlenski committed Jan 23, 2021
1 parent 3e237a7 commit e5770db
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions compat.c
Expand Up @@ -19,6 +19,25 @@

#include <string.h>
#include <stdarg.h>
#ifdef _WIN32
#include <sec_api/stdlib_s.h> /* errno_t, size_t */
errno_t getenv_s(
size_t *ret_required_buf_size,
char *buf,
size_t buf_size_in_bytes,
const char *name
);
errno_t _putenv_s(
const char *varname,
const char *value_string
);

/* XX: needed to get _putenv_s, getenv_s from stdlib.h with MinGW,
* but only works on newer versions.
* https://stackoverflow.com/a/51977723
*/
/* #define MINGW_HAS_SECURE_API 1 */
#endif
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
Expand Down Expand Up @@ -196,6 +215,18 @@ int openconnect__inet_aton(const char *cp, struct in_addr *addr)
#endif

#ifdef _WIN32
int openconnect__win32_setenv(const char *name, const char *value, int overwrite)
{
/* https://stackoverflow.com/a/23616164 */
int errcode = 0;
if(!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if(errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}

char *openconnect__win32_strerror(DWORD err)
{
wchar_t *msgw;
Expand Down
3 changes: 3 additions & 0 deletions openconnect-internal.h
Expand Up @@ -825,6 +825,9 @@ static inline int tun_is_up(struct openconnect_info *vpninfo)
#define pipe(fds) _pipe(fds, 4096, O_BINARY)
int openconnect__win32_sock_init();
char *openconnect__win32_strerror(DWORD err);
#undef setenv
#define setenv openconnect__win32_setenv
int openconnect__win32_setenv(const char *name, const char *value, int overwrite);
#undef inet_pton
#define inet_pton openconnect__win32_inet_pton
int openconnect__win32_inet_pton(int af, const char *src, void *dst);
Expand Down

0 comments on commit e5770db

Please sign in to comment.