display_logged_user

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

20
main.c
View File

@ -14,11 +14,14 @@
void PrintCurrentDir() { // pwd void PrintCurrentDir() { // pwd
char cwd[MAX_SIZE]; char cwd[MAX_SIZE];
if (getcwd(cwd, sizeof(cwd)) != NULL) { if (getcwd(cwd, sizeof(cwd)) == NULL) {
printf("[%s] $ ", cwd);
} else {
perror("getcwd() error"); perror("getcwd() error");
} }
char* user = getenv("USER");
if (user == NULL) {
perror("getenv() error");
}
printf("[%s@%s] $ ", user, cwd);
} }
int ChangeDirectory(char** args) { // cd int ChangeDirectory(char** args) { // cd
@ -34,8 +37,15 @@ int ChangeDirectory(char** args) { // cd
return 0; return 0;
} }
void PrintHelp() { // help void PrintHelp() { // help
printf("-------------------------------------------------");
printf("C Microshell implementation by Maciej Życzyński\n"); 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() 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() int HandleCommand(char* command, char** args) { // Execute commands supported by execvp()
if (execvp(command, args) == -1) { if (execvp(command, args) == -1) {
perror("Error executing command"); printf("Error executing command\n");
return -1; return -1;
} }
return 0; return 0;