Skip to main content

c/
env.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ============================================================
5// getenv / setenv / putenv / unsetenv / clearenv
6// ============================================================
7
8#[no_mangle]
9pub static mut __environ: *mut *mut c_char = core::ptr::null_mut();
10
11// Growable tracking of strings allocated by setenv/putenv.  Realloc keeps
12// this unbounded; if it fails the new string is simply left untracked.
13static mut ENV_ALLOCED: *mut *mut c_char = core::ptr::null_mut();
14static mut ENV_ALLOCED_N: usize = 0;
15static mut ENV_ALLOCED_CAP: usize = 0;
16
17unsafe fn env_rm_add(old: *mut c_char, mut new: *mut c_char) {
18    for i in 0..ENV_ALLOCED_N {
19        if (*ENV_ALLOCED.add(i)) == old {
20            *ENV_ALLOCED.add(i) = new;
21            if !old.is_null() {
22                free(old as *mut c_void);
23            }
24            return;
25        } else if (*ENV_ALLOCED.add(i)).is_null() && !new.is_null() {
26            *ENV_ALLOCED.add(i) = new;
27            new = core::ptr::null_mut();
28        }
29    }
30    if !new.is_null() {
31        if ENV_ALLOCED_N == ENV_ALLOCED_CAP {
32            let new_cap = if ENV_ALLOCED_CAP == 0 { 16 } else { ENV_ALLOCED_CAP * 2 };
33            let grown = realloc(
34                ENV_ALLOCED as *mut c_void,
35                new_cap * core::mem::size_of::<*mut c_char>(),
36            ) as *mut *mut c_char;
37            if grown.is_null() {
38                return;
39            }
40            ENV_ALLOCED = grown;
41            ENV_ALLOCED_CAP = new_cap;
42        }
43        *ENV_ALLOCED.add(ENV_ALLOCED_N) = new;
44        ENV_ALLOCED_N += 1;
45    }
46}
47
48unsafe fn strchrnul_u8(s: *const u8, c: u8) -> *const u8 {
49    let mut p = s;
50    while *p != 0 && *p != c {
51        p = p.add(1);
52    }
53    p
54}
55
56#[no_mangle]
57pub unsafe extern "C" fn getenv(name: *const c_char) -> *mut c_char {
58    if __environ.is_null() || name.is_null() {
59        return core::ptr::null_mut();
60    }
61    let name = name as *const u8;
62    let l = strchrnul_u8(name, b'=') as usize - name as usize;
63    if l == 0 || *name.add(l) != 0 {
64        return core::ptr::null_mut();
65    }
66    let mut e = __environ;
67    while !(*e).is_null() {
68        let entry = *e as *const u8;
69        if strncmp(entry, name, l) == 0 && *entry.add(l) == b'=' {
70            return entry.add(l + 1) as *mut c_char;
71        }
72        e = e.add(1);
73    }
74    core::ptr::null_mut()
75}
76
77#[no_mangle]
78pub unsafe extern "C" fn setenv(
79    var: *const c_char,
80    value: *const c_char,
81    overwrite: c_int,
82) -> c_int {
83    if var.is_null() || value.is_null() {
84        ERRNO!() = EINVAL;
85        return -1;
86    }
87    let var = var as *const u8;
88    let l1 = strchrnul_u8(var, b'=') as usize - var as usize;
89    if l1 == 0 || *var.add(l1) != 0 {
90        ERRNO!() = EINVAL;
91        return -1;
92    }
93    if overwrite == 0 && !getenv(var as *const c_char).is_null() {
94        return 0;
95    }
96    let l2 = strlen(value as *const c_char);
97    let s = malloc(l1 + l2 + 2) as *mut u8;
98    if s.is_null() {
99        return -1;
100    }
101    core::ptr::copy_nonoverlapping(var, s, l1);
102    *s.add(l1) = b'=';
103    core::ptr::copy_nonoverlapping(value as *const u8, s.add(l1 + 1), l2 + 1);
104    putenv_internal(s as *mut c_char, l1, s as *mut c_char)
105}
106
107unsafe fn putenv_internal(s: *mut c_char, l: usize, r: *mut c_char) -> c_int {
108    let mut i = 0;
109    if !__environ.is_null() {
110        let mut e = __environ;
111        while !(*e).is_null() {
112            if strncmp(s as *const u8, *e as *const u8, l + 1) == 0 {
113                let tmp = *e;
114                // putenv() was handed the very string already stored in
115                // environ (a setenv-allocated entry).  It is still live and
116                // tracked, so it must not be freed as a replaced allocation:
117                // environ would keep pointing at freed storage.
118                if tmp == s {
119                    return 0;
120                }
121                *e = s;
122                env_rm_add(tmp, r);
123                return 0;
124            }
125            e = e.add(1);
126            i += 1;
127        }
128    }
129    let newenv = malloc(core::mem::size_of::<*mut c_char>() * (i + 2)) as *mut *mut c_char;
130    if newenv.is_null() {
131        if !r.is_null() { free(r as *mut c_void); }
132        return -1;
133    }
134    if i > 0 && !__environ.is_null() {
135        core::ptr::copy_nonoverlapping(__environ, newenv, i);
136    }
137    *newenv.add(i) = s;
138    *newenv.add(i + 1) = core::ptr::null_mut();
139    // don't free old __environ (may not be from our malloc)
140    __environ = newenv;
141    environ = __environ;
142    if !r.is_null() {
143        env_rm_add(core::ptr::null_mut(), r);
144    }
145    0
146}
147
148#[no_mangle]
149pub unsafe extern "C" fn putenv(s: *mut c_char) -> c_int {
150    if s.is_null() { return -1; }
151    let l = strchrnul_u8(s as *const u8, b'=') as usize - s as *const u8 as usize;
152    if l == 0 || *s.add(l) as u8 == 0 {
153        return unsetenv(s);
154    }
155    putenv_internal(s, l, core::ptr::null_mut())
156}
157
158#[no_mangle]
159pub unsafe extern "C" fn unsetenv(name: *const c_char) -> c_int {
160    if name.is_null() {
161        ERRNO!() = EINVAL;
162        return -1;
163    }
164    let name = name as *const u8;
165    let l = strchrnul_u8(name, b'=') as usize - name as usize;
166    if l == 0 || *name.add(l) != 0 {
167        ERRNO!() = EINVAL;
168        return -1;
169    }
170    if __environ.is_null() {
171        return 0;
172    }
173    let mut e = __environ;
174    let mut eo = e;
175    while !(*e).is_null() {
176        if strncmp(name, *e as *const u8, l) == 0 && *(*e as *const u8).add(l) == b'=' {
177            env_rm_add(*e, core::ptr::null_mut());
178        } else {
179            if eo != e {
180                *eo = *e;
181            }
182            eo = eo.add(1);
183        }
184        e = e.add(1);
185    }
186    if eo != e {
187        *eo = core::ptr::null_mut();
188    }
189    environ = __environ;
190    0
191}
192
193#[no_mangle]
194pub unsafe extern "C" fn clearenv() -> c_int {
195    let e = __environ;
196    __environ = core::ptr::null_mut();
197    environ = core::ptr::null_mut();
198    if !e.is_null() {
199        let mut p = e;
200        while !(*p).is_null() {
201            env_rm_add(*p, core::ptr::null_mut());
202            p = p.add(1);
203        }
204    }
205    0
206}
207