2 lines
2.8 KiB
Plaintext
2 lines
2.8 KiB
Plaintext
local a=syscall.getTask(syscall.getpid()).name;local b={R=false,help=false}local c={}for d,e in ipairs({...})do if e:sub(1,2)=="--"then local f=e:sub(3)if b[f]==nil then print(a..": unrecognized option '"..e.."'")print("try '"..a.." --help' for more information.")syscall.exit(1)return end;b[f]=true elseif e:sub(1,1)=="-"then for g=2,#e do local f=e:sub(g,g)if b[f]==nil then print(a..": invalid option '-"..f.."'")print("try '"..a.." --help' for more information.")syscall.exit(1)return end;b[f]=true end else table.insert(c,e)end end;if b.help then print("Usage: "..a.." [OPTION]... OWNER[:GROUP] FILE...")print(" "..a.." [OPTION]... :GROUP FILE...")print("Change the owner and/or group of each FILE.")print("OWNER and GROUP may be names or numeric IDs.")print("")print("Options:")print(" -R operate on files and directories recursively")print(" --help display this help and exit")return end;if#c<2 then print(a..": missing operand")print("try '"..a.." --help' for more information.")syscall.exit(1)return end;local h=c[1]local i,j;if h:sub(1,1)==":"then j=h:sub(2)else local k=h:find(":",1,true)if k then i=h:sub(1,k-1)j=h:sub(k+1)if j==""then j=nil end else i=h end end;local function l(m)if not m or m==""then return nil end;local n=tonumber(m)if n then return n end;local o=syscall.getuidbyname and syscall.getuidbyname(m)if o then return o end;print(a..": invalid user: '"..m.."'")syscall.exit(1)end;local function p(m)if not m or m==""then return nil end;local n=tonumber(m)if n then return n end;local o=syscall.getuidbyname and syscall.getuidbyname(m)if o then local q=syscall.getpasswd(o)if q then return q.gid end end;print(a..": invalid group: '"..m.."'")syscall.exit(1)end;local r=l(i)local s=p(j)if r==nil and s==nil then print(a..": no owner or group specified")syscall.exit(1)return end;local function t(u)local v=syscall.stat(u)if not v then print(a..": cannot stat '"..u.."': no such file or directory")return false end;local o=r~=nil and r or v.owner;local w=s~=nil and s or v.group;local x,y=pcall(syscall.chown,u,o,w)if not x then local z=tostring(y)if z:find("EPERM")or z:find("EACCES")then z="operation not permitted (must be root)"elseif z:find("ENOENT")then z="no such file or directory"end;print(a..": cannot change owner of '"..u.."': "..z)return false end;return true end;local function A(u)if not t(u)then return end;if syscall.type(u)=="directory"then local x,B=pcall(syscall.listdir,u)if x then for d,C in ipairs(B)do local D=u;if D:sub(-1)~="/"then D=D.."/"end;A(D..C)end end end end;local E=syscall.getcwd()local function F(G)if G:sub(1,1)~="/"then G=E.."/"..G end;return G end;local H=0;for g=2,#c do local u=F(c[g])if not syscall.exists(u)then print(a..": cannot access '"..c[g].."': No such file or directory")H=1 elseif b.R then A(u)else if not t(u)then H=1 end end end;syscall.exit(H)
|