PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
poll.c
1
2#include "types.h"
3#include "defs.h"
4#include "irx.h"
5#include "stdio.h"
6#include "sysclib.h"
7#include "ps2ip.h"
8
9#include "poll.h"
10
11#define POLL_CAN_READ (POLLIN | POLLRDNORM)
12#define POLL_CAN_WRITE (POLLOUT | POLLWRNORM | POLLWRBAND)
13#define POLL_HAS_EXCP (POLLRDBAND | POLLPRI)
14
15#define POLL_EVENTS_MASK (POLL_CAN_READ | POLL_CAN_WRITE | POLL_HAS_EXCP)
16
17int poll(struct pollfd *fds, unsigned long nfds, int timeout)
18{
19 int i, err;
20 fd_set rfd, wfd, efd, ifd;
21 struct timeval timebuf;
22 struct timeval *tbuf = (struct timeval *)0;
23 int n = 0;
24 int count;
25
26 FD_ZERO(&ifd);
27 FD_ZERO(&rfd);
28 FD_ZERO(&wfd);
29 FD_ZERO(&efd);
30
31 for (i = 0; (unsigned long)i < nfds; i++) {
32 int events = fds[i].events;
33 int fd = fds[i].fd;
34
35 fds[i].revents = 0;
36
37 if ((fd < 0) || (FD_ISSET(fd, &ifd)))
38 continue;
39
40 if (fd > n)
41 n = fd;
42
43 if (events & POLL_CAN_READ)
44 FD_SET(fd, &rfd);
45
46 if (events & POLL_CAN_WRITE)
47 FD_SET(fd, &wfd);
48
49 if (events & POLL_HAS_EXCP)
50 FD_SET(fd, &efd);
51 }
52
53 if (timeout >= 0) {
54 timebuf.tv_sec = timeout / 1000;
55 timebuf.tv_usec = (timeout % 1000) * 1000;
56 tbuf = &timebuf;
57 }
58
59 err = lwip_select(n + 1, &rfd, &wfd, &efd, tbuf);
60
61 if (err < 0)
62 return err;
63
64 count = 0;
65
66 for (i = 0; (unsigned long)i < nfds; i++) {
67 int revents = (fds[i].events & POLL_EVENTS_MASK);
68 int fd = fds[i].fd;
69
70 if (fd < 0)
71 continue;
72
73 if (FD_ISSET(fd, &ifd))
74 revents = POLLNVAL;
75 else {
76 if (!FD_ISSET(fd, &rfd))
77 revents &= ~POLL_CAN_READ;
78
79 if (!FD_ISSET(fd, &wfd))
80 revents &= ~POLL_CAN_WRITE;
81
82 if (!FD_ISSET(fd, &efd))
83 revents &= ~POLL_HAS_EXCP;
84 }
85
86 if ((fds[i].revents = revents) != 0)
87 count++;
88 }
89
90 return count;
91}
Definition poll.h:5
Definition time.h:29
u32 count
start sector of fragmented bd/file