diff --git a/std/os/file.jule b/std/os/file.jule index 86606c49a..6a7c8368a 100644 --- a/std/os/file.jule +++ b/std/os/file.jule @@ -4,7 +4,6 @@ use "std/internal/poll" use "std/io" -use "std/jule/integrated" use "std/sys" // Seek whence values. diff --git a/std/os/proc.jule b/std/os/proc.jule index 25492f91b..f594f7a70 100644 --- a/std/os/proc.jule +++ b/std/os/proc.jule @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD 3-Clause // license that can be found in the LICENSE file. +use integ "std/jule/integrated" use "std/os/internal" use "std/runtime" use "std/sys" diff --git a/std/os/proc_unix.jule b/std/os/proc_unix.jule new file mode 100644 index 000000000..7135c89a7 --- /dev/null +++ b/std/os/proc_unix.jule @@ -0,0 +1,15 @@ +// Copyright 2024 The Jule Programming Language. +// Use of this source code is governed by a BSD 3-Clause +// license that can be found in the LICENSE file. + +use integ "std/jule/integrated" +use "std/sys" + +// Retrieves the value of the environment variable named by the key. +// If the variable is present in the environment the value (which may be empty) +// is returned and the boolean is false. Otherwise the returned value will be +// empty and the boolean will be true. +fn LookupEnv(key: str): (val: str, unset: bool) { + keyPtr := integ::StrToBytes(key) + ret unsafe { sys::Getenv(&keyPtr[0]) } +} \ No newline at end of file diff --git a/std/os/proc_windows.jule b/std/os/proc_windows.jule new file mode 100644 index 000000000..e0d75fcf4 --- /dev/null +++ b/std/os/proc_windows.jule @@ -0,0 +1,15 @@ +// Copyright 2024 The Jule Programming Language. +// Use of this source code is governed by a BSD 3-Clause +// license that can be found in the LICENSE file. + +use integ "std/jule/integrated" +use "std/sys" + +// Retrieves the value of the environment variable named by the key. +// If the variable is present in the environment the value (which may be empty) +// is returned and the boolean is false. Otherwise the returned value will be +// 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]) } +} \ No newline at end of file diff --git a/std/sys/syscall_unix.jule b/std/sys/syscall_unix.jule index ca12d9880..ad5890e41 100644 --- a/std/sys/syscall_unix.jule +++ b/std/sys/syscall_unix.jule @@ -34,6 +34,7 @@ cpp unsafe fn remove(path: *integ::Char): int 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 // C's DIR. type Dir: cpp.DIR @@ -96,4 +97,16 @@ unsafe fn Mkdir(path: *byte, mode: int): int { unsafe fn Rmdir(path: *byte): int { ret cpp.rmdir((*integ::Char)(path)) } // Wrapper for C's unlink function. -unsafe fn Unlink(path: *byte): int { ret cpp.unlink((*integ::Char)(path)) } \ No newline at end of file +unsafe fn Unlink(path: *byte): int { ret cpp.unlink((*integ::Char)(path)) } + +// 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: *byte): (val: str, unset: bool) { + valPtr := cpp.getenv((*integ::Char)(key)) + if valPtr == nil { + unset = true + } else { + val = integ::BytePtrToStr((*byte)(valPtr)) + } + ret +} \ No newline at end of file diff --git a/std/sys/syscall_windows.jule b/std/sys/syscall_windows.jule index 0bd80f5b9..b0ce8745d 100644 --- a/std/sys/syscall_windows.jule +++ b/std/sys/syscall_windows.jule @@ -30,6 +30,7 @@ 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 fn GetLastError(): u32 cpp unsafe fn GetCurrentDirectoryW(bufflen: u32, buff: *integ::Wchar): u32 cpp unsafe fn SetCurrentDirectoryW(path: *integ::Wchar): bool @@ -154,4 +155,16 @@ unsafe fn FindNextFile(h: Handle, mut data: *Win32FindData): int { // Call's Windows FindClose function. 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 } \ No newline at end of file