safa_api/syscalls/
process_misc.rs

1use crate::syscalls::types::{OptionalPtrMut, RequiredPtrMut};
2
3use super::{define_syscall, err_from_u16, SyscallNum};
4
5#[cfg(not(feature = "rustc-dep-of-std"))]
6extern crate alloc;
7
8define_syscall! {
9    SyscallNum::SysPCHDir => {
10        /// Changes the current working directory to the path `buf` with length `buf_len`
11        /// (expects given buffer to be utf-8)
12        syschdir(buf: Str)
13    },
14    SyscallNum::SysPGetCWD => {
15        /// Gets the current working directory and puts it in `cwd_buf` with length `cwd_buf_len`
16        /// if `dest_len` is not null, it will be set to the length of the cwd
17        /// if the cwd is too long to fit in `cwd_buf`, the syscall will return [`ErrorStatus::Generic`] (1)
18        /// the cwd is currently maximumally 1024 bytes
19        sysgetcwd(cwd: Slice<u8>, dest_len: OptionalPtrMut<usize>)
20    }
21}
22
23#[inline]
24/// Changes the current work dir to `path`
25pub fn chdir(path: &str) -> Result<(), ErrorStatus> {
26    err_from_u16!(syschdir(Str::from_str(path)))
27}
28
29use alloc::string::String;
30use alloc::vec::Vec;
31use safa_abi::errors::ErrorStatus;
32use safa_abi::ffi::slice::Slice;
33use safa_abi::ffi::str::Str;
34
35#[inline]
36/// Retrieves the current work dir
37pub fn getcwd() -> Result<String, ErrorStatus> {
38    let mut buffer = Vec::with_capacity(safa_abi::consts::MAX_PATH_LENGTH);
39    unsafe {
40        buffer.set_len(buffer.capacity());
41    }
42
43    let mut dest_len: usize = 0xAAAAAAAAAAAAAAAAusize;
44    let ptr = RequiredPtrMut::new(&raw mut dest_len).into();
45    let len = err_from_u16!(sysgetcwd(Slice::from_slice(&buffer), ptr), dest_len)?;
46
47    buffer.truncate(len);
48    unsafe { Ok(String::from_utf8_unchecked(buffer)) }
49}