#[cfg(not(target_os = "fuchsia"))]
use crate::backend::fd::AsFd;
#[cfg(feature = "fs")]
use crate::path;
#[cfg(any(feature = "fs", not(target_os = "fuchsia")))]
use crate::{backend, io};
#[cfg(all(feature = "alloc", feature = "fs"))]
use {
crate::ffi::{CStr, CString},
crate::path::SMALL_PATH_BUFFER_SIZE,
alloc::vec::Vec,
};
#[inline]
#[cfg(feature = "fs")]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub fn chdir<P: path::Arg>(path: P) -> io::Result<()> {
path.into_with_c_str(backend::process::syscalls::chdir)
}
#[cfg(not(target_os = "fuchsia"))]
#[inline]
pub fn fchdir<Fd: AsFd>(fd: Fd) -> io::Result<()> {
backend::process::syscalls::fchdir(fd.as_fd())
}
#[cfg(all(feature = "alloc", feature = "fs"))]
#[cfg(not(target_os = "wasi"))]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[inline]
pub fn getcwd<B: Into<Vec<u8>>>(reuse: B) -> io::Result<CString> {
_getcwd(reuse.into())
}
#[cfg(all(feature = "alloc", feature = "fs"))]
#[allow(unsafe_code)]
fn _getcwd(mut buffer: Vec<u8>) -> io::Result<CString> {
buffer.clear();
buffer.reserve(SMALL_PATH_BUFFER_SIZE);
loop {
match backend::process::syscalls::getcwd(buffer.spare_capacity_mut()) {
Err(io::Errno::RANGE) => {
buffer.reserve(buffer.capacity() + 1);
}
Ok(_) => {
unsafe {
buffer.set_len(
CStr::from_ptr(buffer.as_ptr().cast())
.to_bytes_with_nul()
.len(),
);
return Ok(CString::from_vec_with_nul_unchecked(buffer));
}
}
Err(errno) => return Err(errno),
}
}
}