mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-22 09:26:26 +04:00
Fix cat && add count_lines
This commit is contained in:
parent
1c55663478
commit
29d6d26644
@ -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
|
||||||
|
30
user/src/bin/count_lines.rs
Normal file
30
user/src/bin/count_lines.rs
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user