summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2013-09-07 22:49:08 +0200
committerPali Rohár <pali.rohar@gmail.com>2013-09-07 22:49:08 +0200
commit55bfee79a2c2f946a9e7f757670fae08fd8d4810 (patch)
tree26a1a22e16c069e62084fffc83d1903a5eba9afc
parent287cb102507d07633d804795605a77c3e89cdf5a (diff)
download0xFFFF-55bfee79a2c2f946a9e7f757670fae08fd8d4810.tar.bz2
usb-device: Handle Ctrl+C SIGINT and quit wait loop
-rw-r--r--src/usb-device.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/usb-device.c b/src/usb-device.c
index eab5c92..e281440 100644
--- a/src/usb-device.c
+++ b/src/usb-device.c
@@ -23,6 +23,7 @@
#include <string.h>
#include <errno.h>
#include <ctype.h>
+#include <signal.h>
#include <usb.h>
@@ -234,11 +235,21 @@ static struct usb_device_info * usb_search_device(struct usb_device * dev, int l
}
+static volatile sig_atomic_t signal_quit;
+
+static void signal_handler(int signum) {
+
+ signal_quit = 1;
+ (void)signum;
+
+}
+
struct usb_device_info * usb_open_and_wait_for_device(void) {
struct usb_bus * bus;
struct usb_device_info * ret = NULL;
int i = 0;
+ sighandler_t prev;
static char progress[] = {'/','-','\\', '|'};
usb_init();
@@ -247,7 +258,11 @@ struct usb_device_info * usb_open_and_wait_for_device(void) {
PRINTF_BACK();
printf("\n");
- while ( 1 ) {
+ signal_quit = 0;
+
+ prev = signal(SIGINT, signal_handler);
+
+ while ( ! signal_quit ) {
PRINTF_LINE("Waiting for USB device... %c", progress[++i%sizeof(progress)]);
@@ -278,6 +293,9 @@ struct usb_device_info * usb_open_and_wait_for_device(void) {
}
+ if ( prev != SIG_ERR )
+ signal(SIGINT, prev);
+
PRINTF_BACK();
printf("\n");