From 81e412fa80ddce218120578b980f4a68f80d1ae1 Mon Sep 17 00:00:00 2001 From: Yu Chen Date: Mon, 4 Apr 2022 09:37:06 +0800 Subject: [PATCH] add usr/src/bin/cat_filea.rs to show text file: filea's contents --- user/src/bin/cat_filea.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 user/src/bin/cat_filea.rs diff --git a/user/src/bin/cat_filea.rs b/user/src/bin/cat_filea.rs new file mode 100644 index 00000000..6307ecd5 --- /dev/null +++ b/user/src/bin/cat_filea.rs @@ -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 +}