From 9a09c0bfc5956ed57fa4c26f84babb6778fa8a34 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 1 Sep 2026 07:58:49 +0000 Subject: path --- src/command/builtin.cc | 3 ++- src/command/cd.cc | 19 +++++++++++++++++++ src/handler.cc | 4 +++- src/init.cc | 1 - src/main.cc | 10 ++-------- src/path.cc | 20 +++++++++++--------- src/util/prompt.cc | 16 ++++++++++++++++ 7 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 src/command/cd.cc create mode 100644 src/util/prompt.cc (limited to 'src') diff --git a/src/command/builtin.cc b/src/command/builtin.cc index 917dee4..4c5c1e2 100644 --- a/src/command/builtin.cc +++ b/src/command/builtin.cc @@ -13,7 +13,8 @@ unordered_map> cmd = { "exit", ov4::command::exit }, { "jobs", ov4::command::jobs }, { "bg", ov4::command::bg }, - { "fg", ov4::command::fg } + { "fg", ov4::command::fg }, + { "cd", ov4::command::cd }, }; /* diff --git a/src/command/cd.cc b/src/command/cd.cc new file mode 100644 index 0000000..49b1899 --- /dev/null +++ b/src/command/cd.cc @@ -0,0 +1,19 @@ +#include "command/builtin.h" + +namespace ov4::command +{ + +using namespace std; + +void cd(char **argv) +{ + if (argv == nullptr) return; + if (argv[1] == nullptr) + { + cout << "cd: requires path" << endl; + return; + } + ov4::set_current_dir(argv[1]); +} + +} diff --git a/src/handler.cc b/src/handler.cc index 5356d47..b6399c2 100644 --- a/src/handler.cc +++ b/src/handler.cc @@ -8,11 +8,13 @@ sigset_t BLOCK_HANDLER; void signal_init() { + /* Install the signal handlers */ + sigemptyset(&BLOCK_HANDLER); sigaddset(&BLOCK_HANDLER, SIGINT); sigaddset(&BLOCK_HANDLER, SIGTSTP); sigaddset(&BLOCK_HANDLER, SIGCHLD); - + Signal(SIGINT, sigint_handler); /* ctrl-c */ Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */ diff --git a/src/init.cc b/src/init.cc index 9fdd5b7..e4d0b8f 100644 --- a/src/init.cc +++ b/src/init.cc @@ -14,7 +14,6 @@ There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR P Published under GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) Version 3." << endl; signal_init(); - set_current_dir(get_current_dir_name()); } diff --git a/src/main.cc b/src/main.cc index f983590..2132ccb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -18,7 +18,6 @@ using namespace std; using namespace ov4; /* Global variables */ -char prompt[] = "$ "; /* command line prompt (DO NOT CHANGE) */ char sbuf[MAXLINE]; /* for composing sprintf messages */ /* @@ -31,7 +30,6 @@ int main(int argc, char **argv) char c; char cmdline[MAXLINE]; - int emit_prompt = 1; /* emit prompt (default) */ /* Redirect stderr to stdout (so that driver will get all output * on the pipe connected to stdout) */ @@ -54,8 +52,6 @@ int main(int argc, char **argv) } } - /* Install the signal handlers */ - /* This one provides a clean way to kill the shell */ @@ -68,10 +64,8 @@ int main(int argc, char **argv) while (1) { /* Read command line */ - if (emit_prompt) { - printf("%s", prompt); - fflush(stdout); - } + print_prompt(); + if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin)) app_error("fgets error"); if (feof(stdin)) { /* End of file (ctrl-d) */ diff --git a/src/path.cc b/src/path.cc index e9d1892..4d2ea1b 100644 --- a/src/path.cc +++ b/src/path.cc @@ -5,19 +5,16 @@ namespace ov4 using namespace std; -string current_dir; - -string find_cmd(char* s) +string find_cmd(const char * s) { - string tmp = s; + const string tmp = s; return find_cmd(tmp); } -string find_cmd(string s) +string find_cmd(const string &s) { if (s.size() == 0) return ""; string unified_s = path_unifier(s); - cout << unified_s << endl; if (access(unified_s.c_str(), F_OK) == 0) return unified_s; @@ -48,13 +45,18 @@ string path_unifier(const string &s) if (s[0] == '/') return s; if (s[0] == '.') { - return current_dir + s.substr(1); + return get_current_dir() + s.substr(1); } } -string set_current_dir(string s) +int set_current_dir(const string &s) +{ + return chdir(s.c_str()); +} + +string get_current_dir() { - return current_dir = s; + return (string)(get_current_dir_name()); } diff --git a/src/util/prompt.cc b/src/util/prompt.cc new file mode 100644 index 0000000..8f4e5e1 --- /dev/null +++ b/src/util/prompt.cc @@ -0,0 +1,16 @@ +#include "util/prompt.h" + +namespace ov4 +{ + using namespace std; + + bool emit_prompt = true; /* emit prompt (default) */ + + void print_prompt() + { + if (emit_prompt) + { + cout << name << " : " << get_current_dir() << " " << prompt << " " << flush; + } + } +} -- cgit v1.3.1-4-g156e