/* * Wait and read single byte input from device. * $Id$ * Copyright (c) 1997 by Tycho Softworks. * For conditions of distribution and reuse see product license. */ #include #include #include #ifndef SYS_POLL_H_MISSING #define USE_POLL #endif #ifndef POLL_H_MISSING #define USE_POLL #endif #ifdef USE_POLL int inkey(fd_t fd, int timeout) { struct pollfd poller; uchar buf; poller.fd = fd; poller.revents = 0; poller.events = POLLIN ; if(!poll(&poller, 1, timeout)) return -1; if(poller.revents & (POLLERR | POLLHUP)) return -1; if(read(fd, &buf, 1) != 1) return -1; return buf; } #else int inkey(fd_t fd, int timeout) { fd_set inp,out,exc; struct timeval timer; uchar buf; FD_ZERO(&inp); FD_ZERO(&out); FD_ZERO(&exc); FD_SET(fd, &inp); FD_SET(fd, &exc); timer.tv_sec = timeout / 1000; timer.tv_usec = timeout % 1000; if(!select(fd + 1, &inp, &out, &exc, &timer)) return -1; if(FD_ISSET(fd, &exc)) return -1; if(FD_ISSET(fd, &inp)) { if(read(fd, &buf, 1) != 1) return -1; return buf; } return -1; } #endif