Skip to content

Commit 803e866

Browse files
authored
Merge pull request #260 from numpy1314/test
2 parents 3222a37 + e3954e1 commit 803e866

File tree

1 file changed

+20
-18
lines changed
  • api/arceos_posix_api/src/imp

1 file changed

+20
-18
lines changed

api/arceos_posix_api/src/imp/io.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::ctypes;
2-
use axerrno::LinuxError;
2+
use axerrno::{LinuxError, LinuxResult};
33
use core::ffi::{c_int, c_void};
44

55
#[cfg(feature = "fd")]
@@ -30,27 +30,29 @@ pub fn sys_read(fd: c_int, buf: *mut c_void, count: usize) -> ctypes::ssize_t {
3030
})
3131
}
3232

33+
fn write_impl(fd: c_int, buf: *const c_void, count: usize) -> LinuxResult<ctypes::ssize_t> {
34+
if buf.is_null() {
35+
return Err(LinuxError::EFAULT);
36+
}
37+
let src = unsafe { core::slice::from_raw_parts(buf as *const u8, count) };
38+
#[cfg(feature = "fd")]
39+
{
40+
Ok(get_file_like(fd)?.write(src)? as ctypes::ssize_t)
41+
}
42+
#[cfg(not(feature = "fd"))]
43+
match fd {
44+
0 => Err(LinuxError::EPERM),
45+
1 | 2 => Ok(super::stdio::stdout().write(src)? as ctypes::ssize_t),
46+
_ => Err(LinuxError::EBADF),
47+
}
48+
}
49+
3350
/// Write data to the file indicated by `fd`.
3451
///
3552
/// Return the written size if success.
3653
pub fn sys_write(fd: c_int, buf: *const c_void, count: usize) -> ctypes::ssize_t {
3754
debug!("sys_write <= {} {:#x} {}", fd, buf as usize, count);
38-
syscall_body!(sys_write, {
39-
if buf.is_null() {
40-
return Err(LinuxError::EFAULT);
41-
}
42-
let src = unsafe { core::slice::from_raw_parts(buf as *const u8, count) };
43-
#[cfg(feature = "fd")]
44-
{
45-
Ok(get_file_like(fd)?.write(src)? as ctypes::ssize_t)
46-
}
47-
#[cfg(not(feature = "fd"))]
48-
match fd {
49-
0 => Err(LinuxError::EPERM),
50-
1 | 2 => Ok(super::stdio::stdout().write(src)? as ctypes::ssize_t),
51-
_ => Err(LinuxError::EBADF),
52-
}
53-
})
55+
syscall_body!(sys_write, write_impl(fd, buf, count))
5456
}
5557

5658
/// Write a vector.
@@ -64,7 +66,7 @@ pub unsafe fn sys_writev(fd: c_int, iov: *const ctypes::iovec, iocnt: c_int) ->
6466
let iovs = unsafe { core::slice::from_raw_parts(iov, iocnt as usize) };
6567
let mut ret = 0;
6668
for iov in iovs.iter() {
67-
ret += sys_write(fd, iov.iov_base, iov.iov_len);
69+
ret += write_impl(fd, iov.iov_base, iov.iov_len)?;
6870
}
6971

7072
Ok(ret)

0 commit comments

Comments
 (0)