add usr/src/bin/cat_filea.rs to show text file: filea's contents
This commit is contained in:
parent
0c2244d9c5
commit
81e412fa80
1 changed files with 27 additions and 0 deletions
27
user/src/bin/cat_filea.rs
Normal file
27
user/src/bin/cat_filea.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use user_lib::{close, open, read, OpenFlags};
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let fd = open("filea\0", OpenFlags::RDONLY);
|
||||
if fd == -1 {
|
||||
panic!("Error occured when opening file");
|
||||
}
|
||||
let fd = fd as usize;
|
||||
let mut buf = [0u8; 256];
|
||||
loop {
|
||||
let size = read(fd, &mut buf) as usize;
|
||||
if size == 0 {
|
||||
break;
|
||||
}
|
||||
println!("{}", core::str::from_utf8(&buf[..size]).unwrap());
|
||||
}
|
||||
close(fd);
|
||||
0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue