|
|
|
@ -1,14 +1,19 @@
@@ -1,14 +1,19 @@
|
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <stdio.h> |
|
|
|
|
#include <stdbool.h> |
|
|
|
|
#include <sys/wait.h> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
|
#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) {
@@ -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) {
@@ -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) {
@@ -104,7 +137,6 @@ int repl(void) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
|
|
|
|
|
char *chicken[] = { |
|
|
|
|