Skip to content

Commit

Permalink
std/os: add the Setenv function
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Nov 22, 2024
1 parent 807c9a4 commit 7963e61
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
8 changes: 8 additions & 0 deletions std/os/proc_unix.jule
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ use "std/sys"
fn LookupEnv(key: str): (val: str, unset: bool) {
keyPtr := integ::StrToBytes(key)
ret unsafe { sys::Getenv(&keyPtr[0]) }
}

// Sets the value of the environment variable named by the key.
// Reports whether it successful.
fn Setenv(key: str, val: str): bool {
keyPtr := integ::StrToBytes(key)
valPtr := integ::StrToBytes(val)
ret unsafe { sys::Setenv(&keyPtr[0], &valPtr[0], 1) }
}
20 changes: 19 additions & 1 deletion std/os/proc_windows.jule
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,23 @@ use "std/sys"
// empty and the boolean will be true.
fn LookupEnv(key: str): (val: str, unset: bool) {
keyPtr := integ::UTF16FromStr(key)
ret unsafe { sys::Getenv(&keyPtr[0]) }
mut n := u32(100)
for {
mut b := make([]u16, n)
n = unsafe { sys::GetEnvironmentVariable(&keyPtr[0], &b[0], u32(len(b))) }
if n == 0 && sys::GetLastError() == sys::ERROR_ENVVAR_NOT_FOUND {
ret "", false
}
if n <= u32(len(b)) {
ret integ::UTF16ToStr(b[:n]), true
}
}
}

// Sets the value of the environment variable named by the key.
// Reports whether it successful.
fn Setenv(key: str, val: str): bool {
keyPtr := integ::UTF16FromStr(key)
valPtr := integ::UTF16FromStr(val)
ret unsafe { sys::SetEnvironmentVariable(&keyPtr[0], &valPtr[0]) }
}
7 changes: 7 additions & 0 deletions std/sys/syscall_unix.jule
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cpp unsafe fn mkdir(path: *integ::Char, mode: int): int
cpp unsafe fn rmdir(path: *integ::Char): int
cpp unsafe fn unlink(path: *integ::Char): int
cpp unsafe fn getenv(key: *integ::Char): *integ::Char
cpp unsafe fn setenv(key: *integ::Char, val: *integ::Char, overwrite: integ::Int): int

// C's DIR.
type Dir: cpp.DIR
Expand Down Expand Up @@ -109,4 +110,10 @@ unsafe fn Getenv(key: *byte): (val: str, unset: bool) {
val = integ::BytePtrToStr((*byte)(valPtr))
}
ret
}

// Wrapper for C's setenv function.
// Reports whether it successful.
unsafe fn Setenv(key: *byte, val: *byte, overwrite: integ::Int): bool {
ret cpp.setenv((*integ::Char)(key), (*integ::Char)(val), overwrite) == 0
}
21 changes: 10 additions & 11 deletions std/sys/syscall_windows.jule
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ cpp fn GetStdHandle(stdh: uintptr): *unsafe
cpp unsafe fn CloseHandle(stdh: *unsafe): bool
cpp unsafe fn _wstat(path: *integ::Wchar, mut handle: *cpp._stat): int
cpp unsafe fn _wopen(path: *integ::Wchar, flag: int, mode: int): int
cpp unsafe fn _wgetenv(key: *integ::Wchar): *integ::Wchar
cpp unsafe fn GetEnvironmentVariableW(key: *integ::Wchar, mut out: *integ::Wchar, nSize: cpp.DWORD): cpp.DWORD
cpp unsafe fn SetEnvironmentVariableW(key: *integ::Wchar, val: *integ::Wchar): bool
cpp fn GetLastError(): u32
cpp unsafe fn GetCurrentDirectoryW(bufflen: u32, buff: *integ::Wchar): u32
cpp unsafe fn SetCurrentDirectoryW(path: *integ::Wchar): bool
Expand Down Expand Up @@ -157,14 +158,12 @@ fn FindClose(h: Handle): int {
ret unsafe { cpp.FindClose(cpp.HANDLE(h)) }
}

// Retrieves the value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
unsafe fn Getenv(key: *u16): (val: str, unset: bool) {
valPtr := cpp._wgetenv((*integ::Wchar)(key))
if valPtr == nil {
unset = true
} else {
val = integ::U16PtrToStr((*u16)(valPtr))
}
ret
// Windows's GetEnvironmentVariableW function.
unsafe fn GetEnvironmentVariable(key: *u16, mut out: *u16, outLen: u32): u32 {
ret u32(cpp.GetEnvironmentVariableW((*integ::Wchar)(key), (*integ::Wchar)(out), cpp.DWORD(outLen)))
}

// Windows's SetEnvironmentVariableW function.
unsafe fn SetEnvironmentVariable(key: *u16, val: *u16): bool {
ret cpp.SetEnvironmentVariableW((*integ::Wchar)(key), (*integ::Wchar)(val))
}

0 comments on commit 7963e61

Please sign in to comment.