use cfg_if::cfg_if;
use std::fmt;
#[derive(Clone, Copy, Debug)]
pub struct ClearEnvError;
impl fmt::Display for ClearEnvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "clearenv failed")
}
}
impl std::error::Error for ClearEnvError {}
pub unsafe fn clearenv() -> std::result::Result<(), ClearEnvError> {
let ret;
cfg_if! {
if #[cfg(any(target_os = "fuchsia",
target_os = "wasi",
target_env = "wasi",
target_env = "uclibc",
target_os = "linux",
target_os = "android",
target_os = "emscripten"))] {
ret = libc::clearenv();
} else {
use std::env;
for (name, _) in env::vars_os() {
env::remove_var(name);
}
ret = 0;
}
}
if ret == 0 {
Ok(())
} else {
Err(ClearEnvError)
}
}