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