From 182788482e8bd01c70bbe6af3f7ed79765d4fe53 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Tue, 19 May 2020 19:58:54 +0200 Subject: exec shit works, kinda --- 02_exercise/shell.c | 122 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 42 deletions(-) diff --git a/02_exercise/shell.c b/02_exercise/shell.c index 2594d6b..75e77b7 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -11,72 +11,110 @@ #include "array.h" #include "errno.h" -#include "stdbool.h" +#include -void print_prompt(char const *const original_wd, char const *current_wd); +void print_prompt(); -// Returns the return code of the executed programm -int exec_command(char *command); +// returns the return code of the executed program +int exec_command(const char *path, char *const argv[], unsigned timeout); -int main(void) { - // the directory is specified without a / at the end - char const *const original_wd = get_current_dir_name(); - char const *current_wd = get_current_dir_name(); +int main(int argc, char* argv[]) { + + char *argument; + char **arguments; + if (arrayInit(argument) != 0 || arrayInit(arguments) != 0) { + fprintf(stderr, "Failed to prepare new argument array"); + exit(-1); + } while (true) { char *command = NULL; size_t cap = 0; size_t length = 0; - print_prompt(original_wd, current_wd); + print_prompt(); if ((length = getline(&command, &cap, stdin)) < 0) { fprintf(stderr, "Failed to read from STDIN"); exit(-1); } + + command[length - 1] = '\0'; // cut the line feed +/* + char c; + int i = 0; + while (true) { + c = command[i++]; + if (c == ' ') { + arrayPush(argument) = '\0'; + arrayPush(arguments) = argument; + printf("+ %s\n", argument); + arrayRelease(argument); + arrayInit(argument); + } else if (c == '\0') { + + } else { + arrayPush(argument) = c; + } + } +*/ if (strstr(command, "cd") != NULL) { printf("Changing dirs \n"); chdir("beispiele"); - free((void *) current_wd); - current_wd = get_current_dir_name(); } else if (strcmp(command, "exit\n") == 0) { exit(0); } else { - exec_command(command); + exec_command(command, (char *const []) {command, NULL}, 0); } free((void *)command); } - // Gotta cast to void otherwise we discard qualifiers - free((void *)original_wd); - free((void *)current_wd); } -void print_prompt(char const *const original_wd, char const *const current_wd) { - // easiest case first - { - int result = strcmp(original_wd, current_wd); - if (result == 0) { - printf(".> "); - return; - } - } - printf("%s > ", current_wd); - // Now checking for substrings aka are we in a subdir - { - size_t dirlen = strlen(original_wd); - char *const orig_dir_with_slash = malloc(dirlen + 2); - if (orig_dir_with_slash == NULL) { - fprintf(stderr, "Failed to allocate for prompt"); - exit(ENOMEM); - } - memcpy(orig_dir_with_slash, original_wd, dirlen); - orig_dir_with_slash[dirlen] = '/'; - orig_dir_with_slash[dirlen + 1] = '\0'; - char const *const result = strstr(current_wd, original_wd); - if (result != NULL) { - printf(".%s > ", current_wd + dirlen); - return; - } +void print_prompt() { + size_t length = 1024; + char *buffer = malloc(length * sizeof(char)); + getcwd(buffer, length); + if (buffer == NULL) { + printf(".> "); + } else { + printf("%s > ", buffer); } } -int exec_command(char *command) { return 0; } +int exec_command(const char *path, char *const argv[], unsigned timeout) { + int pid; + int pipefd[2]; + int status; + char buf[512]; + + pipe(pipefd); + if ((pid = fork()) == 0) { + close(pipefd[0]); + dup2(pipefd[1], 1); // includes close(1); + close(pipefd[1]); + execvp(path, argv); + fprintf(stderr, "no program\n"); + exit(-1); + } + + if (pid < 0) { + fprintf(stderr, "no fork\n"); + exit(-2); + } + + close(pipefd[1]); + + size_t length = 0; + if ((length = read(pipefd[0], buf, 512)) <= 0) { + fprintf(stderr, "error\n"); + exit(-3); + } + + waitpid(pid, &status, 0); + + close(pipefd[0]); + + buf[length] = '\0'; + + printf("%s", buf); + return WEXITSTATUS(status); +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf