1 #![deny(unsafe_op_in_unsafe_fn)]
3 use crate::error::Error as StdError;
4 use crate::ffi::{CStr, OsStr, OsString};
7 use crate::marker::PhantomData;
9 use crate::os::wasi::prelude::*;
10 use crate::path::{self, PathBuf};
12 use crate::sys::common::small_c_string::{run_path_with_cstr, run_with_cstr};
13 use crate::sys::memchr;
14 use crate::sys::unsupported;
17 // Add a few symbols not in upstream `libc` just yet.
22 pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char;
23 pub fn chdir(dir: *const c_char) -> c_int;
28 if #[cfg(target_feature = "atomics")] {
29 // Access to the environment must be protected by a lock in multi-threaded scenarios.
30 use crate::sync::{PoisonError, RwLock};
31 static ENV_LOCK: RwLock<()> = RwLock::new(());
32 pub fn env_read_lock() -> impl Drop {
33 ENV_LOCK.read().unwrap_or_else(PoisonError::into_inner)
35 pub fn env_write_lock() -> impl Drop {
36 ENV_LOCK.write().unwrap_or_else(PoisonError::into_inner)
39 // No need for a lock if we are single-threaded.
40 pub fn env_read_lock() -> impl Drop {
43 pub fn env_write_lock() -> impl Drop {
49 pub fn errno() -> i32 {
52 static errno: libc::c_int;
55 unsafe { errno as i32 }
58 pub fn error_string(errno: i32) -> String {
59 let mut buf = [0 as libc::c_char; 1024];
61 let p = buf.as_mut_ptr();
63 if libc::strerror_r(errno as libc::c_int, p, buf.len()) < 0 {
64 panic!("strerror_r failure");
66 str::from_utf8(CStr::from_ptr(p).to_bytes()).unwrap().to_owned()
70 pub fn getcwd() -> io::Result<PathBuf> {
71 let mut buf = Vec::with_capacity(512);
74 let ptr = buf.as_mut_ptr() as *mut libc::c_char;
75 if !libc::getcwd(ptr, buf.capacity()).is_null() {
76 let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len();
79 return Ok(PathBuf::from(OsString::from_vec(buf)));
81 let error = io::Error::last_os_error();
82 if error.raw_os_error() != Some(libc::ERANGE) {
87 // Trigger the internal buffer resizing logic of `Vec` by requiring
88 // more space than the current capacity.
89 let cap = buf.capacity();
96 pub fn chdir(p: &path::Path) -> io::Result<()> {
97 let result = run_path_with_cstr(p, |p| unsafe { Ok(libc::chdir(p.as_ptr())) })?;
98 match result == (0 as libc::c_int) {
100 false => Err(io::Error::last_os_error()),
104 pub struct SplitPaths<'a>(!, PhantomData<&'a ()>);
106 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
107 panic!("unsupported")
110 impl<'a> Iterator for SplitPaths<'a> {
112 fn next(&mut self) -> Option<PathBuf> {
118 pub struct JoinPathsError;
120 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
122 I: Iterator<Item = T>,
128 impl fmt::Display for JoinPathsError {
129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130 "not supported on wasm yet".fmt(f)
134 impl StdError for JoinPathsError {
136 fn description(&self) -> &str {
137 "not supported on wasm yet"
141 pub fn current_exe() -> io::Result<PathBuf> {
145 iter: vec::IntoIter<(OsString, OsString)>,
148 impl !Send for Env {}
149 impl !Sync for Env {}
151 impl Iterator for Env {
152 type Item = (OsString, OsString);
153 fn next(&mut self) -> Option<(OsString, OsString)> {
156 fn size_hint(&self) -> (usize, Option<usize>) {
157 self.iter.size_hint()
161 pub fn env() -> Env {
163 let _guard = env_read_lock();
164 let mut environ = libc::environ;
165 let mut result = Vec::new();
166 if !environ.is_null() {
167 while !(*environ).is_null() {
168 if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
169 result.push(key_value);
171 environ = environ.add(1);
174 return Env { iter: result.into_iter() };
177 // See src/libstd/sys/unix/os.rs, same as that
178 fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
179 if input.is_empty() {
182 let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
185 OsStringExt::from_vec(input[..p].to_vec()),
186 OsStringExt::from_vec(input[p + 1..].to_vec()),
192 pub fn getenv(k: &OsStr) -> Option<OsString> {
193 let s = run_with_cstr(k.as_bytes(), |k| unsafe {
194 let _guard = env_read_lock();
195 Ok(libc::getenv(k.as_ptr()) as *const libc::c_char)
201 Some(OsStringExt::from_vec(unsafe { CStr::from_ptr(s) }.to_bytes().to_vec()))
205 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
206 run_with_cstr(k.as_bytes(), |k| {
207 run_with_cstr(v.as_bytes(), |v| unsafe {
208 let _guard = env_write_lock();
209 cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop)
214 pub fn unsetenv(n: &OsStr) -> io::Result<()> {
215 run_with_cstr(n.as_bytes(), |nbuf| unsafe {
216 let _guard = env_write_lock();
217 cvt(libc::unsetenv(nbuf.as_ptr())).map(drop)
221 pub fn temp_dir() -> PathBuf {
222 panic!("no filesystem on wasm")
225 pub fn home_dir() -> Option<PathBuf> {
229 pub fn exit(code: i32) -> ! {
230 unsafe { libc::exit(code) }
233 pub fn getpid() -> u32 {
234 panic!("unsupported");
238 pub trait IsMinusOne {
239 fn is_minus_one(&self) -> bool;
242 macro_rules! impl_is_minus_one {
243 ($($t:ident)*) => ($(impl IsMinusOne for $t {
244 fn is_minus_one(&self) -> bool {
250 impl_is_minus_one! { i8 i16 i32 i64 isize }
252 fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
253 if t.is_minus_one() { Err(io::Error::last_os_error()) } else { Ok(t) }