Commit a4dface0 authored by Matthew Burket's avatar Matthew Burket

worked on cd

parent fa2172be
......@@ -16,8 +16,6 @@ int main() {
int should_run = 1;
char *readBuff = malloc(BUFFSIZE);
char *HOME = getenv("HOME");
char *HISTORY_FILE = HOME;
strcat(HISTORY_FILE, "/.bsh_history");
// Copyright statement
fprintf(stdout, "Welcome to Burket Shell (bsh)\n");
fprintf(stdout, "Copyright 2018 Matthew Burket\n");
......@@ -28,12 +26,9 @@ int main() {
fprintf(stdout, "bsh # ");
// Getting User Input
fgets(readBuff, sizeof(char) * 5000, stdin);
// If input wr
// History store stuff
if (strlen(readBuff) > 0) {
FILE *fp = fopen(HISTORY_FILE, "a+");
write(fp->_fileno, readBuff, BUFFSIZE);
close(fp->_fileno);
//
}
// Remove the newline from fgets
strtok(readBuff, "\n");
......@@ -50,7 +45,16 @@ int main() {
fprintf(stderr, "bsh: expected one argument for cd");
continue;
} else {
chdir(tokens[1]);
if (strcmp(tokens[1], "~") == 0) {
chdir(getenv("HOME"));
strcpy(tokens[1], getenv("HOME"));
} else if (strcmp(tokens[1], "-") == 0) {
chdir(getenv("OLDPWD"));
strcpy(tokens[1], getenv("OLDPWD"));
}
else {
chdir(tokens[1]);
}
}
}
......@@ -61,7 +65,10 @@ int main() {
}
if (fork() == 0) {
execvp(tokens[0], tokens);
int returnCode = execvp(tokens[0], tokens);
if (returnCode == -1) {
fprintf(stdout, "bsh: Command not found\n");
}
} else {
if (runInBackground == 0) {
wait(NULL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment