diff --git a/src/header/stdio/helpers.rs b/src/header/stdio/helpers.rs
index f67199e8a6ae166f7a45140abc8bd0ea18e5059f..3d1629fdd4f65e12e17fc892b71c14208c3f5c7d 100644
--- a/src/header/stdio/helpers.rs
+++ b/src/header/stdio/helpers.rs
@@ -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;
     }
 
diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs
index e4e0e8d9ef2e2f1ec77feb76a1cc72a11686b801..6420a846e1d0b86d37523bced1b34c86e29b720e 100644
--- a/src/header/stdio/mod.rs
+++ b/src/header/stdio/mod.rs
@@ -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();
     }
diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs
index 8334f110865669938df91ce170600b7e88254541..816c2aed41d82684ac541c334f8d8d0455e65b16 100644
--- a/src/header/unistd/mod.rs
+++ b/src/header/unistd/mod.rs
@@ -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]
diff --git a/src/platform/rlb.rs b/src/platform/rlb.rs
index a65dc157b001d679454aa2bd4a00f64dc91107b9..8b8f65a6d4371275b0c8c4a6d178de09b1940d22 100644
--- a/src/platform/rlb.rs
+++ b/src/platform/rlb.rs
@@ -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;