]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/hermit/os.rs
New upstream version 1.53.0+dfsg1
[rustc.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;
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>(!, PhantomData<&'a ()>);
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 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 }
114
115 impl !Send for Env {}
116 impl !Sync for Env {}
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() };
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 }