File: poll.lua

package info (click to toggle)
lua-posix 31-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,604 kB
  • ctags: 1,346
  • sloc: sh: 14,309; ansic: 4,794; perl: 76; makefile: 41
file content (24 lines) | stat: -rw-r--r-- 471 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
local P = require 'posix'

local fd1 = P.open(arg[1], P.O_RDONLY)
local fd2 = P.open(arg[2], P.O_RDONLY)

local fds = {
  [fd1] = { events = {IN=true} },
  [fd2] = { events = {IN=true} }
}

while true do
  P.poll(fds,-1)
  for fd in pairs(fds) do
    if  fds[fd].revents.IN then
      local res = P.read(fd,1024)
      P.write(1,res);
    end
    if  fds[fd].revents.HUP then
      P.close(fd)
      fds[fd] = nil
      if not next(fds) then return end
    end
  end
end