forked from devkitPro/wut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.c
More file actions
105 lines (88 loc) · 2.17 KB
/
select.c
File metadata and controls
105 lines (88 loc) · 2.17 KB
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
#include "wut_socket.h"
int
select(int nfds,
fd_set *readfds,
fd_set *writefds,
fd_set *exceptfds,
struct timeval *timeout)
{
int cnv_nfds = 0, rc, i;
nsysnet_fd_set cnv_rd, cnv_wr, cnv_ex;
struct nsysnet_timeval cnv_timeout;
NSYSNET_FD_ZERO(&cnv_rd);
NSYSNET_FD_ZERO(&cnv_wr);
NSYSNET_FD_ZERO(&cnv_ex);
for (i = 0; i < nfds; i++) {
int rd_fd, wr_fd, ex_fd, cnv_fd;
rd_fd = readfds && FD_ISSET(i, readfds);
wr_fd = writefds && FD_ISSET(i, writefds);
ex_fd = exceptfds && FD_ISSET(i, exceptfds);
if (!rd_fd && !wr_fd && !ex_fd) {
continue;
}
cnv_fd = __wut_get_nsysnet_fd(i);
if (cnv_fd == -1) {
return -1;
}
if ((cnv_fd + 1) > cnv_nfds) {
cnv_nfds = cnv_fd + 1;
if (cnv_nfds > NSYSNET_FD_SETSIZE) {
errno = EINVAL;
return -1;
}
}
if (rd_fd) {
NSYSNET_FD_SET(cnv_fd, &cnv_rd);
}
if (wr_fd) {
NSYSNET_FD_SET(cnv_fd, &cnv_wr);
}
if (ex_fd) {
NSYSNET_FD_SET(cnv_fd, &cnv_ex);
}
}
if (timeout) {
cnv_timeout.tv_sec = timeout->tv_sec;
cnv_timeout.tv_usec = timeout->tv_usec;
}
rc = RPLWRAP(select)(cnv_nfds,
readfds ? &cnv_rd : NULL,
writefds ? &cnv_wr : NULL,
exceptfds ? &cnv_ex : NULL,
timeout ? &cnv_timeout : NULL);
rc = __wut_get_nsysnet_result(NULL, rc);
if (rc == -1) {
return rc;
}
if (readfds) {
FD_ZERO(readfds);
}
if (writefds) {
FD_ZERO(writefds);
}
if (exceptfds) {
FD_ZERO(exceptfds);
}
if (rc == 0)
return 0;
rc = 0;
for (i = 0; i < nfds; i++) {
int cnv_fd = __wut_get_nsysnet_fd(i);
if (cnv_fd == -1) {
continue;
}
if (NSYSNET_FD_ISSET(cnv_fd, &cnv_rd)) {
FD_SET(i, readfds);
rc++;
}
if (NSYSNET_FD_ISSET(cnv_fd, &cnv_wr)) {
FD_SET(i, writefds);
rc++;
}
if (NSYSNET_FD_ISSET(cnv_fd, &cnv_ex)) {
FD_SET(i, exceptfds);
rc++;
}
}
return rc;
}