Fix cat && add count_lines

This commit is contained in:
Yifan Wu 2022-01-22 12:54:00 -08:00
parent 1c55663478
commit 29d6d26644
2 changed files with 31 additions and 1 deletions

View File

@ -21,7 +21,7 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
if size == 0 { if size == 0 {
break; break;
} }
println!("{}", core::str::from_utf8(&buf[..size]).unwrap()); print!("{}", core::str::from_utf8(&buf[..size]).unwrap());
} }
close(fd); close(fd);
0 0

View File

@ -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
}