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 <toku_stdlib.h>
#include "toku_portability.h"
#include <assert.h>
int
setenv(const char *name, const char *value, int overwrite) {
char buf[2]; //Need a dummy buffer
BOOL exists = TRUE;
int r = GetEnvironmentVariable(name, buf, sizeof(buf));
if (r==0) {
r = GetLastError();
if (r==ERROR_ENVVAR_NOT_FOUND) exists = FALSE;
else {
errno = r;
r = -1;
goto cleanup;
}
}
char * current = getenv(name);
BOOL exists = current!=NULL;
int r;
if (overwrite || !exists) {
r = SetEnvironmentVariable(name, value);
if (r==0) {
char setstring[sizeof("=") + strlen(name) + strlen(value)];
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();
r = -1;
goto cleanup;
}
}
......@@ -30,12 +27,7 @@ cleanup:
int
unsetenv(const char *name) {
int r = SetEnvironmentVariable(name, NULL);
if (r==0) { //0 is failure
r = -1;
errno = GetLastError();
}
else r = 0;
int r = setenv(name, "", 1);
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