Browse Source

added the setenv and unsetenv bindings for darwin, inspired but os_linux.odin

Hector Mejia 1 year ago
parent
commit
2b854c94da
1 changed files with 25 additions and 0 deletions
  1. 25 0
      core/os/os_darwin.odin

+ 25 - 0
core/os/os_darwin.odin

@@ -477,7 +477,11 @@ foreign libc {
 	@(link_name="calloc")   _unix_calloc   :: proc(num, size: int) -> rawptr ---
 	@(link_name="calloc")   _unix_calloc   :: proc(num, size: int) -> rawptr ---
 	@(link_name="free")     _unix_free     :: proc(ptr: rawptr) ---
 	@(link_name="free")     _unix_free     :: proc(ptr: rawptr) ---
 	@(link_name="realloc")  _unix_realloc  :: proc(ptr: rawptr, size: int) -> rawptr ---
 	@(link_name="realloc")  _unix_realloc  :: proc(ptr: rawptr, size: int) -> rawptr ---
+
 	@(link_name="getenv")   _unix_getenv   :: proc(cstring) -> cstring ---
 	@(link_name="getenv")   _unix_getenv   :: proc(cstring) -> cstring ---
+	@(link_name="unsetenv") _unix_unsetenv :: proc(cstring) -> c.int ---
+	@(link_name="setenv")   _unix_setenv   :: proc(key: cstring, value: cstring, overwrite: c.int) -> c.int ---
+
 	@(link_name="getcwd")   _unix_getcwd   :: proc(buf: cstring, len: c.size_t) -> cstring ---
 	@(link_name="getcwd")   _unix_getcwd   :: proc(buf: cstring, len: c.size_t) -> cstring ---
 	@(link_name="chdir")    _unix_chdir    :: proc(buf: cstring) -> c.int ---
 	@(link_name="chdir")    _unix_chdir    :: proc(buf: cstring) -> c.int ---
 	@(link_name="mkdir")    _unix_mkdir    :: proc(buf: cstring, mode: u16) -> c.int ---
 	@(link_name="mkdir")    _unix_mkdir    :: proc(buf: cstring, mode: u16) -> c.int ---
@@ -914,6 +918,27 @@ get_env :: proc(key: string, allocator := context.allocator) -> (value: string)
 	return
 	return
 }
 }
 
 
+set_env :: proc(key, value: string) -> Errno {
+	runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
+	key_cstring := strings.clone_to_cstring(key, context.temp_allocator)
+	value_cstring := strings.clone_to_cstring(value, context.temp_allocator)
+	res := _unix_setenv(key_cstring, value_cstring, 1)
+	if res < 0 {
+		return Errno(get_last_error())
+	}
+	return ERROR_NONE
+}
+
+unset_env :: proc(key: string) -> Errno {
+	runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
+	s := strings.clone_to_cstring(key, context.temp_allocator)
+	res := _unix_unsetenv(s)
+	if res < 0 {
+		return Errno(get_last_error())
+	}
+	return ERROR_NONE
+}
+
 get_current_directory :: proc() -> string {
 get_current_directory :: proc() -> string {
 	page_size := get_page_size() // NOTE(tetra): See note in os_linux.odin/get_current_directory.
 	page_size := get_page_size() // NOTE(tetra): See note in os_linux.odin/get_current_directory.
 	buf := make([dynamic]u8, page_size)
 	buf := make([dynamic]u8, page_size)