-
Notifications
You must be signed in to change notification settings - Fork 0
/
chmod.src
71 lines (60 loc) · 2.58 KB
/
chmod.src
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//command: chmod2.0
usage_info = "
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
or: chmod [OPTION]... OCTAL-MODE FILE...
Change the mode of each FILE to MODE.
-R, --recursive change files and directories recursively
-h, --help display this help and exit
--version output version information and exit
Each MODE is of the form '[ugoa]*([-+=]([rwx]|[0-7]'.
"
version_info = "
chmod (GreyHack coreutils) 2.0
Copyright (C) 2024.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Salmon85 https://github.com/salmon85.
"
if params.len == 1 and params[0] == "--version" then exit(version_info)
if params.len < 2 or (params.len == 3 and params[0] != "-R") or params[0] == "-h" or params[0] == "--help" then exit(usage_info)
// function to convert numerical permissions into ones used by the game
permissions = {1: "+x", 2: "+w", 3: "+wx", 4: "+r", 5: "+rx", 6: "+rw", 7: "+rwx"}
permissions_type = {1: "u", 2: "g", 3: "o"}
numpermissions = function(permission)
return permissions[permission]
end function
// function to apply the numerical permissions
file_chmod = function(file, permissions_number, Recursive)
if permissions_number == 0 then permissions_number = "000"
// check to make sure we aren't passing a bad number
for i in range(1,3)
if to_int(str(permissions_number)[i-1]) < 0 or to_int(str(permissions_number)[i-1]) > 7 then exit("invalid number for permissions, should be 0-7")
end for
for i in range(1,3)
// remove all permissions first
file.chmod(permissions_type[i]+"-rwx", Recursive)
// dont do anything if 0
if to_int(str(permissions_number)[i-1]) == 0 then continue else file.chmod(permissions_type[i]+permissions[to_int(str(permissions_number)[i-1])], Recursive)
end for
end function
file_permissions = params[0]
pathFile = params[1]
isRecursive = 0
if params.len == 3 then
file_permissions = params[1]
pathFile = params[2]
isRecursive = 1
end if
file = get_shell.host_computer.File(pathFile)
if file == null then exit("chmod: can't find " + pathFile)
if typeof(to_int(file_permissions)) == "number" then
output = file_chmod(file, file_permissions, isRecursive)
else if file_permissions.indexOf("a") then
a = file_permissions.split("a")
output = file.chmod("u"+a[1], isRecursive)
output = file.chmod("g"+a[1], isRecursive)
output = file.chmod("o"+a[1], isRecursive)
else
output = file.chmod(file_permissions, isRecursive)
end if