1994-12-02 06:40:24 +00:00
|
|
|
/* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for *
|
|
|
|
* details. If they are missing then this copy is in violation of *
|
|
|
|
* the copyright conditions. */
|
1994-10-07 08:58:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** lib_twait.c
|
|
|
|
**
|
|
|
|
** The routine timed_wait().
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
1994-12-02 06:40:24 +00:00
|
|
|
#include <sys/types.h> /* some systems can't live without this */
|
1994-10-07 08:58:58 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
1994-12-02 06:40:24 +00:00
|
|
|
#if defined(SYS_SELECT)
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
1994-10-07 08:58:58 +00:00
|
|
|
#include "curses.priv.h"
|
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
#if defined(NOUSLEEP)
|
|
|
|
void usleep(unsigned int usec)
|
|
|
|
{
|
|
|
|
struct timeval tval;
|
|
|
|
|
|
|
|
tval.tv_sec = usec / 1000000;
|
|
|
|
tval.tv_usec = usec % 1000000;
|
|
|
|
select(0, NULL, NULL, NULL, &tval);
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1994-10-07 08:58:58 +00:00
|
|
|
int timed_wait(int fd, int wait, int *timeleft)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct timeval timeout;
|
|
|
|
static fd_set set;
|
|
|
|
#ifndef GOOD_SELECT
|
|
|
|
struct timeval starttime, returntime;
|
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
gettimeofday(&starttime, NULL);
|
1994-10-07 08:58:58 +00:00
|
|
|
#endif
|
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(fd, &set);
|
1994-10-07 08:58:58 +00:00
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
/* the units of wait are milliseconds */
|
|
|
|
timeout.tv_sec = wait / 1000;
|
|
|
|
timeout.tv_usec = (wait % 1000) * 1000;
|
1994-10-07 08:58:58 +00:00
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
T(("start twait: sec = %d, usec = %d", timeout.tv_sec, timeout.tv_usec));
|
1994-10-07 08:58:58 +00:00
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
result = select(fd+1, &set, NULL, NULL, &timeout);
|
1994-10-07 08:58:58 +00:00
|
|
|
|
|
|
|
#ifndef GOOD_SELECT
|
1994-12-02 06:40:24 +00:00
|
|
|
gettimeofday(&returntime, NULL);
|
|
|
|
timeout.tv_sec -= (returntime.tv_sec - starttime.tv_sec);
|
|
|
|
timeout.tv_usec -= (returntime.tv_usec - starttime.tv_usec);
|
|
|
|
if (timeout.tv_usec < 0 && timeout.tv_sec > 0) {
|
1994-10-07 08:58:58 +00:00
|
|
|
timeout.tv_sec--;
|
|
|
|
timeout.tv_usec += 1000000;
|
1994-12-02 06:40:24 +00:00
|
|
|
}
|
|
|
|
if (timeout.tv_sec < 0)
|
1994-10-07 08:58:58 +00:00
|
|
|
timeout.tv_sec = timeout.tv_usec = 0;
|
|
|
|
#endif
|
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
/* return approximate time left on the timeout, in milliseconds */
|
|
|
|
if (timeleft)
|
1994-10-07 08:58:58 +00:00
|
|
|
*timeleft = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000);
|
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
T(("end twait: returned %d, sec = %d, usec = %d (%d msec)",
|
|
|
|
result, timeout.tv_sec, timeout.tv_usec, *timeleft));
|
1994-10-07 08:58:58 +00:00
|
|
|
|
1994-12-02 06:40:24 +00:00
|
|
|
return(result);
|
1994-10-07 08:58:58 +00:00
|
|
|
}
|