Hilbish/nature/commands/cat.lua

33 lines
629 B
Lua
Raw Normal View History

local commander = require 'commander'
local fs = require 'fs'
commander.register('cat', function(args, sinks)
local exit = 0
if #args == 0 then
sinks.out:writeln [[
usage: cat [file]...]]
end
local chunk_size = 2^13 -- 8K buffer size
for _, fName in ipairs(args) do
local f = io.open(fName)
if f == nil then
exit = 1
sinks.out:writeln(string.format('cat: %s: no such file or directory', fName))
goto continue
end
while true do
local block = f:read(chunk_size)
if not block then break end
sinks.out:write(block)
end
sinks.out:writeln("")
::continue::
end
io.flush()
return exit
end)