Files
greyhack/source/ll.src

86 lines
2.0 KiB
Plaintext

if params.len > 0 and (params[0] == "-h" or params[0] == "--help") then
current_program = get_shell.host_computer.File(program_path)
print "Usage: " + current_program.name + " [path...]"
exit
end if
computer = get_shell.host_computer
entry_line = function(f, display_name)
name = display_name
if f.is_folder then
name = name + "/"
else if f.permissions.indexOf("x") != null then
name = name + "*"
end if
return f.permissions + " " + f.owner + " " + f.group + " " + f.size + " " + name
end function
list_path = function(target, raw_path)
if not target then
print raw_path + ": No such file or directory"
return
end if
if not target.is_folder then
print format_columns(entry_line(target, target.name))
return
end if
if not target.has_permission("r") then
print raw_path + ": Permission denied"
return
end if
lines = entry_line(target, ".")
parent = target.parent
if parent then
lines = lines + "\n" + entry_line(parent, "..")
end if
folders = target.get_folders
if not folders then folders = []
files = target.get_files
if not files then files = []
named = []
for f in folders
named.push {
"name": f.name,
"entry": f,
}
end for
named.sort "name"
for item in named
lines = lines + "\n" + entry_line(item.entry, item.name)
end for
named = []
for f in files
named.push {
"name": f.name,
"entry": f,
}
end for
named.sort "name"
for item in named
lines = lines + "\n" + entry_line(item.entry, item.name)
end for
print format_columns(lines)
end function
resolve = function(p)
if p == "." then return computer.File(current_path)
if p == ".." then return computer.File(parent_path(current_path))
target = computer.File(p)
if target then return target
if current_path == "/" then return computer.File("/" + p)
return computer.File(current_path + "/" + p)
end function
if params.len < 1 then
list_path computer.File(current_path), current_path
else
i = 0
for p in params
if i > 0 then print("")
if params.len > 1 then print(p + ":")
list_path resolve(p), p
i = i + 1
end for
end if