pish/src/Integrated.c

63 lines
1.6 KiB
C
Raw Normal View History

#include <assert.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Integrated.h"
// Gets home directory location of user
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)
{
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;
}
// Reads ~/.pishrc and runs each command in the file
void ReadPishrc(CommandStruct* command, char* inputString)
{
char buffer[1];
int forkID;
int* argc = &(command->argc);
int fd = open(GetHomeDir(), O_RDONLY | O_CREAT);
assert(fd > -1);
while (read(fd, buffer, 1) > 0)
{
if (buffer[0] == ' ')
{
command->argv[*argc] = inputString;
strcpy(inputString, "");
*argc++;
}
if (buffer[0] == '\n')
{
command->argv[*argc] = inputString;
strcpy(inputString, "");
forkID = fork();
if (forkID == 0)
execvp(command->argv[0], command->argv);
wait(&forkID);
ResetCommandStruct(command);
}
strcat(inputString, buffer);
}
assert(close(fd) >= 0);
}