From: Roy Marples Date: Sat, 7 Jun 2008 00:11:17 +0000 (+0000) Subject: Add a function to test if a fd has data to be read. X-Git-Tag: v4.0.2~294 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=751e2f818d970ca55fc094647df3ab9345ad3166;p=thirdparty%2Fdhcpcd.git Add a function to test if a fd has data to be read. --- diff --git a/common.c b/common.c index 32df8844..7eeb1d9d 100644 --- a/common.c +++ b/common.c @@ -33,6 +33,7 @@ #ifdef BSD # include #endif +#include #include #include #include @@ -150,6 +151,25 @@ close_fds(void) return 0; } +int +fd_hasdata(int fd) +{ + struct pollfd fds; + int retval; + + if (fd == -1) + return -1; + fds.fd = fd; + fds.events = POLLIN; + fds.revents = 0; + retval = poll(&fds, 1, 0); + if (retval == -1) + return -1; + if (retval > 0 && fds.revents & POLLIN) + return retval; + return 0; +} + int close_on_exec(int fd) { diff --git a/common.h b/common.h index 9df668a8..817ccaaa 100644 --- a/common.h +++ b/common.h @@ -35,6 +35,8 @@ #include #include +#define UNCONST(a) ((void *)(unsigned long)(const void *)(a)) + #if __GNUC__ > 2 || defined(__INTEL_COMPILER) # define _unused __attribute__((__unused__)) #else @@ -70,6 +72,7 @@ int closefrom(int); int close_fds(void); int close_on_exec(int); +int fd_hasdata(int); ssize_t get_line(char **, size_t *, FILE *); int get_time(struct timeval *); time_t uptime(void);