Commit 90c96e42 authored by Matthew Burket's avatar Matthew Burket

Started History

parent 8a900b9a
......@@ -3,5 +3,5 @@ project(coms352_shell C)
set(CMAKE_C_STANDARD 11)
add_executable(coms352_shell main.c tokenize.c tokenize.h)
add_executable(coms352_shell main.c tokenize.c tokenize.h file_operations.c file_operations.h)
target_link_libraries(coms352_shell)
//
// Created by mburket on 2/28/18.
//
#include "file_operations.h"
//
// Created by mburket on 2/28/18.
//
#ifndef COMS352_SHELL_FILE_OPERATIONS_H
#define COMS352_SHELL_FILE_OPERATIONS_H
#endif //COMS352_SHELL_FILE_OPERATIONS_H
......@@ -7,55 +7,65 @@
#include "tokenize.h"
#define TRUE 0
#define READ 0
#define BUFFSIZE (sizeof(char) * 5000)
int main() {
// Read line examples: https://eli.thegreenplace.net/2016/basics-of-using-the-readline-library/
// Setup Some Vars
int should_run = 1;
char *readBuff = malloc(sizeof(char) * 5000);
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");
fprintf(stdout, "Released under the GPL v3 License\n");
// Time to start the main loop
while (should_run) {
// Burket Shell (bsh) prompt
fprintf(stdout, "bsh # ");
// Getting User Input
fgets(readBuff, sizeof(char) * 5000, stdin);
// If input wr
if (strlen(readBuff) > 0) {
FILE *fp = fopen(HISTORY_FILE, "a+");
write(fp->_fileno, readBuff, BUFFSIZE);
close(fp);
}
// Remove the newline from fgets
strtok(readBuff, "\n");
//while ((readBuff = readline("bsh >> ")) != 0) {
if (strlen(readBuff) > 0) {
// History stuff and errors
if (strcmp("exit", readBuff) == TRUE) {
should_run = 0;
free(readBuff);
exit(EXIT_SUCCESS);
}
char **tokens = malloc(sizeof(char) * 5000);
int numTokens = tokenize(readBuff, tokens);
char *lastToken = tokens[numTokens - 2];
if (strcmp(tokens[0], "cd") == TRUE) {
if (tokens[1] == NULL) {
fprintf(stderr, "bsh: expected one argument for cd");
continue;
} else {
chdir(tokens[1]);
}
}
if (strcmp("exit", readBuff) == TRUE) {
should_run = 0;
free(readBuff);
exit(EXIT_SUCCESS);
}
char **tokens = malloc(sizeof(char) * 5000);
int numTokens = tokenize(readBuff, tokens);
char *lastToken = tokens[numTokens-2];
if (strcmp(tokens[0], "cd") == TRUE) {
if (tokens[1] == NULL) {
fprintf(stderr, "bsh: expected one argument for cd");
continue;
} else {
chdir(tokens[1]);
}
}
int runInBackground = 0;
if (strcmp(lastToken, "&") == TRUE) {
runInBackground = 1;
tokens[numTokens - 2] = NULL;
}
int runInBackground = 0;
if (strcmp(lastToken, "&") == TRUE) {
runInBackground = 1;
tokens[numTokens-2] = NULL;
}
if (fork() == 0) {
int returnCode = execvp(tokens[0], tokens);
printf("Return code %d\n", returnCode);
} else {
if (runInBackground == 0) {
wait(NULL);
}
if (fork() == 0) {
execvp(tokens[0], tokens);
} else {
if (runInBackground == 0) {
wait(NULL);
}
free(tokens);
}
//}
free(tokens);
}
}
\ No newline at end of file
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