timeoutread.c (412B)
1 #include "timeoutread.h" 2 3 #include "select.h" 4 #include "error.h" 5 #include "readwrite.h" 6 7 ssize_t timeoutread(int t, int fd, char *buf, size_t len) 8 { 9 fd_set rfds; 10 struct timeval tv; 11 12 tv.tv_sec = t; 13 tv.tv_usec = 0; 14 15 FD_ZERO(&rfds); 16 FD_SET(fd,&rfds); 17 18 if (select(fd + 1,&rfds,NULL,NULL,&tv) == -1) return -1; 19 if (FD_ISSET(fd,&rfds)) return read(fd,buf,len); 20 21 errno = error_timeout; 22 return -1; 23 }