1 use super::unsupported
;
2 use crate::error
::Error
as StdError
;
3 use crate::ffi
::{CStr, CString, OsStr, OsString}
;
8 solid
::ffi
::{OsStrExt, OsStringExt}
,
10 use crate::path
::{self, PathBuf}
;
11 use crate::sys_common
::rwlock
::StaticRwLock
;
14 use super::{error, itron, memchr}
;
16 // `solid` directly maps `errno`s to μITRON error codes.
17 impl itron
::error
::ItronError
{
19 pub(crate) fn as_io_error(self) -> crate::io
::Error
{
20 crate::io
::Error
::from_raw_os_error(self.as_raw())
24 pub fn errno() -> i32 {
28 pub fn error_string(errno
: i32) -> String
{
29 if let Some(name
) = error
::error_name(errno
) { name.to_owned() }
else { format!("{errno}
") }
32 pub fn getcwd() -> io::Result<PathBuf> {
36 pub fn chdir(_: &path::Path) -> io::Result<()> {
40 pub struct SplitPaths<'a>(&'a !);
42 pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
46 impl<'a> Iterator for SplitPaths<'a> {
48 fn next(&mut self) -> Option<PathBuf> {
54 pub struct JoinPathsError;
56 pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
58 I: Iterator<Item = T>,
64 impl fmt::Display for JoinPathsError {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 "not supported on this platform yet
".fmt(f)
70 impl StdError for JoinPathsError {
72 fn description(&self) -> &str {
73 "not supported on this platform yet
"
77 pub fn current_exe() -> io::Result<PathBuf> {
81 static ENV_LOCK: StaticRwLock = StaticRwLock::new();
84 iter: vec::IntoIter<(OsString, OsString)>,
90 impl Iterator for Env {
91 type Item = (OsString, OsString);
92 fn next(&mut self) -> Option<(OsString, OsString)> {
95 fn size_hint(&self) -> (usize, Option<usize>) {
100 /// Returns a vector of (variable, value) byte-vector pairs for all the
101 /// environment variables of the current process.
102 pub fn env() -> Env {
104 static mut environ: *const *const c_char;
108 let _guard = ENV_LOCK.read();
109 let mut result = Vec::new();
110 if !environ.is_null() {
111 while !(*environ).is_null() {
112 if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
113 result.push(key_value);
115 environ = environ.add(1);
118 return Env { iter: result.into_iter() };
121 fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
122 // Strategy (copied from glibc): Variable name and value are separated
123 // by an ASCII equals sign '='. Since a variable name must not be
124 // empty, allow variable names starting with an equals sign. Skip all
126 if input.is_empty() {
129 let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
132 OsStringExt::from_vec(input[..p].to_vec()),
133 OsStringExt::from_vec(input[p + 1..].to_vec()),
139 pub fn getenv(k: &OsStr) -> Option<OsString> {
140 // environment variables with a nul byte can't be set, so their value is
141 // always None as well
142 let k = CString::new(k.as_bytes()).ok()?;
144 let _guard = ENV_LOCK.read();
145 let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
149 Some(OsStringExt::from_vec(CStr::from_ptr(s).to_bytes().to_vec()))
154 pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
155 let k = CString::new(k.as_bytes())?;
156 let v = CString::new(v.as_bytes())?;
159 let _guard = ENV_LOCK.write();
160 cvt_env(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop)
164 pub fn unsetenv(n: &OsStr) -> io::Result<()> {
165 let nbuf = CString::new(n.as_bytes())?;
168 let _guard = ENV_LOCK.write();
169 cvt_env(libc::unsetenv(nbuf.as_ptr())).map(drop)
173 /// In kmclib, `setenv` and `unsetenv` don't always set `errno`, so this
174 /// function just returns a generic error.
175 fn cvt_env(t: c_int) -> io::Result<c_int> {
176 if t == -1 { Err(io::const_io_error!(io::ErrorKind::Uncategorized, "failure")) } else { Ok(t) }
179 pub fn temp_dir() -> PathBuf {
180 panic!("no standard temporary directory on this platform
")
183 pub fn home_dir() -> Option<PathBuf> {
187 pub fn exit(code: i32) -> ! {
188 rtabort!("exit({}
) called
", code);
191 pub fn getpid() -> u32 {
192 panic!("no pids on this platform
")