From 08d6167988a0bbec72d84ff12f8857f0b4d0b42a Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Wed, 13 May 2020 20:38:21 +0200 Subject: Changing dirs kinda works --- 02_exercise/beispiele/pipe_bidirect/pingpong.c | 205 +++++++++++++------------ 02_exercise/shell.c | 52 ++++++- 2 files changed, 147 insertions(+), 110 deletions(-) diff --git a/02_exercise/beispiele/pipe_bidirect/pingpong.c b/02_exercise/beispiele/pipe_bidirect/pingpong.c index ac5eb1c..f38394f 100644 --- a/02_exercise/beispiele/pipe_bidirect/pingpong.c +++ b/02_exercise/beispiele/pipe_bidirect/pingpong.c @@ -1,124 +1,125 @@ +#include +#include #include #include -#include #include static int split(int fd[2]); -static int redir(int fd, const char* file); +static int redir(int fd, const char *file); static void sig_handler(int sig); static int parent; int main(void) { - ssize_t bytes; - char buf[256]; - int fd[2]; - - // disable buffering for stdout - setvbuf(stdout, NULL, _IONBF, 0); - - // redirect stdout to write into a file - if (redir(1, "log.txt") < 0) { - perror("redir()"); - exit(-1); - } - - // install the signal handler for Ctrl+C - if (signal(SIGINT, sig_handler) == SIG_ERR) { - perror("signal()"); - exit(-1); - } - - // split processes and assign file descriptors for communication - switch (split(fd)) { - case -1: // error - exit(-1); - - case 0: // child - while ((bytes = read(fd[0], buf, sizeof(buf))) > 0) { - printf("[%u]: %s\n", getpid(), buf); - sleep(1); - write(fd[1], "pong", 5); - } - break; - - case 1: // parent - parent = 1; - write(fd[1], "ping", 5); - while ((bytes = read(fd[0], buf, sizeof(buf))) > 0) { - printf("[%u]: %s\n", getpid(), buf); - sleep(1); - write(fd[1], "ping", 5); - } - break; - } - - // close the file descriptors - close(fd[0]); - close(fd[1]); - - return 0; + ssize_t bytes; + char buf[256]; + int fd[2]; + + // disable buffering for stdout + setvbuf(stdout, NULL, _IONBF, 0); + + // redirect stdout to write into a file + if (redir(1, "log.txt") < 0) { + perror("redir()"); + exit(-1); + } + + // install the signal handler for Ctrl+C + if (signal(SIGINT, sig_handler) == SIG_ERR) { + perror("signal()"); + exit(-1); + } + + // split processes and assign file descriptors for communication + switch (split(fd)) { + case -1: // error + exit(-1); + + case 0: // child + while ((bytes = read(fd[0], buf, sizeof(buf))) > 0) { + printf("[%u]: %s\n", getpid(), buf); + sleep(1); + write(fd[1], "pong", 5); + } + break; + + case 1: // parent + parent = 1; + write(fd[1], "ping", 5); + while ((bytes = read(fd[0], buf, sizeof(buf))) > 0) { + printf("[%u]: %s\n", getpid(), buf); + sleep(1); + write(fd[1], "ping", 5); + } + break; + } + + // close the file descriptors + close(fd[0]); + close(fd[1]); + + return 0; } int split(int fd[2]) { - int pfd[2][2]; - - if (pipe(pfd[0]) < 0) { - perror("pipe()"); - goto err_pipe1; - } - - if (pipe(pfd[1]) < 0) { - perror("pipe()"); - goto err_pipe2; - } - - switch (fork()) { - case -1: - perror("fork()"); - goto err_fork; - - case 0: // child process - close(pfd[0][1]); // close write for child - close(pfd[1][0]); // close read for parent - fd[0] = pfd[0][0]; // keep read for child - fd[1] = pfd[1][1]; // keep write for parent - return 0; - - default: // parent process - close(pfd[1][1]); // close write for parent - close(pfd[0][0]); // close read for child - fd[0] = pfd[1][0]; // keep read for parent - fd[1] = pfd[0][1]; // keep write for child - return 1; - } - + int pfd[2][2]; + + if (pipe(pfd[0]) < 0) { + perror("pipe()"); + goto err_pipe1; + } + + if (pipe(pfd[1]) < 0) { + perror("pipe()"); + goto err_pipe2; + } + + switch (fork()) { + case -1: + perror("fork()"); + goto err_fork; + + case 0: // child process + close(pfd[0][1]); // close write for child + close(pfd[1][0]); // close read for parent + fd[0] = pfd[0][0]; // keep read for child + fd[1] = pfd[1][1]; // keep write for parent + return 0; + + default: // parent process + close(pfd[1][1]); // close write for parent + close(pfd[0][0]); // close read for child + fd[0] = pfd[1][0]; // keep read for parent + fd[1] = pfd[0][1]; // keep write for child + return 1; + } + err_fork: - close(pfd[1][0]); - close(pfd[1][1]); + close(pfd[1][0]); + close(pfd[1][1]); err_pipe2: - close(pfd[0][0]); - close(pfd[0][1]); + close(pfd[0][0]); + close(pfd[0][1]); err_pipe1: - return -1; + return -1; } -int redir(int fd, const char* file) { - int fd2 = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644); - - if (fd2 < 0) - return -1; - - dup2(fd2, fd); - close(fd2); - - return 0; +int redir(int fd, const char *file) { + int fd2 = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644); + + if (fd2 < 0) + return -1; + + dup2(fd2, fd); + close(fd2); + + return 0; } void sig_handler(int sig) { - if (parent) { - const char msg[] = "Programmabbruch durch Nutzer\n"; - write(1, msg, sizeof(msg) - 1); - } - exit(0); + if (parent) { + const char msg[] = "Programmabbruch durch Nutzer\n"; + write(1, msg, sizeof(msg) - 1); + } + exit(0); } diff --git a/02_exercise/shell.c b/02_exercise/shell.c index 88b17fd..2594d6b 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -10,12 +10,16 @@ #include #include "array.h" +#include "errno.h" #include "stdbool.h" void print_prompt(char const *const original_wd, char const *current_wd); -int main(void) { +// Returns the return code of the executed programm +int exec_command(char *command); +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(); @@ -23,12 +27,21 @@ int main(void) { char *command = NULL; size_t cap = 0; size_t length = 0; + print_prompt(original_wd, current_wd); if ((length = getline(&command, &cap, stdin)) < 0) { fprintf(stderr, "Failed to read from STDIN"); + exit(-1); } - if (strcmp(command, "exit\n") == 0) { + 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); } free((void *)command); } @@ -37,10 +50,33 @@ int main(void) { free((void *)current_wd); } -void print_prompt(char const *const original_wd, char const *current_wd) { - - int result = strcmp(original_wd, current_wd); - if (result == 0) { - printf("./ > "); +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; + } } -} \ No newline at end of file + 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; + } + } +} + +int exec_command(char *command) { return 0; } -- cgit v1.2.3-54-g00ecf