2 lines
3.8 KiB
Plaintext
2 lines
3.8 KiB
Plaintext
local a={a=false,h=false,l=false,help=false}local b={...}local c={}local d=syscall.getTask(syscall.getpid()).name;for e,f in pairs(b)do if f:sub(1,2)=="--"then local g=f:sub(3)if a[g]==nil then print(d..": unrecognized option '"..f.."'.")print("try '"..d.." --help' for more information.")return end;a[g]=true elseif f:sub(1,1)=="-"then for h=2,#f do local g=f:sub(h,h)if a[g]==nil then print(d..": invalid option '-"..g.."'.")print("try '"..d.." --help' for more information.")return end;a[g]=true end else table.insert(c,f)end end;if a.help then print("Usage: "..d.." [OPTION]... [DIR]")print("List all entries in the specified DIRectory, or cwd if not specified.")print("")print("Options:")print(" -a do not ignore entries starting with .")print(" -h with -l, print sizes in human readable format")print(" -l use a long listing format")print(" --help display this help and exit")return end;local i=require("fs")local j=c[1]or""if j:sub(1,1)~="/"then j=syscall.getcwd().."/"..j end;if j:sub(-1)~="/"then j=j.."/"end;if not i.isDir(j)then print(d..": cannot access '"..(c[1]or j).."': no such directory")return end;local function k(l,m)local function n(o)return math.floor(l/2^o)%2==1 end;local p;if m==0x01 then p="l"elseif m==nil then p="-"else p="-"end;local q=n(5)and"r"or"-"local r=n(4)and"w"or"-"local s=n(9)and(n(6)and"s"or"x")or(n(6)and"S"or"-")local t=n(3)and"r"or"-"local u=n(2)and"w"or"-"local v=n(8)and"x"or"-"local w=n(1)and"r"or"-"local x=n(0)and"w"or"-"local y=n(7)and"x"or"-"return p..q..r..s..t..u..v..w..x..y end;local z={"K","M","G","T"}local function A(B)local C=0;while B>=1024 and C<#z do B=B/1024;C=C+1 end;if C==0 then return tostring(B).."B"end;if B<10 then return string.format("%.1f%s",B,z[C])end;return math.floor(B)..z[C]end;local D=syscall.devctl(1,"size")local E=tonumber(D:match("^(%d+)"))or 80;local F=i.list(j)if not a.a then for h=#F,1,-1 do if F[h]:sub(1,1)=="."then table.remove(F,h)end end end;table.sort(F)if#F==0 then return end;if a.l then for e,f in ipairs(F)do local G=j..f;local H=syscall.lstat and syscall.lstat(G)or syscall.stat(G)local I=i.isDir(G)local J=H and H.etype==0x01;local isSock=H and H.etype==0x02;local K;if J then K="l"elseif I then K="d"elseif isSock then K="s"else K="-"end;local L;if H and H.perms then L=k(H.perms,H.etype)else L=K.."---------"end;local B=H and H.size or 0;local M=a.h and A(B)or tostring(B)local N=H and H.modified and math.floor(H.modified/1000)or 0;local O=H and tostring(H.owner)or"0"local P=H and tostring(H.group)or"0"printInline(L.." "..O.." "..P.." ")printInline(string.format("%6s",M).." ")printInline(tostring(N).." ")if J then syscall.devctl(1,"sfgc",0x00FFFF)printInline(f)syscall.devctl(1,"sfgc",0xFFFFFF)local Q,R=pcall(syscall.readlink,G)if Q then printInline(" -> ")local S=pcall(syscall.stat,G)syscall.devctl(1,"sfgc",S and 0x00FFFF or 0xFF0000)printInline(R)syscall.devctl(1,"sfgc",0xFFFFFF)end elseif I then syscall.devctl(1,"sfgc",0x6D00FF)printInline(f)syscall.devctl(1,"sfgc",0xFFFFFF)elseif isSock then syscall.devctl(1,"sfgc",0xFF00FF)printInline(f)syscall.devctl(1,"sfgc",0xFFFFFF)else local T=H and H.perms and math.floor(H.perms/2^9)%2==1;syscall.devctl(1,"sfgc",T and 0x00FF00 or 0xFFFFFF)printInline(f)syscall.devctl(1,"sfgc",0xFFFFFF)end;print("")end;return end;local U=0;for e,f in ipairs(F)do if#f+2>U then U=#f+2 end end;local V=math.max(1,math.floor(E/U))for h,f in ipairs(F)do local G=j..f;local I=i.isDir(G)local H=syscall.lstat and syscall.lstat(G)or syscall.stat(G)local J=H and H.etype==0x01;if J then syscall.devctl(1,"sfgc",0x00FFFF)elseif I then syscall.devctl(1,"sfgc",0x6D00FF)elseif isSock then syscall.devctl(1,"sfgc",0xFF00FF)else local T=H and H.perms and math.floor(H.perms/2^9)%2==1;syscall.devctl(1,"sfgc",T and 0x00FF00 or 0xFFFFFF)end;printInline(f)syscall.devctl(1,"sfgc",0xFFFFFF)printInline((" "):rep(U-#f))if h%V==0 then print("")end end;if#F%V~=0 then print("")end
|