diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-12-07 23:12:33 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-12-07 23:12:33 -0500 |
| commit | 86228917b98a3ef4019e9e18fcafacc948ffcfd1 (patch) | |
| tree | cf424b886cc0b743cd258b3a58ab832b3767f08b /src/stdlib | |
| parent | 08c47e1fabd1a2fb43c18828db8ad845f7db3a99 (diff) | |
Better error handling for setenv()
Diffstat (limited to 'src/stdlib')
| -rw-r--r-- | src/stdlib/stdlib.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c index defb263c..f4e6d678 100644 --- a/src/stdlib/stdlib.c +++ b/src/stdlib/stdlib.c @@ -226,8 +226,16 @@ OptionalText_t getenv_text(Text_t name) { public void setenv_text(Text_t name, OptionalText_t value) { - if (value.tag == TEXT_NONE) unsetenv(Text$as_c_string(name)); - else setenv(Text$as_c_string(name), Text$as_c_string(value), 1); + int status; + if (value.tag == TEXT_NONE) { + status = unsetenv(Text$as_c_string(name)); + } else { + status = setenv(Text$as_c_string(name), Text$as_c_string(value), 1); + } + if (status != 0) { + if (errno == EINVAL) fail("Invalid environment variable name: ", Text$quoted(name, false, Text("\""))); + else fail("Failed to set environment variable (", strerror(errno)); + } } typedef struct cleanup_s { |
