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
100
101
102
103
104
105
106
107
108
109
110
111
|
/* Unix serial I/O class poll() emulation. */
/*
* Argyll Color Correction System
*
* Author: Graeme W. Gill
* Date: 12/9/2004
*
* Copyright 2004, 2010 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
* see the License2.txt file for licencing details.
*/
#ifdef UNIX
/* Fake up poll() support on systems that only support select() */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <memory.h>
/* select() defined, but not poll(), so emulate poll() */
#if defined(FD_CLR) && !defined(POLLIN)
#include <sys/time.h>
#include "pollem.h"
int pollem(struct pollfd *fds, unsigned long nfds, int timeout) {
int i, nfd;
fd_set rd_ary; /* Select array for read file descriptors */
fd_set wr_ary; /* Select array for write file descriptors */
fd_set ex_ary; /* Select array for exception file descriptors */
struct timeval tv; /* Timeout value */
struct timeval *ptv = &tv; /* Pointer to above */
int result; /* Select return value */
/* Translate files and events */
FD_ZERO(&rd_ary);
FD_ZERO(&wr_ary);
FD_ZERO(&ex_ary);
for (i = nfd = 0; i < nfds; i++) {
fds[i].revents = 0;
if (fds[i].events & POLLIN) {
FD_SET(fds[i].fd, &rd_ary);
if (fds[i].fd > nfd)
nfd = fds[i].fd;
}
if (fds[i].events & POLLPRI) {
FD_SET(fds[i].fd, &ex_ary);
if (fds[i].fd > nfd)
nfd = fds[i].fd;
}
if (fds[i].events & POLLOUT) {
FD_SET(fds[i].fd, &wr_ary);
if (fds[i].fd > nfd)
nfd = fds[i].fd;
}
}
nfd++; /* Maximum file desciptor index + 1 */
/* Translate timeout */
if (timeout == -1) {
ptv = NULL; /* Wait forever */
} else if (timeout == 0) {
tv.tv_sec = 0; /* Return imediately */
tv.tv_usec = 0;
} else { /* Convert milliseconds to seconds and useconds */
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout - tv.tv_sec * 1000) * 1000;
}
/* Do the select */
if ((result = select(nfd, &rd_ary, &wr_ary, &ex_ary, ptv)) > 0) {
for (i = 0; i < nfds; i++) {
fds[i].revents = 0;
if (FD_ISSET(fds[i].fd, &ex_ary))
fds[i].revents |= POLLPRI;
if (FD_ISSET(fds[i].fd, &rd_ary))
fds[i].revents |= POLLIN;
if (FD_ISSET(fds[i].fd, &wr_ary))
fds[i].revents |= POLLOUT;
}
}
return result;
}
#endif /* FD_CLR */
#endif /* UNIX */
|