From 20b9f1ff5989cdd9e09a79c65b2df151b4f9b30f Mon Sep 17 00:00:00 2001 From: Zac Pierson Date: Thu, 23 Feb 2017 15:28:25 -0600 Subject: [PATCH] Added `getenv` to the *nix stdlib. --- core/os_linux.odin | 9 +++++++++ core/os_x.odin | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/core/os_linux.odin b/core/os_linux.odin index 5d1d6c98d..1ec48d340 100644 --- a/core/os_linux.odin +++ b/core/os_linux.odin @@ -49,6 +49,7 @@ unix_gettid :: proc() -> u64 unix_malloc :: proc(size: int) -> rawptr #foreign libc "malloc"; unix_free :: proc(ptr: rawptr) #foreign libc "free"; unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #foreign libc "realloc"; +unix_getenv :: proc(^u8) -> ^u8 #foreign libc "getenv"; unix_exit :: proc(status: int) #foreign libc "exit"; @@ -180,6 +181,14 @@ heap_free :: proc(ptr: rawptr) { unix_free(ptr); } +getenv :: proc(name: string) -> (string, bool) { + cstr: ^u8 = unix_getenv(to_c_str(name)); + if(cstr == nil) { + return "", false; + } + return from_c_str(cstr), true; +} + exit :: proc(code: int) { unix_exit(code); } diff --git a/core/os_x.odin b/core/os_x.odin index 80f858a5c..be86e2252 100644 --- a/core/os_x.odin +++ b/core/os_x.odin @@ -52,6 +52,7 @@ unix_gettid :: proc() -> u64 unix_malloc :: proc(size: int) -> rawptr #foreign libc "malloc"; unix_free :: proc(ptr: rawptr) #foreign libc "free"; unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #foreign libc "realloc"; +unix_getenv :: proc(^u8) -> ^u8 #foreign libc "getenv"; unix_exit :: proc(status: int) #foreign libc "exit"; @@ -182,6 +183,13 @@ heap_free :: proc(ptr: rawptr) #inline { unix_free(ptr); } +getenv :: proc(name: string) -> (string, bool) { + cstr: ^u8 = unix_getenv(to_c_str(name)); + if(cstr == nil) { + return "", false; + } + return from_c_str(cstr), true; +} exit :: proc(code: int) #inline { unix_exit(code);