Changeset 19784

Show
Ignore:
Timestamp:
01/30/07 10:30:48 (2 years ago)
Author:
patrick
Message:

Work around what seems to be an issue with PR_GetEnv() and PR_SetEnv()
not handling environment variables that are set programmatically before
PR_Init() gets called. I am not sure that this is the best way to deal
with this (especially since this change only handles the Windows case),
but I need things to behave properly for an upcoming change where
DllMain?() will be used to modify the process environment. This can always
be backed out if a better solution is found.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • juggler/trunk/modules/vapor/vpr/md/NSPR/SystemNSPR.cpp

    r19729 r19784  
    3838#ifdef VPR_OS_Windows 
    3939#  include <winsock2.h> /* For struct tiemval */ 
     40#  include <cstdlib>    /* For std::getenv() and _putenv() */ 
     41#else 
     42#  include <cstring>    /* For strdup() */ 
    4043#endif 
    4144 
     45#include <sstream> 
    4246#include <boost/concept_check.hpp> 
    4347 
     
    5963} 
    6064 
     65bool SystemNSPR::getenv(const std::string& name, std::string& result) 
     66{ 
     67   bool status(false); 
     68 
     69#if defined(VPR_OS_Windows) 
     70#if defined(_MSC_VER) && _MSC_VER >= 1400 
     71   char* val(NULL); 
     72   size_t num; 
     73   _dupenv_s(&val, &num, name.c_str()); 
     74#else 
     75   const char* val = std::getenv(name.c_str()); 
     76#endif 
     77#else 
     78   const char* val = PR_GetEnv(name.c_str()); 
     79#endif 
     80 
     81   if ( val != NULL ) 
     82   { 
     83      result = val; 
     84      status = true; 
     85 
     86#if defined(VPR_OS_Windows) && defined(_MSC_VER) && _MSC_VER >= 1400 
     87      std::free(val); 
     88      val = NULL; 
     89#endif 
     90   } 
     91 
     92   return status; 
    6193} 
     94 
     95bool SystemNSPR::setenv(const std::string& name, const std::string& value) 
     96{ 
     97   bool status(false); 
     98   // NSPR and _putenv() require the form "name=value". 
     99   std::ostringstream env_stream; 
     100   env_stream << name << "=" << value; 
     101 
     102#if defined(VPR_OS_Windows) 
     103   const int ret_val = _putenv(env_stream.str().c_str()); 
     104   status = ret_val == 0; 
     105#else 
     106   // NSPR takes possesion of the string memory. 
     107   const PRStatus ret_val = PR_SetEnv(strdup(env_stream.str().c_str())); 
     108   status = ret_val == PR_SUCCESS; 
     109#endif 
     110 
     111   return status; 
     112} 
     113 
     114} 
  • juggler/trunk/modules/vapor/vpr/md/NSPR/SystemNSPR.h

    r19729 r19784  
    209209    *         variable could not be found in the run-time environment. 
    210210    */ 
    211    static bool getenv(const std::string& name, std::string& result) 
    212    { 
    213       char* val; 
    214       bool status(false); 
    215  
    216       val = PR_GetEnv(name.c_str()); 
    217  
    218       if ( val != NULL ) 
    219       { 
    220          result = val; 
    221          status = true; 
    222       } 
    223  
    224       return status; 
    225    } 
     211   static bool getenv(const std::string& name, std::string& result); 
    226212 
    227213   /** 
     
    242228    *         variable set operation failed. 
    243229    */ 
    244    static bool setenv(const std::string& name, const std::string& value) 
    245    { 
    246       // NSPR requires form of "name=value" 
    247       // NSPR takes possesion of the string memory, so we just leak here 
    248       std::string* set_value = new std::string(name); 
    249       (*set_value) += "="; 
    250       (*set_value) += value; 
    251  
    252       const PRStatus ret_val = PR_SetEnv(set_value->c_str()); 
    253  
    254       return ret_val == PR_SUCCESS; 
    255    } 
     230   static bool setenv(const std::string& name, const std::string& value); 
    256231 
    257232   /**