]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/wasi/os.rs
libstd: deny(elided_lifetimes_in_paths), fixes in wasi
[rust.git] / src / libstd / sys / wasi / os.rs
1 use crate::any::Any;
2 use crate::error::Error as StdError;
3 use crate::ffi::{OsString, OsStr, CString, CStr};
4 use crate::fmt;
5 use crate::io;
6 use crate::marker::PhantomData;
7 use crate::os::wasi::prelude::*;
8 use crate::path::{self, PathBuf};
9 use crate::ptr;
10 use crate::str;
11 use crate::sys::memchr;
12 use crate::sys::{cvt, unsupported, Void};
13 use crate::vec;
14
15 #[cfg(not(target_feature = "atomics"))]
16 pub unsafe fn env_lock() -> impl Any {
17     // No need for a lock if we're single-threaded, but this function will need
18     // to get implemented for multi-threaded scenarios
19 }
20
21 pub fn errno() -> i32 {
22     extern {
23         #[thread_local]
24         static errno: libc::c_int;
25     }
26
27     unsafe { errno as i32 }
28 }
29
30 pub fn error_string(_errno: i32) -> String {
31     "operation failed".to_string()
32 }
33
34 pub fn getcwd() -> io::Result<PathBuf> {
35     unsupported()
36 }
37
38 pub fn chdir(_: &path::Path) -> io::Result<()> {
39     unsupported()
40 }
41
42 pub struct SplitPaths<'a>(&'a Void);
43
44 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
45     panic!("unsupported")
46 }
47
48 impl<'a> Iterator for SplitPaths<'a> {
49     type Item = PathBuf;
50     fn next(&mut self) -> Option<PathBuf> {
51         match *self.0 {}
52     }
53 }
54
55 #[derive(Debug)]
56 pub struct JoinPathsError;
57
58 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
59     where I: Iterator<Item=T>, T: AsRef<OsStr>
60 {
61     Err(JoinPathsError)
62 }
63
64 impl fmt::Display for JoinPathsError {
65     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66         "not supported on wasm yet".fmt(f)
67     }
68 }
69
70 impl StdError for JoinPathsError {
71     fn description(&self) -> &str {
72         "not supported on wasm yet"
73     }
74 }
75
76 pub fn current_exe() -> io::Result<PathBuf> {
77     unsupported()
78 }
79
80 pub struct Env {
81     iter: vec::IntoIter<(OsString, OsString)>,
82     _dont_send_or_sync_me: PhantomData<*mut ()>,
83 }
84
85 impl Iterator for Env {
86     type Item = (OsString, OsString);
87     fn next(&mut self) -> Option<(OsString, OsString)> { self.iter.next() }
88     fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
89 }
90
91
92 pub fn env() -> Env {
93     unsafe {
94         let _guard = env_lock();
95         let mut environ = libc::environ;
96         let mut result = Vec::new();
97         while environ != ptr::null_mut() && *environ != ptr::null_mut() {
98             if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
99                 result.push(key_value);
100             }
101             environ = environ.offset(1);
102         }
103         return Env {
104             iter: result.into_iter(),
105             _dont_send_or_sync_me: PhantomData,
106         }
107     }
108
109     // See src/libstd/sys/unix/os.rs, same as that
110     fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
111         if input.is_empty() {
112             return None;
113         }
114         let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
115         pos.map(|p| (
116             OsStringExt::from_vec(input[..p].to_vec()),
117             OsStringExt::from_vec(input[p+1..].to_vec()),
118         ))
119     }
120 }
121
122 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
123     let k = CString::new(k.as_bytes())?;
124     unsafe {
125         let _guard = env_lock();
126         let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
127         let ret = if s.is_null() {
128             None
129         } else {
130             Some(OsStringExt::from_vec(CStr::from_ptr(s).to_bytes().to_vec()))
131         };
132         Ok(ret)
133     }
134 }
135
136 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
137     let k = CString::new(k.as_bytes())?;
138     let v = CString::new(v.as_bytes())?;
139
140     unsafe {
141         let _guard = env_lock();
142         cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(|_| ())
143     }
144 }
145
146 pub fn unsetenv(n: &OsStr) -> io::Result<()> {
147     let nbuf = CString::new(n.as_bytes())?;
148
149     unsafe {
150         let _guard = env_lock();
151         cvt(libc::unsetenv(nbuf.as_ptr())).map(|_| ())
152     }
153 }
154
155 pub fn temp_dir() -> PathBuf {
156     panic!("no filesystem on wasm")
157 }
158
159 pub fn home_dir() -> Option<PathBuf> {
160     None
161 }
162
163 pub fn exit(code: i32) -> ! {
164     unsafe {
165         libc::exit(code)
166     }
167 }
168
169 pub fn getpid() -> u32 {
170     panic!("unsupported");
171 }