Skip to content

Commit 62caa1e

Browse files
committed
auto merge of #6505 : cmr/rust/unsetenv, r=catamorphism
2 parents f6360b4 + 6ef226d commit 62caa1e

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/libcore/os.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,33 @@ pub fn setenv(n: &str, v: &str) {
289289
}
290290
}
291291

292+
/// Remove a variable from the environment entirely
293+
pub fn unsetenv(n: &str) {
294+
#[cfg(unix)]
295+
fn _unsetenv(n: &str) {
296+
unsafe {
297+
do with_env_lock {
298+
do str::as_c_str(n) |nbuf| {
299+
libc::funcs::posix01::unistd::unsetenv(nbuf);
300+
}
301+
}
302+
}
303+
}
304+
#[cfg(windows)]
305+
fn _unsetenv(n: &str) {
306+
unsafe {
307+
do with_env_lock {
308+
use os::win32::as_utf16_p;
309+
do as_utf16_p(n) |nbuf| {
310+
libc::SetEnvironmentVariableW(nbuf, ptr::null());
311+
}
312+
}
313+
}
314+
}
315+
316+
_unsetenv(n);
317+
}
318+
292319
pub fn fdopen(fd: c_int) -> *FILE {
293320
unsafe {
294321
return do as_c_charp("r") |modebuf| {
@@ -1412,7 +1439,7 @@ mod tests {
14121439
use option::Some;
14131440
use option;
14141441
use os::{as_c_charp, env, getcwd, getenv, make_absolute, real_args};
1415-
use os::{remove_file, setenv};
1442+
use os::{remove_file, setenv, unsetenv};
14161443
use os;
14171444
use path::Path;
14181445
use rand::RngUtil;
@@ -1448,6 +1475,14 @@ mod tests {
14481475
assert!(getenv(n) == option::Some(~"VALUE"));
14491476
}
14501477
1478+
#[test]
1479+
fn test_unsetenv() {
1480+
let n = make_rand_name();
1481+
setenv(n, ~"VALUE");
1482+
unsetenv(n);
1483+
assert!(getenv(n) == option::None);
1484+
}
1485+
14511486
#[test]
14521487
#[ignore(cfg(windows))]
14531488
#[ignore]

0 commit comments

Comments
 (0)