1 #![deny(unsafe_op_in_unsafe_fn)]
3 use crate::collections::HashMap;
4 use crate::error::Error as StdError;
5 use crate::ffi::{CStr, OsStr, OsString};
8 use crate::marker::PhantomData;
10 use crate::path::{self, PathBuf};
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::*;
18 pub fn errno() -> i32 {
22 pub fn error_string(_errno: i32) -> String {
23 "operation successful".to_string()
26 pub fn getcwd() -> io::Result<PathBuf> {
30 pub fn chdir(_: &path::Path) -> io::Result<()> {
34 pub struct SplitPaths<'a>(&'a Void);
36 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
40 impl<'a> Iterator for SplitPaths<'a> {
42 fn next(&mut self) -> Option<PathBuf> {
48 pub struct JoinPathsError;
50 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
52 I: Iterator<Item = T>,
58 impl fmt::Display for JoinPathsError {
59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60 "not supported on hermit yet".fmt(f)
64 impl StdError for JoinPathsError {
66 fn description(&self) -> &str {
67 "not supported on hermit yet"
71 pub fn current_exe() -> io::Result<PathBuf> {
75 static mut ENV: Option<Mutex<HashMap<OsString, OsString>>> = None;
77 pub fn init_environment(env: *const *const i8) {
79 ENV = Some(Mutex::new(HashMap::new()));
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);
91 environ = environ.add(1);
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
100 if input.is_empty() {
103 let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
106 OsStringExt::from_vec(input[..p].to_vec()),
107 OsStringExt::from_vec(input[p + 1..].to_vec()),
114 iter: vec::IntoIter<(OsString, OsString)>,
115 _dont_send_or_sync_me: PhantomData<*mut ()>,
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(), _dont_send_or_sync_me: PhantomData };
143 pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
145 match ENV.as_ref().unwrap().lock().unwrap().get_mut(k) {
146 Some(value) => Ok(Some(value.clone())),
152 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
154 let (k, v) = (k.to_owned(), v.to_owned());
155 ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
160 pub fn unsetenv(k: &OsStr) -> io::Result<()> {
162 ENV.as_ref().unwrap().lock().unwrap().remove(k);
167 pub fn temp_dir() -> PathBuf {
168 panic!("no filesystem on hermit")
171 pub fn home_dir() -> Option<PathBuf> {
175 pub fn exit(code: i32) -> ! {
181 pub fn getpid() -> u32 {
182 unsafe { abi::getpid() }