Commit 08ca3223 authored by Yoni Fogel's avatar Yoni Fogel

Addresses #1531 Reimplemented setenv, unsetenv correctly

git-svn-id: file:///svn/toku/tokudb@9974 c7de825b-a66e-492c-adef-691d508d4ae1
parent bc32229a
#include <windows.h> #include <windows.h>
#include <toku_stdlib.h> #include <toku_stdlib.h>
#include "toku_portability.h"
#include <assert.h>
int int
setenv(const char *name, const char *value, int overwrite) { setenv(const char *name, const char *value, int overwrite) {
char buf[2]; //Need a dummy buffer char * current = getenv(name);
BOOL exists = TRUE; BOOL exists = current!=NULL;
int r = GetEnvironmentVariable(name, buf, sizeof(buf));
if (r==0) { int r;
r = GetLastError();
if (r==ERROR_ENVVAR_NOT_FOUND) exists = FALSE;
else {
errno = r;
r = -1;
goto cleanup;
}
}
if (overwrite || !exists) { if (overwrite || !exists) {
r = SetEnvironmentVariable(name, value); char setstring[sizeof("=") + strlen(name) + strlen(value)];
if (r==0) { int bytes = snprintf(setstring, sizeof(setstring), "%s=%s", name, value);
assert(bytes>=0);
assert((size_t)bytes < sizeof(setstring));
r = _putenv(setstring);
if (r==-1) {
errno = GetLastError(); errno = GetLastError();
r = -1;
goto cleanup; goto cleanup;
} }
} }
...@@ -30,12 +27,7 @@ cleanup: ...@@ -30,12 +27,7 @@ cleanup:
int int
unsetenv(const char *name) { unsetenv(const char *name) {
int r = SetEnvironmentVariable(name, NULL); int r = setenv(name, "", 1);
if (r==0) { //0 is failure
r = -1;
errno = GetLastError();
}
else r = 0;
return r; return r;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment