File: uvtee.lua

package info (click to toggle)
lua-luv 1.51.0-1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,464 kB
  • sloc: ansic: 6,996; makefile: 74; sh: 33
file content (34 lines) | stat: -rw-r--r-- 756 bytes parent folder | download | duplicates (4)
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
local uv = require('luv')

if not arg[1] then
    print(string.format("please run %s filename",arg[0]))
    return
end


local stdin = uv.new_tty(0, true)
local stdout = uv.new_tty(1, true)
--local stdin_pipe = uv.new_pipe(false)
--uv.pipe_open(stdin_pipe,0)

local fname = arg[1]

uv.fs_open(fname, 'w+', tonumber('644', 8), function(err,fd)
    if err then
        print("error opening file:"..err)
    else
        local fpipe = uv.new_pipe(false)
        uv.pipe_open(fpipe, fd)

        uv.read_start(stdin, function(err,chunk)
            if err then
                print('Read error: '..err)
            else
                uv.write(stdout,chunk)
                uv.write(fpipe,chunk)
            end
        end);
    end
end)

uv.run('default')