83 lines
2.4 KiB
Plaintext
83 lines
2.4 KiB
Plaintext
--:Minify:--
|
|
local name = syscall.getTask(syscall.getpid()).name
|
|
local cloptions = { n = false, f = false, e = false, help = false }
|
|
local args = {}
|
|
|
|
for _, v in ipairs({ ... }) do
|
|
if v:sub(1, 2) == "--" then
|
|
local opt = v:sub(3)
|
|
if cloptions[opt] == nil then
|
|
print(name .. ": unrecognized option '" .. v .. "'")
|
|
syscall.exit(1); return
|
|
end
|
|
cloptions[opt] = true
|
|
elseif v:sub(1, 1) == "-" then
|
|
for i = 2, #v do
|
|
local opt = v:sub(i, i)
|
|
if cloptions[opt] == nil then
|
|
print(name .. ": invalid option '-" .. opt .. "'")
|
|
syscall.exit(1); return
|
|
end
|
|
cloptions[opt] = true
|
|
end
|
|
else
|
|
table.insert(args, v)
|
|
end
|
|
end
|
|
|
|
if cloptions.help then
|
|
print("Usage: " .. name .. " [OPTION]... FILE...")
|
|
print("Print the resolved target of symbolic links.")
|
|
print("")
|
|
print("Options:")
|
|
print(" -f canonicalize: follow every symlink; last component need not exist")
|
|
print(" -e like -f but all components must exist")
|
|
print(" -n do not output trailing newline")
|
|
print(" --help display this help and exit")
|
|
return
|
|
end
|
|
|
|
if #args == 0 then
|
|
print(name .. ": missing operand")
|
|
print("try '" .. name .. " --help' for more information.")
|
|
syscall.exit(1); return
|
|
end
|
|
|
|
local function absPath(p)
|
|
if p:sub(1,1) ~= "/" then
|
|
local d = syscall.getcwd()
|
|
if d:sub(-1) ~= "/" then d = d .. "/" end
|
|
p = d .. p
|
|
end
|
|
return p
|
|
end
|
|
|
|
local anyErr = false
|
|
for _, path in ipairs(args) do
|
|
path = absPath(path)
|
|
|
|
if cloptions.f or cloptions.e then
|
|
local ok, stat = pcall(syscall.stat, path)
|
|
if not ok then
|
|
if cloptions.e then
|
|
print(name .. ": " .. path .. ": " .. tostring(stat))
|
|
anyErr = true
|
|
else
|
|
if not cloptions.n then print(path) else printInline(path) end
|
|
end
|
|
else
|
|
if not cloptions.n then print(path) else printInline(path) end
|
|
end
|
|
else
|
|
local ok, target = pcall(syscall.readlink, path)
|
|
if not ok then
|
|
print(name .. ": " .. path .. ": " .. tostring(target))
|
|
anyErr = true
|
|
else
|
|
if not cloptions.n then print(target) else printInline(target) end
|
|
end
|
|
end
|
|
end
|
|
|
|
if anyErr then syscall.exit(1) end
|