Home
use pipe (bugged) - bsleep - Unnamed repository; edit this file 'description' to name the repository. DIR Log DIR Files DIR Refs DIR README DIR LICENSE --- DIR commit dc93ba1a8438c25f25be6ddfb051d48b028f9171 DIR parent 792145777faf160260aaf3b13f78221627c75bec HTML Author: kroovy <me@kroovy.de> Date: Mon, 24 Feb 2025 20:21:00 +0100 use pipe (bugged) Diffstat: M bsleep.c | 37 ++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) --- DIR diff --git a/bsleep.c b/bsleep.c @@ -11,29 +11,54 @@ main(void) { int i, in; pid_t pid; - + int fd[2]; + char c = ' '; + + if (pipe(fd) == -1) { + printf("An error ocurred with opening the pipe\n"); + } if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { perror("signal"); exit(EXIT_FAILURE); } pid = fork(); - + switch (pid) { case -1: perror("fork"); exit(EXIT_FAILURE); + case 0: - /* child */ + /* CHILD */ + + /* setup */ + close(fd[1]); + + /* read button press + count seconds */ for (i=1;;i++) { - printf(" press 'b' to interrupt: %ds\r", i); fflush(stdout); + pread(fd[0], &c, sizeof(char), 0); + printf("\r[ press 'b' to interrupt: %ds ] [ '%c' was pressed ] ", i, c); fflush(stdout); sleep(1); } + /* cleanup */ + close(fd[0]); exit(EXIT_SUCCESS); + default: - /* parent */ + /* PARENT */ + + /* setup */ + close(fd[0]); system("/bin/stty raw -echo"); - while ((in = getchar()) != 'b'); /* loop */ + + /* handle button-press */ + while ((in = getchar()) != 'b') { + write(fd[1], &in, sizeof(char)); + } kill(pid, SIGKILL); + + /* cleanup */ + close(fd[1]); system("/bin/stty cooked echo"); printf("\n"); exit(EXIT_SUCCESS);