mirror of
https://github.com/MariaDB/server.git
synced 2025-01-22 14:54:20 +01:00
Addresses #1531 Reimplemented setenv, unsetenv correctly
git-svn-id: file:///svn/toku/tokudb@9974 c7de825b-a66e-492c-adef-691d508d4ae1
This commit is contained in:
parent
04fe238cc4
commit
a8fd238d7e
1 changed files with 13 additions and 21 deletions
|
@ -1,25 +1,22 @@
|
|||
#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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue