]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Add a function to test if a fd has data to be read.
authorRoy Marples <roy@marples.name>
Sat, 7 Jun 2008 00:11:17 +0000 (00:11 +0000)
committerRoy Marples <roy@marples.name>
Sat, 7 Jun 2008 00:11:17 +0000 (00:11 +0000)
common.c
common.h

index 32df8844b20d9bdfea9a01dccdc9d15f9bbbcdd6..7eeb1d9d268c032c7c2cfc1c03d73750886f5a62 100644 (file)
--- a/common.c
+++ b/common.c
@@ -33,6 +33,7 @@
 #ifdef BSD
 #  include <paths.h>
 #endif
+#include <poll.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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)
 {
index 9df668a809cde7a4d60fc868ea2bfc6260a76233..817ccaaa2f56a0dc75ef3c03142d98d5512bb312 100644 (file)
--- a/common.h
+++ b/common.h
@@ -35,6 +35,8 @@
 #include <stdio.h>
 #include <string.h>
 
+#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);