-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
98 lines (82 loc) · 1.63 KB
/
util.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
#define _POSIX_C_SOURCE 200809L /* Because I want to get all poll constants */
#include <stdio.h>
#include <poll.h>
struct int_str
{
int i;
const char *s;
};
static void
print_ored (struct int_str vals[], int val)
{
int print_pipe = 0;
int remains = val;
for (int i = 0; vals[i].s != NULL; ++i)
{
if (vals[i].i == 0)
{
continue;
}
if (~(val | ~vals[i].i) == 0)
{
if (print_pipe)
{
printf (" | ");
}
else
{
print_pipe = 1;
}
printf ("%s", vals[i].s);
remains &= ~vals[i].i;
}
}
if (remains == 0)
{
if (!print_pipe)
{
printf ("0");
}
}
else
{
if (print_pipe)
{
printf (" | ");
}
printf ("%d", remains);
}
}
#define IS(i) {i, #i}
void
mini_poll (struct pollfd fds[], nfds_t nfds, int timeout)
{
struct int_str poll_ored[] = {
IS (POLLIN),
IS (POLLRDNORM),
IS (POLLRDBAND),
IS (POLLPRI),
IS (POLLOUT),
IS (POLLWRNORM),
IS (POLLWRBAND),
IS (POLLERR),
IS (POLLHUP),
IS (POLLNVAL),
{0}
};
int result = poll (fds, nfds, timeout);
if (result == -1)
{
perror ("mini: mini_poll: poll (...) failed");
return;
}
printf ("poll (...) = %d\n", result);
for (nfds_t i = 0; i != nfds; ++i)
{
printf ("fds[%d] = {fd = %d, events = ", (int)i, fds[i].fd);
print_ored (poll_ored, fds[i].events);
printf (", revents = ");
print_ored (poll_ored, fds[i].revents);
printf ("}\n");
}
}