1 use crate::collections::HashMap;
2 use crate::error::Error as StdError;
3 use crate::ffi::{CStr, OsStr, OsString};
6 use crate::marker::PhantomData;
8 use crate::path::{self, PathBuf};
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::*;
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>(&'a Void);
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)>,
113 _dont_send_or_sync_me: PhantomData<*mut ()>,
116 impl Iterator for Env {
117 type Item = (OsString, OsString);
118 fn next(&mut self) -> Option<(OsString, OsString)> {
121 fn size_hint(&self) -> (usize, Option<usize>) {
122 self.iter.size_hint()
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 {
130 let guard = ENV.as_ref().unwrap().lock().unwrap();
131 let mut result = Vec::new();
133 for (key, value) in guard.iter() {
134 result.push((key.clone(), value.clone()));
137 return Env { iter: result.into_iter(), _dont_send_or_sync_me: PhantomData };
141 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
143 match ENV.as_ref().unwrap().lock().unwrap().get_mut(k) {
144 Some(value) => Ok(Some(value.clone())),
150 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
152 let (k, v) = (k.to_owned(), v.to_owned());
153 ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
158 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
160 ENV.as_ref().unwrap().lock().unwrap().remove(k);
165 pub fn temp_dir() -> PathBuf {
166 panic!("no filesystem on hermit")
169 pub fn home_dir() -> Option<PathBuf> {
173 pub fn exit(code: i32) -> ! {
179 pub fn getpid() -> u32 {
180 unsafe { abi::getpid() }