From 326e62862c790fac79e412263392680ee5628874 Mon Sep 17 00:00:00 2001 From: alistair Date: Mon, 25 Nov 2019 15:44:26 +1000 Subject: [PATCH] support cd --- builtins.c | 0 builtins.h | 0 main.c | 40 ++++++++++++++++++++++++++++++++++++---- run.c | 18 +++++++++--------- run.h | 9 +++------ 5 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 builtins.c create mode 100644 builtins.h diff --git a/builtins.c b/builtins.c new file mode 100644 index 0000000..e69de29 diff --git a/builtins.h b/builtins.h new file mode 100644 index 0000000..e69de29 diff --git a/main.c b/main.c index 6b6ef29..0d8827e 100644 --- a/main.c +++ b/main.c @@ -1,14 +1,19 @@ #include #include #include +#include +#include +#include + #include "error.h" #include "run.h" -#define INITIALLINEBUFFER 80 +#define INITIAL_LINE_BUFFER 80 #define INITIAL_WORD_SIZE 80 +#define NUM_BUILTINS 2 int readline(FILE* in, char **out) { - int size_of_buffer = INITIALLINEBUFFER; + int size_of_buffer = INITIAL_LINE_BUFFER; char *buffer = calloc(size_of_buffer, sizeof(char)); char character; int count = 0; @@ -82,6 +87,28 @@ int split_string(char **sep_string[], int *num, char *string, char delim) { return ER_FAILURE; } +int check_builtins(char *args[]) { + char *builtins[NUM_BUILTINS] = { + "cd", + }; + + if (!strcmp(args[0], "cd")) { + + if (args[1] == 0) { + chdir(getenv("HOME")); + return ER_SUCCESS; + } + + if (args[2] != 0) { + return ER_FAILURE; + } + + chdir(args[1]); + } + + return ER_FAILURE; +} + int repl(void) { while (true) { char *line; @@ -94,7 +121,13 @@ int repl(void) { int num_words; split_string(&sep_string, &num_words, line, ' '); - execute(stdin, stdout, sep_string); + pid_t child; + + if (check_builtins(sep_string)) { + execute(stdin, stdout, sep_string, &child); + } + + waitpid(child, NULL, 0); for (int i = 0; i < num_words; i++) { free(sep_string[i]); @@ -104,7 +137,6 @@ int repl(void) { } } - int main(int argc, char** argv) { char *chicken[] = { diff --git a/run.c b/run.c index 9b47d2f..00c64d6 100644 --- a/run.c +++ b/run.c @@ -1,11 +1,18 @@ #include "run.h" +/* Contains builtin functions and code for executing system programs */ + +int change_dir(char *dir) { + return chdir(dir); +} + + /* Fork and exec a command given in args taking input from in and sending * output to out. The arguments array must end in a NULL. * * Returns true on success or 1 on failure. */ -int execute(FILE *in, FILE *out, char *args[]) { +int execute(FILE *in, FILE *out, char *args[], pid_t* child_ID) { int in_FD = fileno(in); int out_FD = fileno(out); @@ -16,6 +23,7 @@ int execute(FILE *in, FILE *out, char *args[]) { } if (err) { // parent + *child_ID = err; return ER_SUCCESS; } else { // child dup2(in_FD, 0); @@ -27,11 +35,3 @@ int execute(FILE *in, FILE *out, char *args[]) { return ER_SUCCESS; } -int test_exec(void) { - char *arg[] = {"ls", "lah", 0}; - - execute(stdin, stdout, arg); - - return 0; -} - diff --git a/run.h b/run.h index 4e3f57e..0adca76 100644 --- a/run.h +++ b/run.h @@ -8,11 +8,8 @@ #ifndef RUNH #define RUNH -/* Fork and exec a command given in args taking input from in and sending - * output to out. The arguments array must end in a NULL. - * - * Returns true on success or 1 on failure. - */ -int execute(FILE* in, FILE* out, char *args[]); +int execute(FILE *in, FILE *out, char *args[], pid_t* child_ID); + +int change_dir(char *dir); #endif