From 54a326f04601e93475c74b5a862d0269c70933b6 Mon Sep 17 00:00:00 2001 From: William Roe Date: Mon, 23 May 2022 13:09:37 +0100 Subject: [PATCH] [os] Darwin,FreeBSD,OpenBSD: Rename os.getenv to os.get_env Make os.get_env consistent across Unixes This matches the function name and API from env_windows.odin and os_linux.odin, which should be the same everywhere. Meaning: * named get_env and not getenv * return a string (empty if the environment variable is not found) * accept a default value parameter for the allocator (defaulting to context.allocator) * calls lookup_env which returns an extra found boolean value This is so that you don't have to write platform/OS conditionals when getting environment variable values from the stdlib os.get_env/getenv function. --- core/os/os_darwin.odin | 11 ++++++++--- core/os/os_freebsd.odin | 11 ++++++++--- core/os/os_openbsd.odin | 11 ++++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin index c36823e3f..41dc8345c 100644 --- a/core/os/os_darwin.odin +++ b/core/os/os_darwin.odin @@ -633,13 +633,18 @@ heap_free :: proc(ptr: rawptr) { _unix_free(ptr) } -getenv :: proc(name: string) -> (string, bool) { - path_str := strings.clone_to_cstring(name, context.temp_allocator) +lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { + path_str := strings.clone_to_cstring(key, context.temp_allocator) cstr := _unix_getenv(path_str) if cstr == nil { return "", false } - return string(cstr), true + return strings.clone(string(cstr), allocator), true +} + +get_env :: proc(key: string, allocator := context.allocator) -> (value: string) { + value, _ = lookup_env(key, allocator) + return } get_current_directory :: proc() -> string { diff --git a/core/os/os_freebsd.odin b/core/os/os_freebsd.odin index 6545423d4..a991caafc 100644 --- a/core/os/os_freebsd.odin +++ b/core/os/os_freebsd.odin @@ -618,13 +618,18 @@ heap_free :: proc(ptr: rawptr) { _unix_free(ptr) } -getenv :: proc(name: string) -> (string, bool) { - path_str := strings.clone_to_cstring(name, context.temp_allocator) +lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { + path_str := strings.clone_to_cstring(key, context.temp_allocator) cstr := _unix_getenv(path_str) if cstr == nil { return "", false } - return string(cstr), true + return strings.clone(string(cstr), allocator), true +} + +get_env :: proc(key: string, allocator := context.allocator) -> (value: string) { + value, _ = lookup_env(key, allocator) + return } get_current_directory :: proc() -> string { diff --git a/core/os/os_openbsd.odin b/core/os/os_openbsd.odin index dd230f9b5..9a3dbd874 100644 --- a/core/os/os_openbsd.odin +++ b/core/os/os_openbsd.odin @@ -620,13 +620,18 @@ heap_free :: proc(ptr: rawptr) { _unix_free(ptr) } -getenv :: proc(name: string) -> (string, bool) { - path_str := strings.clone_to_cstring(name, context.temp_allocator) +lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { + path_str := strings.clone_to_cstring(key, context.temp_allocator) cstr := _unix_getenv(path_str) if cstr == nil { return "", false } - return string(cstr), true + return strings.clone(string(cstr), allocator), true +} + +get_env :: proc(key: string, allocator := context.allocator) -> (value: string) { + value, _ = lookup_env(key, allocator) + return } get_current_directory :: proc() -> string {