diff --git a/examples/joystick.c b/examples/joystick.c index 51d65e5..8bce372 100644 --- a/examples/joystick.c +++ b/examples/joystick.c @@ -43,16 +43,24 @@ struct js_event { */ static void joystick_cb(uev_t *w, void *arg, int events) { + ssize_t len; + if (UEV_ERROR == events) { warnx("Spurious problem with the joystick watcher, restarting."); uev_io_start(w); } - if (read(w->fd, &e, sizeof(e)) < 0) { + len = read(w->fd, &e, sizeof(e)); + if (len < 0) { warn("Failed reading joystick event"); return; } + if (len == 0 || UEV_HUP == events) { + warn("Joystick disconnected"); + return; + } + switch (e.type) { case 1: printf("Button %d %s\n", e.number, e.value ? "pressed" : "released"); diff --git a/examples/redirect.c b/examples/redirect.c index a8aefa2..bd2547b 100644 --- a/examples/redirect.c +++ b/examples/redirect.c @@ -35,7 +35,7 @@ void process_stdin(uev_t *w, void *arg, int events) return; } - if (len == 0) { + if (len == 0 || UEV_HUP == events) { warnx("Connection closed."); return; } diff --git a/tests/complete.c b/tests/complete.c index 9e1bb6d..7b1cbe3 100644 --- a/tests/complete.c +++ b/tests/complete.c @@ -72,7 +72,7 @@ static void signal_cb(uev_t *w, void *UNUSED(arg), int events) static void pipe_read_cb(uev_t *w, void UNUSED(*arg), int events) { - int cnt; + int len; char msg[50]; if (UEV_ERROR == events) { @@ -87,13 +87,19 @@ static void pipe_read_cb(uev_t *w, void UNUSED(*arg), int events) uev_timer_set(watchdog, 1000, 0); /* Here we can read() from in or w->fd, either works. */ - cnt = read(w->fd, msg, sizeof(msg)); - if (cnt < 0) { + len = read(w->fd, msg, sizeof(msg)); + if (len < 0) { perror(__func__); goto error; } -// fprintf(stderr, "READ %.*s %d\n", cnt, msg, cnt); - fprintf(stderr, "%.*s.%d ", cnt, msg, cnt); + + if (len == 0 || UEV_HUP == events) { + fprintf(stderr, "Other end of pipe closed, no more data\n"); + return; + } + +// fprintf(stderr, "READ %.*s %d\n", len, msg, len); + fprintf(stderr, "%.*s.%d ", len, msg, len); } static void pipe_write_cb(uev_t *w, void *arg, int UNUSED(events))