use crate::ctypes;
use axerrno::LinuxError;
use core::ffi::{c_int, c_void};
#[cfg(feature = "fd")]
use crate::imp::fd_ops::get_file_like;
#[cfg(not(feature = "fd"))]
use axio::prelude::*;
pub fn sys_read(fd: c_int, buf: *mut c_void, count: usize) -> ctypes::ssize_t {
debug!("sys_read <= {} {:#x} {}", fd, buf as usize, count);
syscall_body!(sys_read, {
if buf.is_null() {
return Err(LinuxError::EFAULT);
}
let dst = unsafe { core::slice::from_raw_parts_mut(buf as *mut u8, count) };
#[cfg(feature = "fd")]
{
Ok(get_file_like(fd)?.read(dst)? as ctypes::ssize_t)
}
#[cfg(not(feature = "fd"))]
match fd {
0 => Ok(super::stdio::stdin().read(dst)? as ctypes::ssize_t),
1 | 2 => Err(LinuxError::EPERM),
_ => Err(LinuxError::EBADF),
}
})
}
pub fn sys_write(fd: c_int, buf: *const c_void, count: usize) -> ctypes::ssize_t {
debug!("sys_write <= {} {:#x} {}", fd, buf as usize, count);
syscall_body!(sys_write, {
if buf.is_null() {
return Err(LinuxError::EFAULT);
}
let src = unsafe { core::slice::from_raw_parts(buf as *const u8, count) };
#[cfg(feature = "fd")]
{
Ok(get_file_like(fd)?.write(src)? as ctypes::ssize_t)
}
#[cfg(not(feature = "fd"))]
match fd {
0 => Err(LinuxError::EPERM),
1 | 2 => Ok(super::stdio::stdout().write(src)? as ctypes::ssize_t),
_ => Err(LinuxError::EBADF),
}
})
}
pub unsafe fn sys_writev(fd: c_int, iov: *const ctypes::iovec, iocnt: c_int) -> ctypes::ssize_t {
debug!("sys_writev <= fd: {}", fd);
syscall_body!(sys_writev, {
if !(0..=1024).contains(&iocnt) {
return Err(LinuxError::EINVAL);
}
let iovs = unsafe { core::slice::from_raw_parts(iov, iocnt as usize) };
let mut ret = 0;
for iov in iovs.iter() {
ret += sys_write(fd, iov.iov_base, iov.iov_len);
}
Ok(ret)
})
}