From 4e8a9f7c8c1f14bc9e5a1ed2c89f04c601adb7a3 Mon Sep 17 00:00:00 2001
From: LoanCold <850323576@qq.com>
Date: Mon, 12 Apr 2021 20:04:19 +0800
Subject: [PATCH] ls

---
 codes/os/src/syscall/fs.rs       |  4 ++++
 codes/os/src/syscall/mod.rs      |  8 ++++++--
 codes/user/src/bin/ls.rs         | 16 ++++++++++++++++
 codes/user/src/bin/user_shell.rs | 15 ++++++++++++---
 codes/user/src/lib.rs            |  8 ++++++--
 codes/user/src/syscall.rs        | 20 +++++++++++++++-----
 6 files changed, 59 insertions(+), 12 deletions(-)
 create mode 100644 codes/user/src/bin/ls.rs

diff --git a/codes/os/src/syscall/fs.rs b/codes/os/src/syscall/fs.rs
index 7fc44eb..a791f8e 100644
--- a/codes/os/src/syscall/fs.rs
+++ b/codes/os/src/syscall/fs.rs
@@ -112,4 +112,8 @@ pub fn sys_dup(fd: usize) -> isize {
 
 pub fn sys_chdir(path: *const u8) -> isize{
     0
+}
+
+pub fn sys_ls(path: *const u8) -> isize{
+    0
 }
\ No newline at end of file
diff --git a/codes/os/src/syscall/mod.rs b/codes/os/src/syscall/mod.rs
index 964471f..7d8ee23 100644
--- a/codes/os/src/syscall/mod.rs
+++ b/codes/os/src/syscall/mod.rs
@@ -1,5 +1,5 @@
-const SYSCALL_CD: usize = 1;
 const SYSCALL_DUP: usize = 24;
+const SYSCALL_CHDIR: usize = 49;
 const SYSCALL_OPEN: usize = 56;
 const SYSCALL_CLOSE: usize = 57;
 const SYSCALL_PIPE: usize = 59;
@@ -13,6 +13,9 @@ const SYSCALL_FORK: usize = 220;
 const SYSCALL_EXEC: usize = 221;
 const SYSCALL_WAITPID: usize = 260;
 
+// Not standard POSIX sys_call
+const SYSCALL_LS: usize = 500;
+
 mod fs;
 mod process;
 
@@ -21,8 +24,8 @@ use process::*;
 
 pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
     match syscall_id {
-        SYSCALL_CD=> sys_chdir(args[0] as *const u8),
         SYSCALL_DUP=> sys_dup(args[0]),
+        SYSCALL_CHDIR=> sys_chdir(args[0] as *const u8),
         SYSCALL_OPEN => sys_open(args[0] as *const u8, args[1] as u32),
         SYSCALL_CLOSE => sys_close(args[0]),
         SYSCALL_PIPE => sys_pipe(args[0] as *mut usize),
@@ -35,6 +38,7 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
         SYSCALL_FORK => sys_fork(),
         SYSCALL_EXEC => sys_exec(args[0] as *const u8, args[1] as *const usize),
         SYSCALL_WAITPID => sys_waitpid(args[0] as isize, args[1] as *mut i32),
+        SYSCALL_LS => sys_ls(args[0] as *const u8),
         _ => panic!("Unsupported syscall_id: {}", syscall_id),
     }
 }
