2009-01-21 11:30:02 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* win32env.c
|
2009-06-11 16:49:15 +02:00
|
|
|
* putenv() and unsetenv() for win32, that updates both process
|
|
|
|
* environment and the cached versions in (potentially multiple)
|
|
|
|
* MSVCRT.
|
2009-01-21 11:30:02 +01:00
|
|
|
*
|
|
|
|
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2009-06-11 16:49:15 +02:00
|
|
|
* $PostgreSQL: pgsql/src/port/win32env.c,v 1.3 2009/06/11 14:49:15 momjian Exp $
|
2009-01-21 11:30:02 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
pgwin32_putenv(const char *envval)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
char *envcpy;
|
|
|
|
char *cp;
|
2009-01-21 11:30:02 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each version of MSVCRT has its own _putenv() call in the runtime
|
|
|
|
* library.
|
|
|
|
*
|
2009-06-11 16:49:15 +02:00
|
|
|
* If we're in VC 7.0 or later (means != mingw), update in the 6.0
|
|
|
|
* MSVCRT.DLL environment as well, to work with third party libraries
|
|
|
|
* linked against it (such as gnuwin32 libraries).
|
2009-01-21 11:30:02 +01:00
|
|
|
*/
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1300)
|
2009-06-11 16:49:15 +02:00
|
|
|
typedef int (_cdecl * PUTENVPROC) (const char *);
|
|
|
|
HMODULE hmodule;
|
|
|
|
static PUTENVPROC putenvFunc = NULL;
|
|
|
|
int ret;
|
2009-01-21 11:30:02 +01:00
|
|
|
|
|
|
|
if (putenvFunc == NULL)
|
|
|
|
{
|
|
|
|
hmodule = GetModuleHandle("msvcrt");
|
|
|
|
if (hmodule == NULL)
|
|
|
|
return 1;
|
2009-06-11 16:49:15 +02:00
|
|
|
putenvFunc = (PUTENVPROC) GetProcAddress(hmodule, "_putenv");
|
2009-01-21 11:30:02 +01:00
|
|
|
if (putenvFunc == NULL)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = putenvFunc(envval);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2009-06-11 16:49:15 +02:00
|
|
|
#endif /* _MSC_VER >= 1300 */
|
2009-01-21 11:30:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Update the process environment - to make modifications visible to child
|
|
|
|
* processes.
|
2009-01-21 11:30:02 +01:00
|
|
|
*
|
|
|
|
* Need a copy of the string so we can modify it.
|
|
|
|
*/
|
|
|
|
envcpy = strdup(envval);
|
|
|
|
cp = strchr(envcpy, '=');
|
|
|
|
if (cp == NULL)
|
|
|
|
return -1;
|
|
|
|
*cp = '\0';
|
|
|
|
cp++;
|
2009-02-12 13:53:34 +01:00
|
|
|
if (strlen(cp))
|
2009-01-21 11:30:02 +01:00
|
|
|
{
|
2009-02-12 13:53:34 +01:00
|
|
|
/*
|
|
|
|
* Only call SetEnvironmentVariable() when we are adding a variable,
|
2009-06-11 16:49:15 +02:00
|
|
|
* not when removing it. Calling it on both crashes on at least
|
|
|
|
* certain versions of MingW.
|
2009-02-12 13:53:34 +01:00
|
|
|
*/
|
|
|
|
if (!SetEnvironmentVariable(envcpy, cp))
|
|
|
|
{
|
|
|
|
free(envcpy);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-21 11:30:02 +01:00
|
|
|
}
|
|
|
|
free(envcpy);
|
|
|
|
|
|
|
|
/* Finally, update our "own" cache */
|
|
|
|
return _putenv(envval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pgwin32_unsetenv(const char *name)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
char *envbuf;
|
2009-01-21 11:30:02 +01:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
envbuf = (char *) malloc(strlen(name) + 2);
|
2009-01-21 11:30:02 +01:00
|
|
|
if (!envbuf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sprintf(envbuf, "%s=", name);
|
|
|
|
pgwin32_putenv(envbuf);
|
|
|
|
free(envbuf);
|
|
|
|
}
|