- while (read(fd, &i, sizeof(i)) == sizeof(i)) {
- if (i.type == 1 && i.value == 1) {
- if (print_usec) {
- printf("%ld.%06ld\n", i.time.tv_sec, i.time.tv_usec);
- } else {
- printf("%ld\n", i.time.tv_sec);
+ while (1) {
+ fd_set ready_inputs = all_inputs;
+ if (select(maxfd+1, &ready_inputs, NULL, NULL, NULL) < 1) {
+ err(EX_IOERR, "select");
+ }
+ for (int fd=0; fd <= maxfd; fd++) {
+ if (FD_ISSET(fd, &ready_inputs)) {
+ int read_return = read(fd, &i, sizeof(i));
+ if (read_return == -1) {
+ err(EX_IOERR, "read");
+ }
+ if (read_return != sizeof(i)) {
+ errx(EX_IOERR, "Unexpected EOF");
+ }
+ if (i.type == EV_KEY && i.value == 1) {
+ if (print_usec) {
+ fprintf(output_file, "%ld.%06ld\n", i.time.tv_sec, i.time.tv_usec);
+ } else {
+ fprintf(output_file, "%ld\n", i.time.tv_sec);
+ }
+ fflush(output_file);
+ }