display_logged_user

This commit is contained in:
s492459 2025-01-04 16:56:48 +01:00
parent 91476b920d
commit f32e77072f

18
main.c
View File

@ -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
@ -35,7 +38,14 @@ int ChangeDirectory(char** args) { // cd
}
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;