From 67e954367501c65a24e3451112e137586b5b7df4 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 18 Apr 2023 18:46:19 +0200 Subject: [PATCH] os: Add FileSystem::change_directory --- apps/sh.cpp | 4 ++-- libos/include/os/FileSystem.h | 2 ++ libos/src/FileSystem.cpp | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/sh.cpp b/apps/sh.cpp index 01d43793..0e16bcc8 100644 --- a/apps/sh.cpp +++ b/apps/sh.cpp @@ -75,11 +75,11 @@ Result luna_main(int argc, char** argv) if (args.size() == 1) { auto home = TRY(os::FileSystem::home_directory()); - if (chdir(home.chars()) < 0) perror("cd"); + TRY(os::FileSystem::change_directory(home.view())); continue; } - if (chdir(args[1].chars()) < 0) perror("cd"); + TRY(os::FileSystem::change_directory(args[1].view())); continue; } diff --git a/libos/include/os/FileSystem.h b/libos/include/os/FileSystem.h index a9085527..19899567 100644 --- a/libos/include/os/FileSystem.h +++ b/libos/include/os/FileSystem.h @@ -17,5 +17,7 @@ namespace os Result working_directory(); Result home_directory(); + + Result change_directory(StringView path); } } diff --git a/libos/src/FileSystem.cpp b/libos/src/FileSystem.cpp index 838556e9..77e3d3fe 100644 --- a/libos/src/FileSystem.cpp +++ b/libos/src/FileSystem.cpp @@ -66,4 +66,14 @@ namespace os::FileSystem return String::from_cstring(pw->pw_dir); } + + Result change_directory(StringView path) + { + long rc = syscall(SYS_chdir, path.chars()); + int _ignore = TRY(Result::from_syscall(rc)); + + ignore(_ignore); + + return {}; + } }