Removed fixed size of environment vars.

svn path=/trunk/; revision=2805
This commit is contained in:
Hartmut Birr 2002-04-01 22:01:33 +00:00
parent 2cda2c03b3
commit ae374594ce
2 changed files with 42 additions and 21 deletions

View file

@ -1,23 +1,35 @@
#include <windows.h> #include <windows.h>
#include <msvcrt/stdlib.h> #include <msvcrt/stdlib.h>
#define NDEBUG
#include <msvcrt/msvcrtdbg.h>
char *getenv(const char *name) char *getenv(const char *name)
{ {
char *buffer; char *buffer = (char*)0xffffffff;
buffer = (char *)malloc(MAX_PATH); int len = GetEnvironmentVariableA(name,buffer,0) + 1;
buffer[0] = 0; DPRINT("getenv(%s)\n", name);
if ( GetEnvironmentVariableA(name,buffer,MAX_PATH) == 0 ) buffer = (char *)malloc(len);
DPRINT("getenv('%s') %d %x\n", name, len, buffer);
if (buffer == NULL || GetEnvironmentVariableA(name,buffer,len) == 0 )
{
free(buffer);
return NULL; return NULL;
}
return buffer; return buffer;
} }
wchar_t *_wgetenv(const wchar_t *name) wchar_t *_wgetenv(const wchar_t *name)
{ {
wchar_t *buffer; wchar_t *buffer = (wchar_t*)0xffffffff;
buffer = (wchar_t *)malloc(MAX_PATH * sizeof(wchar_t)); int len = GetEnvironmentVariableW(name, buffer,0) + 1;
buffer[0] = 0; DPRINT("_wgetenv(%S)\n", name);
if ( GetEnvironmentVariableW(name,buffer,MAX_PATH) == 0 ) buffer = (wchar_t *)malloc(len * sizeof(wchar_t));
if (buffer == NULL || GetEnvironmentVariableW(name,buffer,len) == 0)
{
free(buffer);
return NULL; return NULL;
}
return buffer; return buffer;
} }

View file

@ -2,41 +2,50 @@
#include <msvcrt/stdlib.h> #include <msvcrt/stdlib.h>
#include <msvcrt/string.h> #include <msvcrt/string.h>
#define NDEBUG
#include <msvcrt/msvcrtdbg.h>
extern int BlockEnvToEnviron(); // defined in misc/dllmain.c extern int BlockEnvToEnviron(); // defined in misc/dllmain.c
int _putenv(const char *val) int _putenv(const char *val)
{ {
char buffer[1024]; char *buffer;
char *epos; char *epos;
int res; int res;
strcpy(buffer,val); DPRINT("_putenv('%s')\n", val);
epos = strchr(buffer, '='); epos = strchr(val, '=');
if ( epos == NULL ) if ( epos == NULL )
return -1; return -1;
buffer = (char*)malloc(epos - val + 1);
*epos = 0; if (buffer == NULL)
return -1;
strncpy(buffer, val, epos - val);
buffer[epos - val] = 0;
res = SetEnvironmentVariableA(buffer,epos+1); res = SetEnvironmentVariableA(buffer,epos+1);
if (BlockEnvToEnviron() ) return 0; free(buffer);
if (BlockEnvToEnviron()) return 0;
return res; return res;
} }
int _wputenv(const wchar_t *val) int _wputenv(const wchar_t *val)
{ {
wchar_t buffer[1024]; wchar_t *buffer;
wchar_t *epos; wchar_t *epos;
int res; int res;
wcscpy(buffer,val); DPRINT("_wputenv('%S')\n", val);
epos = wcschr(buffer, L'='); epos = wcsrchr(val, L'=');
if ( epos == NULL ) if ( epos == NULL )
return -1; return -1;
buffer = (char*)malloc((epos - val + 1) * sizeof (wchar_t));
*epos = 0; if (buffer == NULL)
return -1;
wcsncpy(buffer, val, epos - val);
buffer[epos - val] = 0;
res = SetEnvironmentVariableW(buffer,epos+1); res = SetEnvironmentVariableW(buffer,epos+1);
free(buffer);
if (BlockEnvToEnviron() ) return 0; if (BlockEnvToEnviron() ) return 0;
return res; return res;
} }