ACQ2XX_API
fgets_timeout.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <poll.h>
00003 
00004 #include <stdlib.h>
00005 
00006 int fgets_t(char *s, int size, FILE* stream, int timeout)
00007 /* returns >1 on data, 0 on timeout, <0 on error */
00008 {
00009         struct pollfd pollfd;
00010         int rc;
00011 
00012         pollfd.fd = fileno(stream);
00013         pollfd.events = POLLIN;
00014 
00015         rc = poll(&pollfd, 1, timeout);
00016         if (rc > 0){
00017                 return fgets(s, size, stream);
00018         }else{
00019                 return rc;
00020         }
00021 }
00022 
00023 #ifdef TESTHARNESS
00024 int main(int argc, char *argv[]){
00025         char buf[80];
00026         int timeout = argc>1? atoi(argv[1]): 0;
00027 
00028         while(fgets_t(buf, 80, stdin, timeout)){
00029                 printf("input:\"%s\"\n", buf);
00030         }
00031         printf("done\n");
00032         return 0;
00033 }
00034 #endif
00035