Add Error to os2.set_env

This commit is contained in:
gingerBill
2025-02-28 13:52:51 +00:00
parent b3bbb00f1a
commit 79944056b9
5 changed files with 32 additions and 43 deletions
+2 -2
View File
@@ -22,8 +22,8 @@ lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string,
} }
// set_env sets the value of the environment variable named by the key // set_env sets the value of the environment variable named by the key
// Returns true on success, false on failure // Returns Error on failure
set_env :: proc(key, value: string) -> bool { set_env :: proc(key, value: string) -> Error {
return _set_env(key, value) return _set_env(key, value)
} }
+6 -6
View File
@@ -54,7 +54,7 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
return return
} }
_set_env :: proc(key, v_new: string) -> bool { _set_env :: proc(key, v_new: string) -> Error {
if _org_env_begin == 0 { if _org_env_begin == 0 {
_build_env() _build_env()
} }
@@ -63,7 +63,7 @@ _set_env :: proc(key, v_new: string) -> bool {
kv_size := len(key) + len(v_new) + 2 kv_size := len(key) + len(v_new) + 2
if v_curr, idx := _lookup(key); idx != NOT_FOUND { if v_curr, idx := _lookup(key); idx != NOT_FOUND {
if v_curr == v_new { if v_curr == v_new {
return true return nil
} }
sync.mutex_lock(&_env_mutex) sync.mutex_lock(&_env_mutex)
defer sync.mutex_unlock(&_env_mutex) defer sync.mutex_unlock(&_env_mutex)
@@ -78,7 +78,7 @@ _set_env :: proc(key, v_new: string) -> bool {
if len(v_new) > len(v_curr) { if len(v_new) > len(v_curr) {
k_addr = ([^]u8)(runtime.heap_resize(k_addr, kv_size)) k_addr = ([^]u8)(runtime.heap_resize(k_addr, kv_size))
if k_addr == nil { if k_addr == nil {
return false return .Out_Of_Memory
} }
v_addr = &k_addr[len(key) + 1] v_addr = &k_addr[len(key) + 1]
} }
@@ -86,13 +86,13 @@ _set_env :: proc(key, v_new: string) -> bool {
v_addr[len(v_new)] = 0 v_addr[len(v_new)] = 0
append(&_env, string(k_addr[:kv_size])) append(&_env, string(k_addr[:kv_size]))
return true return nil
} }
} }
k_addr := ([^]u8)(runtime.heap_alloc(kv_size)) k_addr := ([^]u8)(runtime.heap_alloc(kv_size))
if k_addr == nil { if k_addr == nil {
return false return .Out_Of_Memory
} }
intrinsics.mem_copy_non_overlapping(k_addr, raw_data(key), len(key)) intrinsics.mem_copy_non_overlapping(k_addr, raw_data(key), len(key))
k_addr[len(key)] = '=' k_addr[len(key)] = '='
@@ -104,7 +104,7 @@ _set_env :: proc(key, v_new: string) -> bool {
sync.mutex_lock(&_env_mutex) sync.mutex_lock(&_env_mutex)
append(&_env, string(k_addr[:kv_size - 1])) append(&_env, string(k_addr[:kv_size - 1]))
sync.mutex_unlock(&_env_mutex) sync.mutex_unlock(&_env_mutex)
return true return nil
} }
_unset_env :: proc(key: string) -> bool { _unset_env :: proc(key: string) -> bool {
+6 -4
View File
@@ -26,13 +26,15 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
return return
} }
_set_env :: proc(key, value: string) -> (ok: bool) { _set_env :: proc(key, value: string) -> (err: Error) {
TEMP_ALLOCATOR_GUARD() TEMP_ALLOCATOR_GUARD()
ckey := strings.clone_to_cstring(key, temp_allocator()) ckey := strings.clone_to_cstring(key, temp_allocator()) or_return
cval := strings.clone_to_cstring(key, temp_allocator()) cval := strings.clone_to_cstring(key, temp_allocator()) or_return
ok = posix.setenv(ckey, cval, true) == .OK if posix.setenv(ckey, cval, true) != nil {
err = _get_platform_error_from_errno()
}
return return
} }
+11 -27
View File
@@ -80,46 +80,30 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
} }
@(require_results) @(require_results)
_set_env :: proc(key, value: string) -> bool { _set_env :: proc(key, value: string) -> (err: Error) {
if err := build_env(); err != nil { build_env() or_return
return false
}
sync.guard(&g_env_mutex) sync.guard(&g_env_mutex)
key_ptr, value_ptr, just_inserted, err := map_entry(&g_env, key) defer if err != nil {
if err != nil { delete_key(&g_env, key)
return false
} }
alloc_err: runtime.Allocator_Error key_ptr, value_ptr, just_inserted := map_entry(&g_env, key) or_return
if just_inserted { if just_inserted {
key_ptr^, alloc_err = clone_string(key, file_allocator()) key_ptr^ = clone_string(key, file_allocator()) or_return
if alloc_err != nil { defer if err != nil {
delete_key(&g_env, key)
return false
}
value_ptr^, alloc_err = clone_string(value, file_allocator())
if alloc_err != nil {
delete_key(&g_env, key)
delete(key_ptr^, file_allocator()) delete(key_ptr^, file_allocator())
return false
} }
value_ptr^ = clone_string(value, file_allocator()) or_return
return true return
} }
delete_string_if_not_original(value_ptr^) delete_string_if_not_original(value_ptr^)
value_ptr^, alloc_err = clone_string(value, file_allocator()) value_ptr^ = clone_string(value, file_allocator()) or_return
if alloc_err != nil { return
delete_key(&g_env, key)
return false
}
return true
} }
@(require_results) @(require_results)
+7 -4
View File
@@ -36,12 +36,15 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
return return
} }
_set_env :: proc(key, value: string) -> bool { _set_env :: proc(key, value: string) -> Error {
TEMP_ALLOCATOR_GUARD() TEMP_ALLOCATOR_GUARD()
k, _ := win32_utf8_to_wstring(key, temp_allocator()) k := win32_utf8_to_wstring(key, temp_allocator()) or_return
v, _ := win32_utf8_to_wstring(value, temp_allocator()) v := win32_utf8_to_wstring(value, temp_allocator()) or_return
return bool(win32.SetEnvironmentVariableW(k, v)) if !win32.SetEnvironmentVariableW(k, v) {
return _get_platform_error()
}
return nil
} }
_unset_env :: proc(key: string) -> bool { _unset_env :: proc(key: string) -> bool {