/*
 * Select roundup preload.  (casper....@you.know.where)
 Note by David Kirkby. The follwing method to compile is slightly different
 from that given by Casper, as Mathematica is a 64-bit binary. 
 * cc -m64 -G -Kpic select_preload.c -o select_preload.so
 *
 */

#include <dlfcn.h>
#include <sys/time.h>

#define FUN_PROTO(type,internal,parms) \
        type internal parms

#define DECLARE(type,name, parms) static FUN_PROTO(type,(*name), parms)
#define CAST(type, parms) (FUN_PROTO(type,(*), parms))

DECLARE(int,next_select,(int, fd_set *, fd_set *, fd_set *, struct timeval *));

#ifdef __GNUC__
void loadit(void) __attribute__ ((constructor));
#else
#pragma init(loadit)
#endif

void
loadit(void)
{
    extern char **environ;
    char **env;
    int offset;

    next_select = CAST(int, (int, fd_set *, fd_set *, fd_set *, struct timeval *
))dlsym(RTLD_NEXT, "select");

}

int select(int nfds, fd_set *restrict readfds, fd_set  *restrict  writefds,
    fd_set  *restrict errorfds, struct timeval *restrict timeout)
{

        if (timeout != NULL && timeout->tv_sec == 0 && timeout->tv_usec > 0 &&
            timeout->tv_usec < 1000)
                timeout->tv_usec = 1000;

        return (next_select(nfds, readfds, writefds, errorfds, timeout));

} 
