From 9326e6e09a720efeb6080804396e1b29bab6a51e Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Sun, 30 Jul 2023 00:52:44 +0300 Subject: [PATCH] add CTRL+C handler --- ~/C/libserialport/listen.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/~/C/libserialport/listen.c b/~/C/libserialport/listen.c index 3ac3456..3b79f8c 100644 --- a/~/C/libserialport/listen.c +++ b/~/C/libserialport/listen.c @@ -2,9 +2,13 @@ #include #include #include +#include /* Helper function for error handling. */ int check(enum sp_return result); +void handle_sigint(int sig); + +bool INTERRUPT = false; int main(int argc, char **argv) { struct sp_port *serial_port; @@ -59,9 +63,11 @@ int main(int argc, char **argv) { check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE)); puts("Connected."); + signal(SIGINT, handle_sigint); + /* Reading lines from serial port. */ bool reading = true; - while (reading) { + while (reading && !INTERRUPT) { int pos = 0; /* Character-by-character reading. */ @@ -119,3 +125,5 @@ int check(enum sp_return result) { return result; } } + +void handle_sigint(int sig) { INTERRUPT = true; }