1
1
use crate :: ctypes;
2
- use axerrno:: LinuxError ;
2
+ use axerrno:: { LinuxError , LinuxResult } ;
3
3
use core:: ffi:: { c_int, c_void} ;
4
4
5
5
#[ cfg( feature = "fd" ) ]
@@ -30,27 +30,29 @@ pub fn sys_read(fd: c_int, buf: *mut c_void, count: usize) -> ctypes::ssize_t {
30
30
} )
31
31
}
32
32
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
+
33
50
/// Write data to the file indicated by `fd`.
34
51
///
35
52
/// Return the written size if success.
36
53
pub fn sys_write ( fd : c_int , buf : * const c_void , count : usize ) -> ctypes:: ssize_t {
37
54
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) )
54
56
}
55
57
56
58
/// Write a vector.
@@ -64,7 +66,7 @@ pub unsafe fn sys_writev(fd: c_int, iov: *const ctypes::iovec, iocnt: c_int) ->
64
66
let iovs = unsafe { core:: slice:: from_raw_parts( iov, iocnt as usize ) } ;
65
67
let mut ret = 0 ;
66
68
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) ? ;
68
70
}
69
71
70
72
Ok ( ret)
0 commit comments