From 8da59918c22df70eaec28150867c7e8bfd4bc1ae Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Thu, 21 May 2020 14:11:38 +0200 Subject: sdfghj --- 02_exercise/beispiele/pipe_bidirect/log.txt | 22 ++++++++++++++++++---- 02_exercise/beispiele/pipe_example/pipe.c | 16 ++++++++-------- 02_exercise/prog.c | 1 + 02_exercise/prompt_utils.c | 2 +- 02_exercise/shell.c | 14 ++++++++++---- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/02_exercise/beispiele/pipe_bidirect/log.txt b/02_exercise/beispiele/pipe_bidirect/log.txt index fbdc2c7..c59b0bb 100644 --- a/02_exercise/beispiele/pipe_bidirect/log.txt +++ b/02_exercise/beispiele/pipe_bidirect/log.txt @@ -1,5 +1,19 @@ -[15234]: ping -[15233]: pong -[15234]: ping -[15233]: pong +[3225581]: ping +[3225580]: pong +[3225581]: ping +[3225580]: pong +[3225581]: ping +[3225580]: pong +[3225581]: ping +[3225580]: pong +[3225581]: ping +[3225580]: pong +[3225581]: ping +[3225580]: pong +[3225581]: ping +[3225580]: pong +[3225581]: ping +[3225580]: pong +[3225581]: ping +[3225580]: pong Programmabbruch durch Nutzer diff --git a/02_exercise/beispiele/pipe_example/pipe.c b/02_exercise/beispiele/pipe_example/pipe.c index 3589c63..422e4d5 100644 --- a/02_exercise/beispiele/pipe_example/pipe.c +++ b/02_exercise/beispiele/pipe_example/pipe.c @@ -35,26 +35,26 @@ void term(const char *msg) { int main(void) { int ret = pipe(pipe_fds); - + if (ret < 0) term("Couldn't create pipe"); - + char out_buf[] = "hello"; ret = write(pipe_fds[1], out_buf, strlen(out_buf) + 1); - + if (ret < 0) term("Couldn't write to pipe"); - + printf("send msg: %s\n", out_buf); - + char in_buf[BUF_SIZE]; memset(in_buf, 0, BUF_SIZE); - + ret = read(pipe_fds[0], in_buf, BUF_SIZE - 1); - + if (ret < 0) term("Couldn't read from pipe"); - + printf("recv msg: %s\n", in_buf); return 0; } diff --git a/02_exercise/prog.c b/02_exercise/prog.c index 17b727a..41c0545 100644 --- a/02_exercise/prog.c +++ b/02_exercise/prog.c @@ -45,6 +45,7 @@ int main(int argc, const char* argv[]) { } printf("Delay: %ds, Exit Code: %d\n", delay, rc); + fflush(stdout); sleep(delay); return rc; diff --git a/02_exercise/prompt_utils.c b/02_exercise/prompt_utils.c index b7c836b..d43a6b6 100644 --- a/02_exercise/prompt_utils.c +++ b/02_exercise/prompt_utils.c @@ -58,7 +58,7 @@ char const *relative_path(char const *const from_dir, char const *const to_dir) } else { char const *const go_up = "/.."; size_t length = strlen(to_dir + common_position) + strlen("..") + - strlen(go_up) * (levels_up - 1); + strlen(go_up) * (levels_up - 1) + 1; return_value = malloc(length * sizeof(char)); strcpy(return_value, ".."); for (size_t j = 0; j < levels_up - 1; ++j) { diff --git a/02_exercise/shell.c b/02_exercise/shell.c index ed53897..1bdeab6 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -31,6 +32,10 @@ int main(void) { exit(-1); } + if (strspn(line, " \n\t") == strlen(line)) { + continue; + } + line[length - 1] = '\0'; // cut the line feed char **arguments = NULL; @@ -82,11 +87,11 @@ int parse_line(char const *line, char ***parts, size_t *part_count) { return -1; } - char c; - int i = 0; - while (true) { - c = line[i++]; + for (size_t i = 0; i < strlen(line) + 1; ++i) { + char c = line[i]; if (c == ' ' || c == '\0') { + if (arrayLen(part) == 0) + continue; arrayPush(part) = '\0'; arrayPush(local_parts) = part; if (c == '\0') { @@ -131,6 +136,7 @@ int exec_command(const char *path, char *const argv[], unsigned timeout) { close(pipefd[1]); execvp(path, argv); fprintf(stderr, "could not execute \"%s\"\n", path); + fflush(stderr); exit(-1); } -- cgit v1.2.3-54-g00ecf