Adjusted GetHomeDir() and fixed cd command

This commit is contained in:
TriantaTV 2022-10-28 16:14:53 -05:00
parent 94f2c4fcb8
commit e0905dbaf2
4 changed files with 16 additions and 8 deletions

View File

@ -5,7 +5,7 @@
const char* GetHomeDir(void); const char* GetHomeDir(void);
void ioRedirection(CommandStruct *command); void ioRedirection(CommandStruct *command);
void IntegratedCheck(CommandStruct* command); int IntegratedCheck(CommandStruct* command);
void GetInput(char* inputString); void GetInput(char* inputString);
void append(char *input, char *file); void append(char *input, char *file);
void input(char *command); void input(char *command);

View File

@ -5,7 +5,7 @@
void ResetCommandStruct(CommandStruct* command) void ResetCommandStruct(CommandStruct* command)
{ {
for (int i = 0; i < command->argc; i++) for (int i = 0; i < command->argc; i++)
command->argv[i] = ""; command->argv[i] = NULL;
command->argc = 0; command->argc = 0;
return; return;
} }

View File

@ -14,20 +14,25 @@
const char *GetHomeDir(void) const char *GetHomeDir(void)
{ {
char *homeDir = (getpwuid(getuid()))->pw_dir; char *homeDir = (getpwuid(getuid()))->pw_dir;
strcat(homeDir, "/.pishrc");
return homeDir; return homeDir;
} }
// Checks for commands that are built into Pish // Checks for commands that are built into Pish
void IntegratedCheck(CommandStruct *command) int IntegratedCheck(CommandStruct *command)
{ {
if (strcmp(command->argv[0], "exit") == 0) if (strcmp(command->argv[0], "exit") == 0)
exit(0); exit(0);
// If there is an argument, change to argument location // If there is an argument, change to argument location
// Else, change to home directory // Else, change to home directory
if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd') 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 // Prints a prompt and then reads a command from the terminal
@ -67,7 +72,9 @@ void ReadPishrc(CommandStruct *command, char *inputString)
char buffer[1]; char buffer[1];
int forkID; int forkID;
int *argc = &(command->argc); 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); assert(fd > -1);
while (read(fd, buffer, 1) > 0) while (read(fd, buffer, 1) > 0)
{ {

View File

@ -21,7 +21,8 @@ void Pish(void)
strcpy(inputString, ""); // Clean inputString strcpy(inputString, ""); // Clean inputString
GetInput(inputString); GetInput(inputString);
ParseInput(inputString, command); ParseInput(inputString, command);
IntegratedCheck(command); if (IntegratedCheck(command))
continue;
forkPID = fork(); forkPID = fork();
// Child // Child
if (forkPID == 0) if (forkPID == 0)