about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCorey Richardson <corey@octayn.net>2013-05-15 13:39:53 -0400
committerCorey Richardson <corey@octayn.net>2013-05-15 13:40:35 -0400
commit6ef226d5d95ac2248178e85accc52d0161d76540 (patch)
treebee504c237ad53bbd16248d6bfede7988bca8724
parentd806c96ffb33acb35414a5743bca3c870d1f15c4 (diff)
downloadrust-6ef226d5d95ac2248178e85accc52d0161d76540.tar.gz
rust-6ef226d5d95ac2248178e85accc52d0161d76540.zip
Add unsetenv wrapper
-rw-r--r--src/libcore/os.rs37
1 files changed, 36 insertions, 1 deletions
diff --git a/src/libcore/os.rs b/src/libcore/os.rs
index daad9cee0fc..b97b32330de 100644
--- a/src/libcore/os.rs
+++ b/src/libcore/os.rs
@@ -289,6 +289,33 @@ pub fn setenv(n: &str, v: &str) {
     }
 }
 
+/// Remove a variable from the environment entirely
+pub fn unsetenv(n: &str) {
+    #[cfg(unix)]
+    fn _unsetenv(n: &str) {
+        unsafe {
+            do with_env_lock {
+                do str::as_c_str(n) |nbuf| {
+                    libc::funcs::posix01::unistd::unsetenv(nbuf);
+                }
+            }
+        }
+    }
+    #[cfg(windows)]
+    fn _unsetenv(n: &str) {
+        unsafe {
+            do with_env_lock {
+                use os::win32::as_utf16_p;
+                do as_utf16_p(n) |nbuf| {
+                    libc::SetEnvironmentVariableW(nbuf, ptr::null());
+                }
+            }
+        }
+    }
+
+    _unsetenv(n);
+}
+
 pub fn fdopen(fd: c_int) -> *FILE {
     unsafe {
         return do as_c_charp("r") |modebuf| {
@@ -1412,7 +1439,7 @@ mod tests {
     use option::Some;
     use option;
     use os::{as_c_charp, env, getcwd, getenv, make_absolute, real_args};
-    use os::{remove_file, setenv};
+    use os::{remove_file, setenv, unsetenv};
     use os;
     use path::Path;
     use rand::RngUtil;
@@ -1449,6 +1476,14 @@ mod tests {
     }
 
     #[test]
+    fn test_unsetenv() {
+        let n = make_rand_name();
+        setenv(n, ~"VALUE");
+        unsetenv(n);
+        assert!(getenv(n) == option::None);
+    }
+
+    #[test]
     #[ignore(cfg(windows))]
     #[ignore]
     fn test_setenv_overwrite() {