diff --git a/src/stdio/src/lib.rs b/src/stdio/src/lib.rs
index 4c00df7c2cf20d1e0c8dd6f91e7bf0a1ceba63fa..d0b38f9405832fc6d96744d76375a7e6a3f3e7ae 100644
--- a/src/stdio/src/lib.rs
+++ b/src/stdio/src/lib.rs
@@ -351,6 +351,9 @@ pub extern "C" fn fgets(s: *mut c_char, n: c_int, stream: &mut FILE) -> *mut c_c
     if let Some((rpos, rend)) = stream.read {
         for _ in (0..(n-1) as usize).take_while(|x| rpos + x < rend) {
             st[diff] = stream.buf[rpos + diff] as i8;
+            if st[diff] == b'\n' as i8 || st[diff] == stream.buf_char {
+                break;
+            }
             diff += 1;
         }
         stream.read = Some((rpos+diff, rend));