watch-react-to-ctrlc.patch (637B)
1 diff --git a/watch.c b/watch.c 2 index 51ac67f..1e5a311 100644 3 --- a/watch.c 4 +++ b/watch.c 5 @@ -3,6 +3,7 @@ 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <unistd.h> 9 +#include <signal.h> 10 11 #include "util.h" 12 13 @@ -12,6 +13,14 @@ usage(void) 14 eprintf("usage: %s [-t] [-n interval] command\n", argv0); 15 } 16 17 +static int running = 1; 18 + 19 +void 20 +ctrlc(int args) 21 +{ 22 + running = 0; 23 +} 24 + 25 int 26 main(int argc, char *argv[]) 27 { 28 @@ -48,7 +57,8 @@ main(int argc, char *argv[]) 29 eprintf("command too long\n"); 30 } 31 32 - for (;;) { 33 + signal(SIGINT, ctrlc); 34 + while (running) { 35 printf("\x1b[2J\x1b[H"); /* clear */ 36 fflush(NULL); 37 system(cmd);