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
// Returns true on success, false on failure
set_env :: proc(key, value: string) -> bool {
// Returns Error on failure
set_env :: proc(key, value: string) -> Error {
return _set_env(key, value)
}
+6 -6
View File
@@ -54,7 +54,7 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
return
}
_set_env :: proc(key, v_new: string) -> bool {
_set_env :: proc(key, v_new: string) -> Error {
if _org_env_begin == 0 {
_build_env()
}
@@ -63,7 +63,7 @@ _set_env :: proc(key, v_new: string) -> bool {
kv_size := len(key) + len(v_new) + 2
if v_curr, idx := _lookup(key); idx != NOT_FOUND {
if v_curr == v_new {
return true
return nil
}
sync.mutex_lock(&_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) {
k_addr = ([^]u8)(runtime.heap_resize(k_addr, kv_size))
if k_addr == nil {
return false
return .Out_Of_Memory
}
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
append(&_env, string(k_addr[:kv_size]))
return true
return nil
}
}
k_addr := ([^]u8)(runtime.heap_alloc(kv_size))
if k_addr == nil {
return false
return .Out_Of_Memory
}
intrinsics.mem_copy_non_overlapping(k_addr, raw_data(key), len(key))
k_addr[len(key)] = '='
@@ -104,7 +104,7 @@ _set_env :: proc(key, v_new: string) -> bool {
sync.mutex_lock(&_env_mutex)
append(&_env, string(k_addr[:kv_size - 1]))
sync.mutex_unlock(&_env_mutex)
return true
return nil
}
_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
}
_set_env :: proc(key, value: string) -> (ok: bool) {
_set_env :: proc(key, value: string) -> (err: Error) {
TEMP_ALLOCATOR_GUARD()
ckey := strings.clone_to_cstring(key, temp_allocator())
cval := 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()) or_return
ok = posix.setenv(ckey, cval, true) == .OK
if posix.setenv(ckey, cval, true) != nil {
err = _get_platform_error_from_errno()
}
return
}
+11 -27
View File
@@ -80,46 +80,30 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
}
@(require_results)
_set_env :: proc(key, value: string) -> bool {
if err := build_env(); err != nil {
return false
}
_set_env :: proc(key, value: string) -> (err: Error) {
build_env() or_return
sync.guard(&g_env_mutex)
key_ptr, value_ptr, just_inserted, err := map_entry(&g_env, key)
if err != nil {
return false
defer if err != nil {
delete_key(&g_env, key)
}
alloc_err: runtime.Allocator_Error
key_ptr, value_ptr, just_inserted := map_entry(&g_env, key) or_return
if just_inserted {
key_ptr^, alloc_err = clone_string(key, file_allocator())
if alloc_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)
key_ptr^ = clone_string(key, file_allocator()) or_return
defer if err != nil {
delete(key_ptr^, file_allocator())
return false
}
return true
value_ptr^ = clone_string(value, file_allocator()) or_return
return
}
delete_string_if_not_original(value_ptr^)
value_ptr^, alloc_err = clone_string(value, file_allocator())
if alloc_err != nil {
delete_key(&g_env, key)
return false
}
return true
value_ptr^ = clone_string(value, file_allocator()) or_return
return
}
@(require_results)
+7 -4
View File
@@ -36,12 +36,15 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
return
}
_set_env :: proc(key, value: string) -> bool {
_set_env :: proc(key, value: string) -> Error {
TEMP_ALLOCATOR_GUARD()
k, _ := win32_utf8_to_wstring(key, temp_allocator())
v, _ := win32_utf8_to_wstring(value, temp_allocator())
k := win32_utf8_to_wstring(key, temp_allocator()) or_return
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 {