diff --git a/codes/user/src/bin/ls.rs b/codes/user/src/bin/ls.rs
new file mode 100644
index 0000000..95e81fb
--- /dev/null
+++ b/codes/user/src/bin/ls.rs
@@ -0,0 +1,16 @@
+#![no_std]
+#![no_main]
+
+#[macro_use]
+extern crate user_lib;
+
+
+use user_lib::console::getchar;
+
+use user_lib::ls;
+
+#[no_mangle]
+pub fn main(argc: usize, argv: &[&str]) -> i32 {
+    ls(argv[0]);
+    0
+}
\ No newline at end of file
diff --git a/codes/user/src/bin/user_shell.rs b/codes/user/src/bin/user_shell.rs
index 4c727ee..373d773 100644
--- a/codes/user/src/bin/user_shell.rs
+++ b/codes/user/src/bin/user_shell.rs
@@ -286,9 +286,7 @@ impl ArgMachine{
     }
 
     pub fn print_state(&mut self){
-        println!("
-        argc: {}
-        state: {}",
+        println!("argc: {}\nstate: {}\n",
         self.argc,self.state);
         for i in 0..self.argc{
             println!("arg[{}]:{}",i, self.args[i].clone().as_str());
@@ -379,7 +377,18 @@ pub fn main() -> i32 {
     // println!("{}{}{}","hello", KEY_DOWN, "down");
     // println!("{}{}{}","hello", KEY_UP, "up");
     // println!("sth_in:{}",sth_in);
+    // let mut sth = 3;
     println!("{}","TTTTTTTTTTTTTTTTTTEST  test end");
+    // print!("Password:");
+    // loop{
+    //     let c = getchar();
+    //     cursor_move_left!(sth);
+    //     cursor_move_right!(sth);
+    //     if c == '!' as u8{
+    //         println!("");
+    //         break;
+    //     }
+    // }
     let mut shellmachine = InputMachine::new();
     let mut arg_machine = ArgMachine::new();
     // print!(">> ");
diff --git a/codes/user/src/lib.rs b/codes/user/src/lib.rs
index b52f491..063290d 100644
--- a/codes/user/src/lib.rs
+++ b/codes/user/src/lib.rs
@@ -69,8 +69,8 @@ bitflags! {
     }
 }
 
-pub fn chdir(path: &str) -> isize { sys_cd(path) }
 pub fn dup(fd: usize) -> isize { sys_dup(fd) }
+pub fn chdir(path: &str) -> isize { sys_chdir(path) }
 pub fn open(path: &str, flags: OpenFlags) -> isize { sys_open(path, flags.bits) }
 pub fn close(fd: usize) -> isize { sys_close(fd) }
 pub fn pipe(pipe_fd: &mut [usize]) -> isize { sys_pipe(pipe_fd) }
@@ -101,9 +101,13 @@ pub fn waitpid(pid: usize, exit_code: &mut i32) -> isize {
         }
     }
 }
+
 pub fn sleep(period_ms: usize) {
     let start = sys_get_time();
     while sys_get_time() < start + period_ms as isize {
         sys_yield();
     }
-}
\ No newline at end of file
+}
+
+// Not standard POSIX sys_call
+pub fn ls(path: &str) -> isize { sys_ls(path) }
\ No newline at end of file
diff --git a/codes/user/src/syscall.rs b/codes/user/src/syscall.rs
index 56f22be..f7bf8cd 100644
--- a/codes/user/src/syscall.rs
+++ b/codes/user/src/syscall.rs
@@ -1,5 +1,6 @@
-const SYSCALL_CD: usize = 1;
+
 const SYSCALL_DUP: usize = 24;
+const SYSCALL_CHDIR: usize = 49;
 const SYSCALL_OPEN: usize = 56;
 const SYSCALL_CLOSE: usize = 57;
 const SYSCALL_PIPE: usize = 59;
@@ -13,6 +14,9 @@ const SYSCALL_FORK: usize = 220;
 const SYSCALL_EXEC: usize = 221;
 const SYSCALL_WAITPID: usize = 260;
 
+// Not standard POSIX sys_call
+const SYSCALL_LS: usize = 500;
+
 fn syscall(id: usize, args: [usize; 3]) -> isize {
     let mut ret: isize;
     unsafe {
@@ -26,14 +30,14 @@ fn syscall(id: usize, args: [usize; 3]) -> isize {
     ret
 }
 
-pub fn sys_cd(path: &str) -> isize {
-    syscall(SYSCALL_CD, [path.as_ptr() as usize, 0, 0])
-}
-
 pub fn sys_dup(fd: usize) -> isize {
     syscall(SYSCALL_DUP, [fd, 0, 0])
 }
 
+pub fn sys_chdir(path: &str) -> isize {
+    syscall(SYSCALL_CHDIR, [path.as_ptr() as usize, 0, 0])
+}
+
 pub fn sys_open(path: &str, flags: u32) -> isize {
     syscall(SYSCALL_OPEN, [path.as_ptr() as usize, flags as usize, 0])
 }
@@ -81,4 +85,10 @@ pub fn sys_exec(path: &str, args: &[*const u8]) -> isize {
 
 pub fn sys_waitpid(pid: isize, exit_code: *mut i32) -> isize {
     syscall(SYSCALL_WAITPID, [pid as usize, exit_code as usize, 0])
+}
+
+
+// Not standard POSIX sys_call
+pub fn sys_ls(path:&str) -> isize {
+    syscall(SYSCALL_LS, [path.as_ptr() as usize, 0, 0])
 }
\ No newline at end of file
-- 
GitLab