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
|
-- e.g. lua tests/test-select.lua <(while :; do echo hello; sleep 1; done) <(while :; do echo world; sleep 1; done)
local l = require "luxio"
local fds = {}
function eet(path, fd, set)
r, errno = l.read(fd, 8192)
if r == -1 then
io.stderr:write(("Couldn't read `%s': %s\n"):format(path, l.strerror(errno)))
return -1
end
w, errno = l.write(l.STDOUT_FILENO, r, 0)
if w == -1 then
io.stderr:write(("Error writing to STDOUT: %s\n"):format(l.strerror(errno)))
return -1
end
return w
end
for _, path in ipairs(arg) do
fd, errno = l.open(path, l.O_RDONLY)
if fd == nil then
io.stderr:write(("Couldn't open file `%s': %s\n"):format(path, l.strerror(errno)))
os.exit(1)
end
fds[#fds + 1] = fd
end
local readset = l.fd_set_new()
l.fd_set_resize(readset, l.FD_SETSIZE)
timeout = l.zero_timeval()
while true do
timeout.tv_sec = 1
l.FD_ZERO(readset)
max_fd = -1
for _, fd in ipairs(fds) do
l.FD_SET(fd, readset)
max_fd = math.max(fd, max_fd)
end
if max_fd == -1 then
break
end
n, errno = l.select(max_fd + 1, readset, nil, nil, timeout)
if n == -1 then
io.stderr:write(("select: %s\n"):format(l.strerror(errno)))
os.exit(1)
end
if n > 0 then
for i, fd in ipairs(fds) do
if fd ~= -1 and l.FD_ISSET(fd, readset) then
path = arg[i]
n = eet(path, fd)
if n == 0 then
l.close(fd)
fds[i] = -1
elseif n == -1 then
os.exit(1)
end
end
end
end
print(readset)
end
|