Skip to content

Commit

Permalink
std/os: add the LookupEnv function
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Nov 22, 2024
1 parent 84eee6f commit 9d98619
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
1 change: 0 additions & 1 deletion std/os/file.jule
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use "std/internal/poll"
use "std/io"
use "std/jule/integrated"
use "std/sys"

// Seek whence values.
Expand Down
1 change: 1 addition & 0 deletions std/os/proc.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions std/os/proc_unix.jule
Original file line number Diff line number Diff line change
@@ -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]) }
}
15 changes: 15 additions & 0 deletions std/os/proc_windows.jule
Original file line number Diff line number Diff line change
@@ -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]) }
}
15 changes: 14 additions & 1 deletion std/sys/syscall_unix.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) }
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
}
13 changes: 13 additions & 0 deletions std/sys/syscall_windows.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 9d98619

Please sign in to comment.