Luna/shell/builtin/cd.cpp

25 lines
761 B
C++
Raw Normal View History

#include "sh.h"
#include <luna/String.h>
#include <os/ArgumentParser.h>
#include <os/FileSystem.h>
shell_builtin_t builtin_cd = [](int argc, char** argv) -> Result<void> {
StringView directory;
os::ArgumentParser parser;
parser.set_should_exit_on_bad_usage(false);
parser.add_description("Change working directory (shell builtin)"_sv);
parser.add_system_program_info("cd"_sv);
parser.add_positional_argument(directory, "directory"_sv, false);
if (!TRY(parser.parse(argc, argv))) return {};
if (!directory.is_empty()) TRY(os::FileSystem::change_directory(directory));
else
{
auto home = TRY(os::FileSystem::home_directory());
TRY(os::FileSystem::change_directory(home.view()));
}
return {};
};