Add sys_dup && support input/output redirection in user_shell

This commit is contained in:
Yifan Wu 2021-02-27 22:27:08 +08:00
parent ff685b86c8
commit 685ca2c1ea
6 changed files with 93 additions and 13 deletions

View file

@ -6,6 +6,7 @@ use crate::mm::{
};
use crate::task::{current_user_token, current_task};
use crate::fs::{make_pipe, OpenFlags, open_file};
use alloc::sync::Arc;
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
let token = current_user_token();
@ -93,4 +94,18 @@ pub fn sys_pipe(pipe: *mut usize) -> isize {
*translated_refmut(token, pipe) = read_fd;
*translated_refmut(token, unsafe { pipe.add(1) }) = write_fd;
0
}
pub fn sys_dup(fd: usize) -> isize {
let task = current_task().unwrap();
let mut inner = task.acquire_inner_lock();
if fd >= inner.fd_table.len() {
return -1;
}
if inner.fd_table[fd].is_none() {
return -1;
}
let new_fd = inner.alloc_fd();
inner.fd_table[new_fd] = Some(Arc::clone(inner.fd_table[fd].as_ref().unwrap()));
new_fd as isize
}