From 1059c1ac20c7b3cb65ec5990a288e305722b8798 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Wed, 3 Jun 2020 20:46:16 -0300 Subject: [PATCH] Internal implementation of cd --- Sources/input.cpp | 2 +- Sources/opsys.cpp | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Sources/input.cpp b/Sources/input.cpp index 1a0c6b7..9ede9b6 100644 --- a/Sources/input.cpp +++ b/Sources/input.cpp @@ -59,7 +59,7 @@ void Prompt::print() get_last_dir(); cwd_changed = false; } - cout << BLUE_ANSI << OS.username << "@" << OS.hostname << ":" << RED_ANSI << last_dir << BLUE_ANSI << "$ " << RESET_ANSI; + cout << BLUE_ANSI << OS.username << "@" << OS.hostname << ":" << RED_ANSI << last_dir << RESET_ANSI << "$ "; } void Prompt::parse(string input) diff --git a/Sources/opsys.cpp b/Sources/opsys.cpp index ddbe198..71651b9 100644 --- a/Sources/opsys.cpp +++ b/Sources/opsys.cpp @@ -43,18 +43,29 @@ string OpSys::get_cwd() void OpSys::change_dir(vector command) { - string cmd = ""; - for(unsigned short int i = 1; i < command.size(); i++) - { - cmd += command[i]; + if (command.size() == 1 || !command[1].compare("~") || !command[1].compare("$HOME")) + { // User wants to cd into $HOME/~. + chdir(getenv("HOME")); + this->cwd = get_cwd(); + cwd_changed = true; + return; } + + string cmd; + + for(unsigned short int i = 1; i < command.size(); i++) // TODO: unneeded? + cmd += command[i]; + if (chdir(cmd.c_str())) { - cerr << "miniShell: cd: " << cmd << ": Arquivo ou diretório não encontrado\n"; + // Couldn't get to change dirs + cerr << "minish2: cd: " << cmd << ": Arquivo ou diretório não encontrado.\n"; } - else { - get_cwd(); - cwd_changed = true; + else + { + // chdir worked. and tell Prompt to act accordingly. + this->cwd = get_cwd(); // Update the OpSys OS's current cwd + cwd_changed = true; // Signals a cwd change to Prompt. } } @@ -85,7 +96,6 @@ short OpSys::simple_command(vector tokens) pid_t pid = fork(); if (pid == 0) { - //printf("execlp(%s, %s, NULL);", bincmd.c_str(), command.c_str()); execvp(tokens[0].c_str(), const_cast(make_argv(tokens).data())); cerr << tokens[0] << ": command not found.\n"; return 0; // TODO: exit with cleanup -- 2.11.4.GIT