Merge pull request #313 from melted/fix_setenv

Fix setenv and unsetenv
This commit is contained in:
Niklas Larsson 2020-06-16 14:39:53 +02:00 committed by GitHub
commit f04af3dd56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 68 additions and 4 deletions

View File

@ -36,9 +36,9 @@ getArgs = primIO prim__getArgs
prim_getEnv : String -> PrimIO (Ptr String)
%foreign support "idris2_getEnvPair"
prim_getEnvPair : Int -> PrimIO (Ptr String)
%foreign libc "setenv"
%foreign support "idris2_setenv"
prim_setEnv : String -> String -> Int -> PrimIO Int
%foreign libc "setenv"
%foreign support "idris2_unsetenv"
prim_unsetEnv : String -> PrimIO Int
export

View File

@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#ifdef _WIN32
extern char **_environ;
@ -73,3 +74,19 @@ int idris2_time() {
char* idris2_getEnvPair(int i) {
return *(environ + i);
}
int idris2_setenv(const char *name, const char *value, int overwrite) {
#ifdef _WIN32
return win32_modenv(name, value, overwrite);
#else
return setenv(name, value, overwrite);
#endif
}
int idris2_unsetenv(const char *name) {
#ifdef _WIN32
return win32_modenv(name, "", 1);
#else
return unsetenv(name);
#endif
}

View File

@ -67,9 +67,14 @@ void win32_gettime(int64_t* sec, int64_t* nsec)
*nsec = (t.QuadPart % 10000000)*100;
*sec = t.QuadPart / 10000000;
*sec -= 11644473600; // LDAP epoch to Unix epoch
*sec -= 11644473600; // LDAP epoch to Unix epoch
}
void win32_sleep(int ms) {
Sleep(ms);
}
int win32_modenv(const char* name, const char* value, int overwrite) {
if (!overwrite && getenv(name)) return 0;
return _putenv_s(name, value);
}

View File

@ -8,3 +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 overwrite);

View File

@ -113,7 +113,7 @@ chezTests
= ["chez001", "chez002", "chez003", "chez004", "chez005", "chez006",
"chez007", "chez008", "chez009", "chez010", "chez011", "chez012",
"chez013", "chez014", "chez015", "chez016", "chez017", "chez018",
"chez019", "chez020", "chez021", "chez022", "chez023",
"chez019", "chez020", "chez021", "chez022", "chez023", "chez024",
"reg001"]
ideModeTests : List String

View File

@ -0,0 +1,26 @@
module Main
import System
main : IO ()
main = do
ok <- setEnv "HELLO" "HI" True
printLn ok
Just str <- getEnv "HELLO"
| Nothing => pure ()
putStrLn str
ok <- setEnv "HELLO" "HO" False
printLn ok
Just str <- getEnv "HELLO"
| Nothing => pure ()
putStrLn str
ok <- setEnv "HELLO" "EH" True
printLn ok
Just str <- getEnv "HELLO"
| Nothing => pure ()
putStrLn str
ok <- unsetEnv "HELLO"
printLn ok
Just str <- getEnv "HELLO"
| Nothing => putStrLn "Nothing there"
pure ()

View File

@ -0,0 +1,10 @@
True
HI
True
HI
True
EH
True
Nothing there
1/1: Building Envy (Envy.idr)
Main> Main> Bye for now!

2
tests/chez/chez024/input Normal file
View File

@ -0,0 +1,2 @@
:exec main
:q

3
tests/chez/chez024/run Normal file
View File

@ -0,0 +1,3 @@
$1 --no-banner Envy.idr < input
rm -rf build