From f32e77072f7e39ec78493f20ccec6c4774fbb020 Mon Sep 17 00:00:00 2001 From: s492459 Date: Sat, 4 Jan 2025 16:56:48 +0100 Subject: [PATCH] display_logged_user --- main.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 051e936..7967a57 100644 --- a/main.c +++ b/main.c @@ -14,11 +14,14 @@ void PrintCurrentDir() { // pwd char cwd[MAX_SIZE]; - if (getcwd(cwd, sizeof(cwd)) != NULL) { - printf("[%s] $ ", cwd); - } else { + if (getcwd(cwd, sizeof(cwd)) == NULL) { perror("getcwd() error"); } + char* user = getenv("USER"); + if (user == NULL) { + perror("getenv() error"); + } + printf("[%s@%s] $ ", user, cwd); } int ChangeDirectory(char** args) { // cd @@ -34,8 +37,15 @@ int ChangeDirectory(char** args) { // cd return 0; } -void PrintHelp() { // help +void PrintHelp() { // help + printf("-------------------------------------------------"); printf("C Microshell implementation by Maciej Życzyński\n"); + printf("\"exit\" - exit the program"); + printf("\"help\" - display this menu"); + printf("Other bash commands work accordingly in child processes"); + printf("Currently logged user / working directory:"); + PrintCurrentDir(); + printf("-------------------------------------------------"); } int HandleBuiltInCommands(char* command, char** args) { // Manual implementation of commands unsupported by execvp() @@ -55,7 +65,7 @@ int HandleBuiltInCommands(char* command, char** args) { // Manual implementation int HandleCommand(char* command, char** args) { // Execute commands supported by execvp() if (execvp(command, args) == -1) { - perror("Error executing command"); + printf("Error executing command\n"); return -1; } return 0;