diff --git a/user/src/bin/cat.rs b/user/src/bin/cat.rs index 05ab353d..424f44f3 100644 --- a/user/src/bin/cat.rs +++ b/user/src/bin/cat.rs @@ -21,7 +21,7 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 { if size == 0 { break; } - println!("{}", core::str::from_utf8(&buf[..size]).unwrap()); + print!("{}", core::str::from_utf8(&buf[..size]).unwrap()); } close(fd); 0 diff --git a/user/src/bin/count_lines.rs b/user/src/bin/count_lines.rs new file mode 100644 index 00000000..276e12ed --- /dev/null +++ b/user/src/bin/count_lines.rs @@ -0,0 +1,30 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use user_lib::read; + +#[no_mangle] +pub fn main(_argc: usize, _argv: &[&str]) -> i32 { + let mut buf = [0u8; 256]; + let mut lines = 0usize; + let mut total_size = 0usize; + loop { + let len = read(0, &mut buf) as usize; + if len == 0 { + break; + } + total_size += len; + let string = core::str::from_utf8(&buf[..len]).unwrap(); + lines += string + .chars() + .fold(0, |acc, c| acc + if c == '\n' { 1 } else { 0 }); + } + if total_size > 0 { + lines += 1; + } + println!("{}", lines); + 0 +}