Header And Logo

PostgreSQL
| The world's most advanced open source database.

win32env.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * win32env.c
00004  *    putenv() and unsetenv() for win32, that updates both process
00005  *    environment and the cached versions in (potentially multiple)
00006  *    MSVCRT.
00007  *
00008  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00009  * Portions Copyright (c) 1994, Regents of the University of California
00010  *
00011  *
00012  * IDENTIFICATION
00013  *    src/port/win32env.c
00014  *
00015  *-------------------------------------------------------------------------
00016  */
00017 
00018 #include "c.h"
00019 
00020 int
00021 pgwin32_putenv(const char *envval)
00022 {
00023     char       *envcpy;
00024     char       *cp;
00025 
00026     /*
00027      * Each version of MSVCRT has its own _putenv() call in the runtime
00028      * library.
00029      *
00030      * mingw always uses MSVCRT.DLL, but if we are in a Visual C++
00031      * environment, attempt to update the environment in all MSVCRT modules
00032      * that are currently loaded, to work properly with any third party
00033      * libraries linked against a different MSVCRT but still relying on
00034      * environment variables.
00035      *
00036      * Also separately update the system environment that gets inherited by
00037      * subprocesses.
00038      */
00039 #ifdef _MSC_VER
00040     typedef int (_cdecl * PUTENVPROC) (const char *);
00041     static struct
00042     {
00043         char       *modulename;
00044         HMODULE     hmodule;
00045         PUTENVPROC  putenvFunc;
00046     }           rtmodules[] =
00047     {
00048         {
00049             "msvcrt", 0, NULL
00050         },                      /* Visual Studio 6.0 / mingw */
00051         {
00052             "msvcr70", 0, NULL
00053         },                      /* Visual Studio 2002 */
00054         {
00055             "msvcr71", 0, NULL
00056         },                      /* Visual Studio 2003 */
00057         {
00058             "msvcr80", 0, NULL
00059         },                      /* Visual Studio 2005 */
00060         {
00061             "msvcr90", 0, NULL
00062         },                      /* Visual Studio 2008 */
00063         {
00064             "msvcr100", 0, NULL
00065         },                      /* Visual Studio 2010 */
00066         {
00067             "msvcr110", 0, NULL
00068         },                      /* Visual Studio 2012 */
00069         {
00070             NULL, 0, NULL
00071         }
00072     };
00073     int         i;
00074 
00075     for (i = 0; rtmodules[i].modulename; i++)
00076     {
00077         if (rtmodules[i].putenvFunc == NULL)
00078         {
00079             if (rtmodules[i].hmodule == 0)
00080             {
00081                 /* Not attempted before, so try to find this DLL */
00082                 rtmodules[i].hmodule = GetModuleHandle(rtmodules[i].modulename);
00083                 if (rtmodules[i].hmodule == NULL)
00084                 {
00085                     /*
00086                      * Set to INVALID_HANDLE_VALUE so we know we have tried
00087                      * this one before, and won't try again.
00088                      */
00089                     rtmodules[i].hmodule = INVALID_HANDLE_VALUE;
00090                     continue;
00091                 }
00092                 else
00093                 {
00094                     rtmodules[i].putenvFunc = (PUTENVPROC) GetProcAddress(rtmodules[i].hmodule, "_putenv");
00095                     if (rtmodules[i].putenvFunc == NULL)
00096                     {
00097                         CloseHandle(rtmodules[i].hmodule);
00098                         rtmodules[i].hmodule = INVALID_HANDLE_VALUE;
00099                         continue;
00100                     }
00101                 }
00102             }
00103             else
00104             {
00105                 /*
00106                  * Module loaded, but we did not find the function last time.
00107                  * We're not going to find it this time either...
00108                  */
00109                 continue;
00110             }
00111         }
00112         /* At this point, putenvFunc is set or we have exited the loop */
00113         rtmodules[i].putenvFunc(envval);
00114     }
00115 #endif   /* _MSC_VER */
00116 
00117     /*
00118      * Update the process environment - to make modifications visible to child
00119      * processes.
00120      *
00121      * Need a copy of the string so we can modify it.
00122      */
00123     envcpy = strdup(envval);
00124     if (!envcpy)
00125         return -1;
00126     cp = strchr(envcpy, '=');
00127     if (cp == NULL)
00128     {
00129         free(envcpy);
00130         return -1;
00131     }
00132     *cp = '\0';
00133     cp++;
00134     if (strlen(cp))
00135     {
00136         /*
00137          * Only call SetEnvironmentVariable() when we are adding a variable,
00138          * not when removing it. Calling it on both crashes on at least
00139          * certain versions of MingW.
00140          */
00141         if (!SetEnvironmentVariable(envcpy, cp))
00142         {
00143             free(envcpy);
00144             return -1;
00145         }
00146     }
00147     free(envcpy);
00148 
00149     /* Finally, update our "own" cache */
00150     return _putenv(envval);
00151 }
00152 
00153 void
00154 pgwin32_unsetenv(const char *name)
00155 {
00156     char       *envbuf;
00157 
00158     envbuf = (char *) malloc(strlen(name) + 2);
00159     if (!envbuf)
00160         return;
00161 
00162     sprintf(envbuf, "%s=", name);
00163     pgwin32_putenv(envbuf);
00164     free(envbuf);
00165 }