-
Notifications
You must be signed in to change notification settings - Fork 1
/
excldep.c
99 lines (84 loc) · 2.31 KB
/
excldep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Allegro CL dependent C helper routines for CLX
*/
/*
* This code requires select and interval timers.
* This means you probably need BSD, or a version
* of Unix with select and interval timers added.
*/
# ifdef __alpha
# pragma pointer_size (save)
# pragma pointer_size (long)
# endif
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <stdio.h>
# ifdef __alpha
# pragma pointer_size (restore)
# endif
#define ERROR -1
#define INTERRUPT -2
#define TIMEOUT 0
#define SUCCESS 1
#ifdef FD_SETSIZE
#define NUMBER_OF_FDS FD_SETSIZE /* Highest possible file descriptor */
#else
#define NUMBER_OF_FDS 32
#endif
/* Length of array needed to hold all file descriptor bits */
#define CHECKLEN ((NUMBER_OF_FDS+8*sizeof(int)-1) / (8 * sizeof(int)))
extern int errno;
/*
* This function waits for input to become available on 'fd'. If timeout is
* 0, wait forever. Otherwise wait 'timeout' seconds. If input becomes
* available before the timer expires, return SUCCESS. If the timer expires
* return TIMEOUT. If an error occurs, return ERROR. If an interrupt occurs
* while waiting, return INTERRUPT.
*/
int fd_wait_for_input(fd, timeout)
register int fd;
register int timeout;
{
struct timeval timer;
register int i;
#ifdef FD_SETSIZE
fd_set checkfds;
#else
int checkfds[CHECKLEN];
#endif
if (fd < 0 || fd >= NUMBER_OF_FDS) {
fprintf(stderr, "Bad file descriptor argument: %d to fd_wait_for_input\n", fd);
fflush(stderr);
}
#ifdef FD_SETSIZE
FD_ZERO(&checkfds);
FD_SET(fd, &checkfds);
if (timeout) {
timer.tv_sec = timeout;
timer.tv_usec = 0;
i = select(NUMBER_OF_FDS, &checkfds, (fd_set *)0, (fd_set *)0, &timer);
} else
i = select(NUMBER_OF_FDS, &checkfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
#else
for (i = 0; i < CHECKLEN; i++)
checkfds[i] = 0;
checkfds[fd / (8 * sizeof(int))] |= 1 << (fd % (8 * sizeof(int)));
if (timeout) {
timer.tv_sec = timeout;
timer.tv_usec = 0;
i = select(32, checkfds, (int *)0, (int *)0, &timer);
} else
i = select(32, checkfds, (int *)0, (int *)0, (struct timeval *)0);
#endif
if (i < 0)
/* error condition */
if (errno == EINTR)
return (INTERRUPT);
else
return (ERROR);
else if (i == 0)
return (TIMEOUT);
else
return (SUCCESS);
}