1 use crate::collections::HashMap;
2 use crate::error::Error as StdError;
3 use crate::ffi::{CStr, OsStr, OsString};
6 use crate::marker::PhantomData;
7 use crate::os::unix::ffi::OsStringExt;
8 use crate::path::{self, PathBuf};
10 use crate::sync::Mutex;
11 use crate::sys::hermit::abi;
12 use crate::sys::memchr;
13 use crate::sys::unsupported;
16 pub fn errno() -> i32 {
20 pub fn error_string(_errno: i32) -> String {
21 "operation successful".to_string()
24 pub fn getcwd() -> io::Result<PathBuf> {
28 pub fn chdir(_: &path::Path) -> io::Result<()> {
32 pub struct SplitPaths<'a>(!, PhantomData<&'a ()>);
34 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
38 impl<'a> Iterator for SplitPaths<'a> {
40 fn next(&mut self) -> Option<PathBuf> {
46 pub struct JoinPathsError;
48 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
50 I: Iterator<Item = T>,
56 impl fmt::Display for JoinPathsError {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 "not supported on hermit yet".fmt(f)
62 impl StdError for JoinPathsError {
64 fn description(&self) -> &str {
65 "not supported on hermit yet"
69 pub fn current_exe() -> io::Result<PathBuf> {
73 static mut ENV: Option<Mutex<HashMap<OsString, OsString>>> = None;
75 pub fn init_environment(env: *const *const i8) {
77 ENV = Some(Mutex::new(HashMap::new()));
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);
89 environ = environ.add(1);
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
101 let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
104 OsStringExt::from_vec(input[..p].to_vec()),
105 OsStringExt::from_vec(input[p + 1..].to_vec()),
112 iter: vec::IntoIter<(OsString, OsString)>,
115 impl !Send for Env {}
116 impl !Sync for Env {}
118 impl Iterator for Env {
119 type Item = (OsString, OsString);
120 fn next(&mut self) -> Option<(OsString, OsString)> {
123 fn size_hint(&self) -> (usize, Option<usize>) {
124 self.iter.size_hint()
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 {
132 let guard = ENV.as_ref().unwrap().lock().unwrap();
133 let mut result = Vec::new();
135 for (key, value) in guard.iter() {
136 result.push((key.clone(), value.clone()));
139 return Env { iter: result.into_iter() };
143 pub fn getenv(k: &OsStr) -> Option<OsString> {
144 unsafe { ENV.as_ref().unwrap().lock().unwrap().get_mut(k).cloned() }
147 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
149 let (k, v) = (k.to_owned(), v.to_owned());
150 ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
155 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
157 ENV.as_ref().unwrap().lock().unwrap().remove(k);
162 pub fn temp_dir() -> PathBuf {
163 panic!("no filesystem on hermit")
166 pub fn home_dir() -> Option<PathBuf> {
170 pub fn exit(code: i32) -> ! {
176 pub fn getpid() -> u32 {
177 unsafe { abi::getpid() }