Jelajahi Sumber

Added `getenv` to the *nix stdlib.

Zac Pierson 8 tahun lalu
induk
melakukan
20b9f1ff59
2 mengubah file dengan 17 tambahan dan 0 penghapusan
  1. 9 0
      core/os_linux.odin
  2. 8 0
      core/os_x.odin

+ 9 - 0
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);
 }

+ 8 - 0
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);