From e0905dbaf2b785420ab67aada282d0a252ce3170 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Fri, 28 Oct 2022 16:14:53 -0500 Subject: [PATCH] Adjusted GetHomeDir() and fixed cd command --- include/Integrated.h | 2 +- src/Common.c | 2 +- src/Integrated.c | 17 ++++++++++++----- src/Pish.c | 3 ++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index 1b91336..ecfd85f 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -5,7 +5,7 @@ const char* GetHomeDir(void); void ioRedirection(CommandStruct *command); -void IntegratedCheck(CommandStruct* command); +int IntegratedCheck(CommandStruct* command); void GetInput(char* inputString); void append(char *input, char *file); void input(char *command); diff --git a/src/Common.c b/src/Common.c index 62393af..1f230a8 100644 --- a/src/Common.c +++ b/src/Common.c @@ -5,7 +5,7 @@ void ResetCommandStruct(CommandStruct* command) { for (int i = 0; i < command->argc; i++) - command->argv[i] = ""; + command->argv[i] = NULL; command->argc = 0; return; } diff --git a/src/Integrated.c b/src/Integrated.c index b003a34..3722651 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -14,20 +14,25 @@ const char *GetHomeDir(void) { char *homeDir = (getpwuid(getuid()))->pw_dir; - strcat(homeDir, "/.pishrc"); return homeDir; } // Checks for commands that are built into Pish -void IntegratedCheck(CommandStruct *command) +int IntegratedCheck(CommandStruct *command) { if (strcmp(command->argv[0], "exit") == 0) exit(0); // If there is an argument, change to argument location // Else, change to home directory if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd') - ; - return; + { + if (command->argv[1] != NULL) + chdir(command->argv[1]); + if (command->argv[1] == NULL) + chdir(GetHomeDir()); + return 1; + } + return 0; } // Prints a prompt and then reads a command from the terminal @@ -67,7 +72,9 @@ void ReadPishrc(CommandStruct *command, char *inputString) char buffer[1]; int forkID; int *argc = &(command->argc); - int fd = open(GetHomeDir(), O_RDONLY | O_CREAT); + char* pishLocation = GetHomeDir(); + strcat(pishLocation, "/.pishrc"); + int fd = open(pishLocation, O_RDONLY | O_CREAT); assert(fd > -1); while (read(fd, buffer, 1) > 0) { diff --git a/src/Pish.c b/src/Pish.c index cc42bf7..a4d370d 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -21,7 +21,8 @@ void Pish(void) strcpy(inputString, ""); // Clean inputString GetInput(inputString); ParseInput(inputString, command); - IntegratedCheck(command); + if (IntegratedCheck(command)) + continue; forkPID = fork(); // Child if (forkPID == 0)