Verified Commit 658dc34d authored by jD91mZM2's avatar jD91mZM2
Browse files

A few I/O related fixes

parent 1e9dbfdf
Pipeline #1357 failed with stages
in 9 minutes and 44 seconds
......@@ -30,8 +30,7 @@ pub unsafe fn parse_mode_flags(mode_str: *const c_char) -> i32 {
}
if (*mode_str) == b'w' as i8 {
flags |= O_TRUNC;
}
if (*mode_str) != b'a' as i8 {
} else if (*mode_str) == b'a' as i8 {
flags |= O_APPEND;
}
......
......@@ -376,13 +376,19 @@ pub extern "C" fn fgets(s: *mut c_char, n: c_int, stream: &mut FILE) -> *mut c_c
idiff += 1;
len -= 1;
if st[pos] == b'\n' || st[pos] as i8 == stream.buf_char {
stream.read = Some((rpos + idiff, rend));
break 'outer;
}
}
stream.read = Some((rpos + idiff, rend));
if rend - rpos == 0 {
len -= stream.read(&mut st[((n - len) as usize)..]) as i32;
break;
match stream.read(&mut st[((n - len) as usize)..]) as i32 {
0 if idiff == 0 => return ptr::null_mut(),
n => {
len -= n.max(0);
break;
}
}
}
if len <= 1 {
break;
......@@ -426,7 +432,13 @@ pub extern "C" fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FI
let flags = unsafe { helpers::parse_mode_flags(mode) };
let fd = fcntl::sys_open(filename, flags, 0o666);
let new_mode = if flags & fcntl::O_CREAT == fcntl::O_CREAT {
0o666
} else {
0
};
let fd = fcntl::sys_open(filename, flags, new_mode);
if fd < 0 {
return ptr::null_mut();
}
......
......@@ -378,7 +378,13 @@ pub extern "C" fn pwrite(
pub extern "C" fn read(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t {
use core::slice;
let buf = unsafe { slice::from_raw_parts_mut(buf as *mut u8, nbyte as usize) };
Sys::read(fildes, buf)
trace_expr!(
Sys::read(fildes, buf),
"read({}, {:p}, {})",
fildes,
buf,
nbyte
)
}
// #[no_mangle]
......
......@@ -70,7 +70,11 @@ impl RawLineBuffer {
self.read += read_usize;
if read == 0 {
return Line::EOF;
return if self.buf.is_empty() {
Line::EOF
} else {
Line::Some(&self.buf)
};
}
if read < 0 {
return Line::Error;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment