//! File and filesystem-related syscalls

use crate::mm::page_table::translated_byte_buffer;
use crate::print;
use crate::task::TASK_MANAGER;

const FD_STDOUT: usize = 1;

/// write buf of length `len`  to a file with `fd`
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
    match fd {
        FD_STDOUT => {
            // 根据当前页表 找到物理内存基地址
            // 这里需要从应用的地址空间里面寻找数据
            let buffers = translated_byte_buffer(TASK_MANAGER.get_current_token(), buf, len);
            for buffer in buffers {
                print!("{}", core::str::from_utf8(buffer).unwrap());
            }
            len as isize
        }
        _ => {
            panic!("Unsupported fd in sys_write!");
        }
    }
}
