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]... MODE FILE...")print("Change the file mode bits of each FILE to MODE.")print("")print("MODE may be octal (e.g. 755) or symbolic (e.g. u+x, go-w, a=r).")print("")print("Octal bit layout (Hyperion):")print(" owner: r=32 w=16 x=512 group: r=8 w=4 x=256")print(" world: r=2 w=1 x=128 suid=64")print(" Common: 644=rw-r--r-- 755=rwxr-xr-x 700=rwx------")print("")print("Symbolic: [ugoa][+-=][rwxs] (comma-separated list)")print("")print("Options:")print(" -R change 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={OWNER_R=32,OWNER_W=16,OWNER_X=512,GROUP_R=8,GROUP_W=4,GROUP_X=256,WORLD_R=2,WORLD_W=1,WORLD_X=128,SUID=64}local function j(k,l)return math.floor(k/2^l)%2==1 end;local function m(n)local o=tonumber(n,8)if not o then return nil end;local p=0;if j(o,8)then p=p+i.OWNER_R end;if j(o,7)then p=p+i.OWNER_W end;if j(o,6)then p=p+i.OWNER_X end;if j(o,5)then p=p+i.GROUP_R end;if j(o,4)then p=p+i.GROUP_W end;if j(o,3)then p=p+i.GROUP_X end;if j(o,2)then p=p+i.WORLD_R end;if j(o,1)then p=p+i.WORLD_W end;if j(o,0)then p=p+i.WORLD_X end;if j(o,11)then p=p+i.SUID end;return p end;local function q(r,s)local t=s;for u in(r..","):gmatch("([^,]+),")do local v,w=u:match("^([ugoa]*)([+%-=].+)$")if not v then print(a..": invalid mode: '"..u.."'")syscall.exit(1)return nil end;if v==""or v=="a"then v="ugo"end;local x=w:sub(1,1)local y=w:sub(2)local z=0;for g=1,#y do local A=y:sub(g,g)for B=1,#v do local C=v:sub(B,B)if A=="r"then if C=="u"then z=z+i.OWNER_R elseif C=="g"then z=z+i.GROUP_R elseif C=="o"then z=z+i.WORLD_R end elseif A=="w"then if C=="u"then z=z+i.OWNER_W elseif C=="g"then z=z+i.GROUP_W elseif C=="o"then z=z+i.WORLD_W end elseif A=="x"then if C=="u"then z=z+i.OWNER_X elseif C=="g"then z=z+i.GROUP_X elseif C=="o"then z=z+i.WORLD_X end elseif A=="s"then if C=="u"then z=z+i.SUID end end end end;if x=="+"then t=t+z-(t%(z+1)-t%z>0 and 0 or 0)t=t-t%1;local function D(E,F)local p,l=0,1;while E>0 or F>0 do if E%2==1 or F%2==1 then p=p+l end;E=math.floor(E/2)F=math.floor(F/2)l=l*2 end;return p end;t=D(t,z)elseif x=="-"then local function G(E,F)local p,l=0,1;while E>0 and F>0 do if E%2==1 and F%2==1 then p=p+l end;E=math.floor(E/2)F=math.floor(F/2)l=l*2 end;return p end;local function H(E,F)local p,l=0,1;while E>0 or F>0 do if E%2==1~=(F%2==1)then p=p+l end;E=math.floor(E/2)F=math.floor(F/2)l=l*2 end;return p end;t=H(t,G(t,z))elseif x=="="then local I=0;for B=1,#v do local C=v:sub(B,B)if C=="u"then I=I+i.OWNER_R+i.OWNER_W+i.OWNER_X+i.SUID elseif C=="g"then I=I+i.GROUP_R+i.GROUP_W+i.GROUP_X elseif C=="o"then I=I+i.WORLD_R+i.WORLD_W+i.WORLD_X end end;local function H(E,F)local p,l=0,1;while E>0 or F>0 do if E%2==1~=(F%2==1)then p=p+l end;E=math.floor(E/2)F=math.floor(F/2)l=l*2 end;return p end;local function G(E,F)local p,l=0,1;while E>0 and F>0 do if E%2==1 and F%2==1 then p=p+l end;E=math.floor(E/2)F=math.floor(F/2)l=l*2 end;return p end;local function D(E,F)local p,l=0,1;while E>0 or F>0 do if E%2==1 or F%2==1 then p=p+l end;E=math.floor(E/2)F=math.floor(F/2)l=l*2 end;return p end;t=H(t,G(t,I))t=D(t,z)else print(a..": invalid operator in mode: '"..u.."'")syscall.exit(1)return nil end end;return t end;local function J(r,s)if r:match("^[0-7]+$")then local K=m(r)if K then return K end end;return q(r,s)end;local function L(M)local N,O=pcall(syscall.stat,M)local s=0;if N then local n=syscall.stat(M)s=n and n.perms or 0 end;local P=J(h,s)if P==nil then return false end;local Q,R=pcall(syscall.chmod,M,P)if not Q then local S=tostring(R)if S:find("EACCES")or S:find("EPERM")then S="permission denied"elseif S:find("ENOENT")then S="no such file or directory"end;print(a..": cannot change permissions of '"..M.."': "..S)return false end;return true end;local function T(M)if not L(M)then return end;if syscall.type(M)=="directory"then local Q,U=pcall(syscall.listdir,M)if Q then for d,V in ipairs(U)do local W=M;if W:sub(-1)~="/"then W=W.."/"end;T(W..V)end end end end;local X=syscall.getcwd()local function Y(K)if K:sub(1,1)~="/"then K=X.."/"..K end;return K end;local Z=0;for g=2,#c do local M=Y(c[g])if not syscall.exists(M)then print(a..": cannot access '"..c[g].."': No such file or directory")Z=1 elseif b.R then T(M)else if not L(M)then Z=1 end end end;syscall.exit(Z)