2022-10-27 00:25:40 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
2022-10-19 21:44:28 -05:00
|
|
|
#include <stdlib.h>
|
2022-10-27 00:25:40 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2022-10-27 03:47:19 -05:00
|
|
|
#include <sys/wait.h>
|
2022-10-19 21:44:28 -05:00
|
|
|
#include "Integrated.h"
|
|
|
|
|
2022-10-27 03:47:19 -05:00
|
|
|
// Gets home directory location of user
|
|
|
|
const char* GetHomeDir(void)
|
|
|
|
{
|
|
|
|
char* homeDir = (getpwuid(getuid()))->pw_dir;
|
|
|
|
strcat(homeDir, "/.pishrc");
|
|
|
|
return homeDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-19 21:44:28 -05:00
|
|
|
// Checks for commands that are built into Pish
|
2022-10-27 04:37:16 -05:00
|
|
|
void IntegratedCheck(CommandStruct* command)
|
2022-10-19 21:44:28 -05:00
|
|
|
{
|
2022-10-27 04:37:16 -05:00
|
|
|
if (strcmp(command->argv[0], "exit") == 0)
|
2022-10-19 21:44:28 -05:00
|
|
|
exit(0);
|
|
|
|
// If there is an argument, change to argument location
|
|
|
|
// Else, change to home directory
|
2022-10-27 04:37:16 -05:00
|
|
|
if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd')
|
2022-10-19 21:44:28 -05:00
|
|
|
;
|
|
|
|
return;
|
2022-10-27 00:25:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reads ~/.pishrc and runs each command in the file
|
2022-10-27 03:47:19 -05:00
|
|
|
void ReadPishrc(CommandStruct* command, char* inputString)
|
2022-10-27 00:25:40 -05:00
|
|
|
{
|
2022-10-27 04:37:16 -05:00
|
|
|
char buffer[1];
|
2022-10-27 03:47:19 -05:00
|
|
|
int forkID;
|
|
|
|
int* argc = &(command->argc);
|
|
|
|
int fd = open(GetHomeDir(), O_RDONLY | O_CREAT);
|
2022-10-27 00:25:40 -05:00
|
|
|
assert(fd > -1);
|
2022-10-27 04:37:16 -05:00
|
|
|
while (read(fd, buffer, 1) > 0)
|
2022-10-27 00:25:40 -05:00
|
|
|
{
|
2022-10-27 04:37:16 -05:00
|
|
|
if (buffer[0] == ' ')
|
2022-10-27 03:47:19 -05:00
|
|
|
{
|
|
|
|
command->argv[*argc] = inputString;
|
|
|
|
strcpy(inputString, "");
|
|
|
|
*argc++;
|
|
|
|
}
|
2022-10-27 04:37:16 -05:00
|
|
|
if (buffer[0] == '\n')
|
2022-10-27 00:25:40 -05:00
|
|
|
{
|
2022-10-27 03:47:19 -05:00
|
|
|
command->argv[*argc] = inputString;
|
|
|
|
strcpy(inputString, "");
|
|
|
|
forkID = fork();
|
|
|
|
if (forkID == 0)
|
|
|
|
execvp(command->argv[0], command->argv);
|
|
|
|
wait(&forkID);
|
|
|
|
ResetCommandStruct(command);
|
2022-10-27 00:25:40 -05:00
|
|
|
}
|
2022-10-27 04:37:16 -05:00
|
|
|
strcat(inputString, buffer);
|
2022-10-27 00:25:40 -05:00
|
|
|
}
|
|
|
|
assert(close(fd) >= 0);
|
2022-10-19 21:44:28 -05:00
|
|
|
}
|