Handle overwrite on windows

Expand test to test overwrite
This commit is contained in:
Niklas Larsson 2020-06-16 13:41:40 +02:00
parent 8633dde240
commit 8540728202
5 changed files with 12 additions and 5 deletions

View File

@ -77,7 +77,7 @@ char* idris2_getEnvPair(int i) {
int idris2_setenv(const char *name, const char *value, int overwrite) {
#ifdef _WIN32
return win32_modenv(name, value);
return win32_modenv(name, value, overwrite);
#else
return setenv(name, value, overwrite);
#endif
@ -85,7 +85,7 @@ int idris2_setenv(const char *name, const char *value, int overwrite) {
int idris2_unsetenv(const char *name) {
#ifdef _WIN32
return win32_modenv(name, "");
return win32_modenv(name, "", 1);
#else
return unsetenv(name);
#endif

View File

@ -74,8 +74,9 @@ void win32_sleep(int ms) {
Sleep(ms);
}
int win32_modenv(const char* name, const char* value) {
int win32_modenv(const char* name, const char* value, int overwrite) {
char buffer[2000];
if (!overwrite && getenv(name)) return -1;
if (strlen(name) + strlen(value) > 1998) return -1;
sprintf(buffer, "%s=%s", name, value);
return putenv(buffer);

View File

@ -8,4 +8,4 @@ FILE *win32_u8fopen(const char *path, const char *mode);
FILE *win32_u8popen(const char *path, const char *mode);
void win32_gettime(int64_t* sec, int64_t* nsec);
void win32_sleep(int ms);
int win32_modenv(const char* name, const char* value);
int win32_modenv(const char* name, const char* value, int overwrite);

View File

@ -4,11 +4,15 @@ import System
main : IO ()
main = do
ok <- setEnv "HELLO" "HI" True
ok <- setEnv "HELLO" "HI" False
printLn ok
Just str <- getEnv "HELLO"
| Nothing => pure ()
putStrLn str
ok <- setEnv "HELLO" "HO" False
printLn ok
ok <- setEnv "HELLO" "EH" True
printLn ok
ok <- unsetEnv "HELLO"
printLn ok
Just str <- getEnv "HELLO"

View File

@ -1,5 +1,7 @@
True
HI
False
True
True
Nothing there
1/1: Building Envy (Envy.idr)