1 /*
2 * The following Copyright Notice references the use of the poll
3 * stub below. The GPL license is not in effect for the following code snippet
4 * braced by the ifdef __MACH__. You are free to embed the following software
5 * and copyright notice without the encumberances of the GNU General Public
6 * License.
7 *
8 * Copyright (c) 2002 Mark Salyzyn
9 * Copyright (c) 2004
10 * All rights reserved.
11 *
12 * modified by John L. Chmielewski:
13 * created poll.h and moved lines there, events and revents were reversed,
14 * added switch statement to look at return value of select(), added more
15 * poll() defines, modified logic to find fd that requires service
16 *
17 * TERMS AND CONDITIONS OF USE
18 *
19 * Redistribution and use in source form, with or without modification, are
20 * permitted provided that redistributions of source code must retain the
21 * above copyright notice, this list of conditions and the following
22 * disclaimer.
23 *
24 * This software is provided `as is' by Mark Salyzyn and any express or implied
25 * warranties, including, but not limited to, the implied warranties of
26 * merchantability and fitness for a particular purpose, are disclaimed. In no
27 * event shall Mark Salyzyn be liable for any direct, indirect, incidental,
28 * special, exemplary or consequential damages (including, but not limited to,
29 * procurement of substitute goods or services; loss of use, data, or profits;
30 * or business interruptions) however caused and on any theory of liability,
31 * whether in contract, strict liability, or tort (including negligence or
32 * otherwise) arising in any way out of the use of this software, even if
33 * advised of the possibility of such damage.
34 */
35
36 #ifdef __MACH__
37
38 #include "ncid_poll.h"
39
poll(struct pollfd * pfds,unsigned int npfds,int ptimeout)40 int poll(struct pollfd * pfds, unsigned int npfds, int ptimeout)
41 {
42 fd_set readfds;
43 fd_set writefds;
44 fd_set exceptfds;
45 struct timeval timeout = { ptimeout / 1000, (ptimeout % 1000) * 1000 };
46 int maxfd = 0;
47 int retVal = 0;
48 unsigned int i;
49
50 FD_ZERO(&readfds);
51 FD_ZERO(&writefds);
52 FD_ZERO(&exceptfds);
53
54 /* Set up fds */
55 if (pfds != NULL) {
56 for (i = 0; i < npfds; i++) {
57 pfds[i].revents = 0;
58 if (pfds[i].fd == -1) continue;
59 if (pfds[i].fd > maxfd) maxfd = pfds[i].fd;
60 if (pfds[i].events & POLLIN)
61 FD_SET(pfds[i].fd, &readfds);
62 if (pfds[i].events & POLLOUT)
63 FD_SET(pfds[i].fd, &writefds);
64 if (pfds[i].events & POLLPRI)
65 FD_SET(pfds[i].fd, &exceptfds);
66 }
67 }
68
69 switch (retVal = select(maxfd+1, &readfds, &writefds, &exceptfds,
70 (ptimeout > 0) ? &timeout : (struct timeval *)NULL))
71 {
72 case -1: /* error */
73 /* perror("select()"); */
74 break;
75 case 0: /* timeout */
76 break;
77 default: /* 1 or more events reported */
78 retVal = 0;
79 for (i = 0; i < npfds; i++) {
80 pfds[i].revents = 0;
81 if (pfds[i].fd == -1) continue;
82 if (FD_ISSET(pfds[i].fd, &readfds))
83 pfds[i].revents |= POLLIN;
84 if (FD_ISSET(pfds[i].fd, &writefds))
85 pfds[i].revents |= POLLOUT;
86 if (FD_ISSET(pfds[i].fd, &exceptfds))
87 pfds[i].revents |= POLLPRI;
88 if (pfds[i].revents != 0)
89 retVal++;
90 }
91
92 break;
93 }
94 return retVal;
95 }
96
97 #endif /* __MACH__ */
98