]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/hermit/os.rs
Merge commit '70c0f90453701e7d6d9b99aaa1fc6a765937b736' into clippyup
[rust.git] / library / std / src / sys / hermit / os.rs
1 use crate::collections::HashMap;
2 use crate::error::Error as StdError;
3 use crate::ffi::{CStr, OsStr, OsString};
4 use crate::fmt;
5 use crate::io;
6 use crate::marker::PhantomData;
7 use crate::memchr;
8 use crate::path::{self, PathBuf};
9 use crate::str;
10 use crate::sync::Mutex;
11 use crate::sys::hermit::abi;
12 use crate::sys::{unsupported, Void};
13 use crate::sys_common::os_str_bytes::*;
14 use crate::vec;
15
16 pub fn errno() -> i32 {
17     0
18 }
19
20 pub fn error_string(_errno: i32) -> String {
21     "operation successful".to_string()
22 }
23
24 pub fn getcwd() -> io::Result<PathBuf> {
25     unsupported()
26 }
27
28 pub fn chdir(_: &path::Path) -> io::Result<()> {
29     unsupported()
30 }
31
32 pub struct SplitPaths<'a>(&'a Void);
33
34 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
35     panic!("unsupported")
36 }
37
38 impl<'a> Iterator for SplitPaths<'a> {
39     type Item = PathBuf;
40     fn next(&mut self) -> Option<PathBuf> {
41         match *self.0 {}
42     }
43 }
44
45 #[derive(Debug)]
46 pub struct JoinPathsError;
47
48 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
49 where
50     I: Iterator<Item = T>,
51     T: AsRef<OsStr>,
52 {
53     Err(JoinPathsError)
54 }
55
56 impl fmt::Display for JoinPathsError {
57     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58         "not supported on hermit yet".fmt(f)
59     }
60 }
61
62 impl StdError for JoinPathsError {
63     #[allow(deprecated)]
64     fn description(&self) -> &str {
65         "not supported on hermit yet"
66     }
67 }
68
69 pub fn current_exe() -> io::Result<PathBuf> {
70     unsupported()
71 }
72
73 static mut ENV: Option<Mutex<HashMap<OsString, OsString>>> = None;
74
75 pub fn init_environment(env: *const *const i8) {
76     unsafe {
77         ENV = Some(Mutex::new(HashMap::new()));
78
79         if env.is_null() {
80             return;
81         }
82
83         let mut guard = ENV.as_ref().unwrap().lock().unwrap();
84         let mut environ = env;
85         while !(*environ).is_null() {
86             if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {
87                 guard.insert(key, value);
88             }
89             environ = environ.add(1);
90         }
91     }
92
93     fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
94         // Strategy (copied from glibc): Variable name and value are separated
95         // by an ASCII equals sign '='. Since a variable name must not be
96         // empty, allow variable names starting with an equals sign. Skip all
97         // malformed lines.
98         if input.is_empty() {
99             return None;
100         }
101         let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
102         pos.map(|p| {
103             (
104                 OsStringExt::from_vec(input[..p].to_vec()),
105                 OsStringExt::from_vec(input[p + 1..].to_vec()),
106             )
107         })
108     }
109 }
110
111 pub struct Env {
112     iter: vec::IntoIter<(OsString, OsString)>,
113     _dont_send_or_sync_me: PhantomData<*mut ()>,
114 }
115
116 impl Iterator for Env {
117     type Item = (OsString, OsString);
118     fn next(&mut self) -> Option<(OsString, OsString)> {
119         self.iter.next()
120     }
121     fn size_hint(&self) -> (usize, Option<usize>) {
122         self.iter.size_hint()
123     }
124 }
125
126 /// Returns a vector of (variable, value) byte-vector pairs for all the
127 /// environment variables of the current process.
128 pub fn env() -> Env {
129     unsafe {
130         let guard = ENV.as_ref().unwrap().lock().unwrap();
131         let mut result = Vec::new();
132
133         for (key, value) in guard.iter() {
134             result.push((key.clone(), value.clone()));
135         }
136
137         return Env { iter: result.into_iter(), _dont_send_or_sync_me: PhantomData };
138     }
139 }
140
141 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
142     unsafe {
143         match ENV.as_ref().unwrap().lock().unwrap().get_mut(k) {
144             Some(value) => Ok(Some(value.clone())),
145             None => Ok(None),
146         }
147     }
148 }
149
150 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
151     unsafe {
152         let (k, v) = (k.to_owned(), v.to_owned());
153         ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
154     }
155     Ok(())
156 }
157
158 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
159     unsafe {
160         ENV.as_ref().unwrap().lock().unwrap().remove(k);
161     }
162     Ok(())
163 }
164
165 pub fn temp_dir() -> PathBuf {
166     panic!("no filesystem on hermit")
167 }
168
169 pub fn home_dir() -> Option<PathBuf> {
170     None
171 }
172
173 pub fn exit(code: i32) -> ! {
174     unsafe {
175         abi::exit(code);
176     }
177 }
178
179 pub fn getpid() -> u32 {
180     unsafe { abi::getpid() }
181 }