pollfd 0.1.0

A simple polling crate
Documentation
/*
 * pollfd.c
 */

#include <sys/poll.h>


int32_t
pollfd(int32_t fd, int32_t event, int32_t timeout)
{
    int err;
    struct pollfd ufd;
    int optval, optlen;

    ufd.fd = fd;
    switch (event) {
        case POLLIN:
            ufd.events = POLLIN;
            break;
        case POLLOUT:
            ufd.events = POLLOUT;
            break;
        default:
            return (1);
    }
    err = poll(&ufd, 1, timeout);
    if (err == 0) {
        // poll() timed out.
        return (0);
    }
    if (err < 0) {
        // poll() hit an error.
        return (1);
    }
    // poll() success.  Sanity check socket.
    optlen = sizeof (optval);
    if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == 0) {
        if (optval != 0) {
            return (1);
        }
    }
    return (0);
}