diff --git a/src/builtin_commands.c b/src/builtin_commands.c index 1246e77..dc67446 100644 --- a/src/builtin_commands.c +++ b/src/builtin_commands.c @@ -1,61 +1,85 @@ #include "builtin_commands.h" -int is_builtin_command(const Command* command, const char* expected) { +int is_builtin_command(const Command *command, const char *expected) +{ return strcmp(command->command, expected) == 0; } -void cd(const char* path) { - if (chdir(path) != 0) { +void cd(const char *path) +{ + if (chdir(path) != 0) + { perror("cd"); } } -void pwd() { - char cwd[1024]; - if (getcwd(cwd, sizeof(cwd)) == NULL) { +void pwd() +{ + char cwd[MAX_INPUT_LENGTH]; + if (getcwd(cwd, sizeof(cwd)) == NULL) + { perror("pwd"); - } else { + } + else + { printf("%s\n", cwd); } } -void exit_shell() { +void exit_shell() +{ exit(EXIT_SUCCESS); } -void echo(const Command* command) { - for (int i = 0; i < command->num_arguments; i++) { +void echo(const Command *command) +{ + for (int i = 0; i < command->num_arguments; i++) + { printf("%s ", command->arguments[i]); } printf("\n"); } -int execute_builtin_command(const Command* command) { - if (is_builtin_command(command, "cd")) { - if (command->num_arguments == 1) { +int execute_builtin_command(const Command *command) +{ + if (is_builtin_command(command, "cd")) + { + if (command->num_arguments == 1) + { cd(command->arguments[0]); return IS_BUILTIN_COMMAND; } perror("Usage: cd "); - } else if (is_builtin_command(command, "pwd")) { - if (command->num_arguments == 0) { + } + else if (is_builtin_command(command, "pwd")) + { + if (command->num_arguments == 0) + { pwd(); return IS_BUILTIN_COMMAND; } perror("Usage: pwd"); - } else if (is_builtin_command(command, "exit")) { - if (command->num_arguments == 0) { + } + else if (is_builtin_command(command, "exit")) + { + if (command->num_arguments == 0) + { exit_shell(); return IS_BUILTIN_COMMAND; } perror("Usage: exit"); - } else if (is_builtin_command(command, "echo")) { - if (command->num_arguments >= 1) { + } + else if (is_builtin_command(command, "echo")) + { + if (command->num_arguments >= 1) + { echo(command); return IS_BUILTIN_COMMAND; } perror("Usage: echo "); - } else { + } + else + { return IS_NOT_BUILTIN_COMMAND; } diff --git a/src/console.c b/src/console.c index 6edd1c7..711aaea 100644 --- a/src/console.c +++ b/src/console.c @@ -2,15 +2,16 @@ void print_title() { - printf("\n" YELB" __ _____ ____ _____ __ __________ __ " RESET); - printf("\n" YELB" / / / _/ | / / / / ___// / / / ____/ / / / " RESET); - printf("\n" YELB" / / / / | | / / / \\__ \\/ /_/ / __/ / / / / " RESET); - printf("\n" YELB" / /____/ / | |/ / /___ ___/ / __ / /___/ /___/ /___ " RESET); - printf("\n" YELB"/_____/___/ |___/_____/ /____/_/ /_/_____/_____/_____/ " RESET); - printf("\n" YELB" "RESET "\n\n"); + printf("\n" YELB " __ _____ ____ _____ __ __________ __ " RESET); + printf("\n" YELB " / / / _/ | / / / / ___// / / / ____/ / / / " RESET); + printf("\n" YELB " / / / / | | / / / \\__ \\/ /_/ / __/ / / / / " RESET); + printf("\n" YELB " / /____/ / | |/ / /___ ___/ / __ / /___/ /___/ /___ " RESET); + printf("\n" YELB "/_____/___/ |___/_____/ /____/_/ /_/_____/_____/_____/ " RESET); + printf("\n" YELB " " RESET "\n\n"); } -void print_perror(const char *format, ...) { +void print_perror(const char *format, ...) +{ va_list args; va_start(args, format); @@ -32,9 +33,15 @@ void print_info(const char *text, ...) void print_prompt() { - char* username = getenv("USER"); - if(username == NULL) - username = "username"; + char *username = getenv("USER"); + if (username == NULL) + username = ""; + + char cwd[MAX_INPUT_LENGTH]; + if (getcwd(cwd, sizeof(cwd)) == NULL) + { + cwd[0] = '\0'; + } // Use this if you want to print the hostname // char hostname[1024]; @@ -42,7 +49,7 @@ void print_prompt() // if(username == NULL) // username = "hostname"; - printf(YELLOW "%s" RESET "@" BLUE "livl-shell" RESET ":$ ", username); + printf(YELLOW "%s" RESET "@" BLUE "livl-shell" GREEN " %s" RESET " $ ", username, cwd); } void print_exit_info()