1 use super::unsupported;
2 use crate::error::Error as StdError;
3 use crate::ffi::{CStr, OsStr, OsString};
8 solid::ffi::{OsStrExt, OsStringExt},
10 use crate::path::{self, PathBuf};
11 use crate::sync::RwLock;
12 use crate::sys::common::small_c_string::run_with_cstr;
15 use super::{error, itron, memchr};
17 // `solid` directly maps `errno`s to μITRON error codes.
18 impl itron::error::ItronError {
20 pub(crate) fn as_io_error(self) -> crate::io::Error {
21 crate::io::Error::from_raw_os_error(self.as_raw())
25 pub fn errno() -> i32 {
29 pub fn error_string(errno: i32) -> String {
30 if let Some(name) = error::error_name(errno) { name.to_owned() } else { format!("{errno}") }
33 pub fn getcwd() -> io::Result<PathBuf> {
37 pub fn chdir(_: &path::Path) -> io::Result<()> {
41 pub struct SplitPaths<'a>(&'a !);
43 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
47 impl<'a> Iterator for SplitPaths<'a> {
49 fn next(&mut self) -> Option<PathBuf> {
55 pub struct JoinPathsError;
57 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
59 I: Iterator<Item = T>,
65 impl fmt::Display for JoinPathsError {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 "not supported on this platform yet".fmt(f)
71 impl StdError for JoinPathsError {
73 fn description(&self) -> &str {
74 "not supported on this platform yet"
78 pub fn current_exe() -> io::Result<PathBuf> {
82 static ENV_LOCK: RwLock<()> = RwLock::new(());
85 iter: vec::IntoIter<(OsString, OsString)>,
91 impl Iterator for Env {
92 type Item = (OsString, OsString);
93 fn next(&mut self) -> Option<(OsString, OsString)> {
96 fn size_hint(&self) -> (usize, Option<usize>) {
101 /// Returns a vector of (variable, value) byte-vector pairs for all the
102 /// environment variables of the current process.
103 pub fn env() -> Env {
105 static mut environ: *const *const c_char;
109 let _guard = ENV_LOCK.read();
110 let mut result = Vec::new();
111 if !environ.is_null() {
112 while !(*environ).is_null() {
113 if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
114 result.push(key_value);
116 environ = environ.add(1);
119 return Env { iter: result.into_iter() };
122 fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
123 // Strategy (copied from glibc): Variable name and value are separated
124 // by an ASCII equals sign '='. Since a variable name must not be
125 // empty, allow variable names starting with an equals sign. Skip all
127 if input.is_empty() {
130 let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
133 OsStringExt::from_vec(input[..p].to_vec()),
134 OsStringExt::from_vec(input[p + 1..].to_vec()),
140 pub fn getenv(k: &OsStr) -> Option<OsString> {
141 // environment variables with a nul byte can't be set, so their value is
142 // always None as well
143 let s = run_with_cstr(k.as_bytes(), |k| {
144 let _guard = ENV_LOCK.read();
145 Ok(unsafe { libc::getenv(k.as_ptr()) } as *const libc::c_char)
152 Some(OsStringExt::from_vec(unsafe { CStr::from_ptr(s) }.to_bytes().to_vec()))
156 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
157 run_with_cstr(k.as_bytes(), |k| {
158 run_with_cstr(v.as_bytes(), |v| {
159 let _guard = ENV_LOCK.write();
160 cvt_env(unsafe { libc::setenv(k.as_ptr(), v.as_ptr(), 1) }).map(drop)
165 pub fn unsetenv(n: &OsStr) -> io::Result<()> {
166 run_with_cstr(n.as_bytes(), |nbuf| {
167 let _guard = ENV_LOCK.write();
168 cvt_env(unsafe { libc::unsetenv(nbuf.as_ptr()) }).map(drop)
172 /// In kmclib, `setenv` and `unsetenv` don't always set `errno`, so this
173 /// function just returns a generic error.
174 fn cvt_env(t: c_int) -> io::Result<c_int> {
175 if t == -1 { Err(io::const_io_error!(io::ErrorKind::Uncategorized, "failure")) } else { Ok(t) }
178 pub fn temp_dir() -> PathBuf {
179 panic!("no standard temporary directory on this platform")
182 pub fn home_dir() -> Option<PathBuf> {
186 pub fn exit(code: i32) -> ! {
187 rtabort!("exit({}) called", code);
190 pub fn getpid() -> u32 {
191 panic!("no pids on this platform")