mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-23 09:23:25 +00:00
Implement getcwd()
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
ea489252f4
commit
bdf89a5de3
@ -1,14 +1,27 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use super::SyscallReturn;
|
||||
use crate::prelude::*;
|
||||
use crate::{
|
||||
fs::fs_resolver::{FsPath, AT_FDCWD},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
pub fn sys_getcwd(buf: Vaddr, len: usize, ctx: &Context) -> Result<SyscallReturn> {
|
||||
// TODO: getcwd only return a fake result now
|
||||
let fake_cwd = CString::new("/")?;
|
||||
let bytes = fake_cwd.as_bytes_with_nul();
|
||||
let current = ctx.process;
|
||||
let dirent = current
|
||||
.fs()
|
||||
.read()
|
||||
.lookup(&FsPath::new(AT_FDCWD, "").unwrap())
|
||||
.unwrap();
|
||||
let name = dirent.abs_path();
|
||||
|
||||
debug!("getcwd: {:?}", name);
|
||||
|
||||
let cwd = CString::new(name)?;
|
||||
let bytes = cwd.as_bytes_with_nul();
|
||||
let write_len = len.min(bytes.len());
|
||||
ctx.get_user_space()
|
||||
.write_bytes(buf, &mut VmReader::from(&bytes[..write_len]))?;
|
||||
|
||||
Ok(SyscallReturn::Return(write_len as _))
|
||||
}
|
||||
|
Reference in New Issue
Block a user