hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/gils
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-10-21 06:45:53 +0200
committerhachem <im@hachem.wtf>2026-10-21 06:45:53 +0200
commit6f8e692be1c196113b91ad1986a00c9d6f6f0e48 (patch)
tree642bf354c1eacf3e67cdb3d41437586dfe44e4c0 /gils
parent306e9cb38e3922093708dfbbaf12979edff2b788 (diff)
feat: user stuff like perms etc..HEADmain
Diffstat (limited to 'gils')
-rw-r--r--gils/chmod.rv23
-rw-r--r--gils/chown.rv17
-rw-r--r--gils/ls.rv131
-rw-r--r--gils/su.rv14
-rw-r--r--gils/whoami.rv1
5 files changed, 183 insertions, 3 deletions
diff --git a/gils/chmod.rv b/gils/chmod.rv
new file mode 100644
index 0000000..f798659
--- /dev/null
+++ b/gils/chmod.rv
@@ -0,0 +1,23 @@
+# chmod <octal> <path
+# ill do the other things later
+
+fn parse_octal(text) -> num do
+ let value = 0
+ for index in 0..text:len() do
+ const code = text[index]:ascii()
+ if (code >= 48 and code <= 55)
+ value = value * 8 + (code - 48)
+ end
+ return value
+end
+
+const context = env.ctx
+
+if context.argv:len() < 3 do
+ fail("usage: chmod <octal> <path>\n")
+end else do
+ const mode = parse_octal(context.argv[1])
+ const path = expand_path(context.argv[2])
+ if (fs:chmod(path, mode) == :false)
+ fail("chmod: #{path}: no such file\n")
+end
diff --git a/gils/chown.rv b/gils/chown.rv
new file mode 100644
index 0000000..b64101d
--- /dev/null
+++ b/gils/chown.rv
@@ -0,0 +1,17 @@
+# chown <user> <path>
+# chown lung hachem
+
+const context = env.ctx
+
+if context.argv:len() < 3 do
+ fail("usage: chown <user> <path>\n")
+end else do
+ const uid = user_uid(context.argv[1])
+ if uid < 0 do
+ fail("chown: #{context.argv[1]}: no such user\n")
+ end else do
+ const path = expand_path(context.argv[2])
+ if (fs:chown(path, uid) == :false)
+ fail("chown: #{path}: no such file\n")
+ end
+end
diff --git a/gils/ls.rv b/gils/ls.rv
index 4afbb91..b81fc95 100644
--- a/gils/ls.rv
+++ b/gils/ls.rv
@@ -1,6 +1,131 @@
-# list a directory
+# ls
+# it has some features but not all of them obv
+
+fn mode_string(type, mode) -> string do
+ let out = "-"
+ if (type == 2) out = "d"
+ if (type == 3) out = "c"
+
+ const chars = "rwxrwxrwx"
+ for bit in 0..9 do
+ if ((mode band (1 shl (8 - bit))) != 0) do
+ out = out ~ chars[bit]
+ end else do
+ out = out ~ "-"
+ end
+ end
+
+ return out
+end
+
+fn human_size(n) -> string do
+ if (n < 1024)
+ return "#{n}"
+
+ if n < 1048576 do
+ const whole = math.floor(n / 1024)
+ const frac = math.floor((n - whole * 1024) * 10 / 1024)
+ return "#{whole}.#{frac}K"
+ end
+
+ const whole = math.floor(n / 1048576)
+ const frac = math.floor((n - whole * 1048576) * 10 / 1048576)
+ return "#{whole}.#{frac}M"
+end
+
+fn pad_left(text, width) -> string do
+ let out = text
+ while (out:len() < width) do out = " " ~ out end
+ return out
+end
+
+fn pad_right(text, width) -> string do
+ let out = text
+ while (out:len() < width) do out = out ~ " " end
+ return out
+end
const context = env.ctx
-const target = if (context.argv:len() > 1) expand_path(context.argv[1]) else wd.path
+let long = :false
+let all = :false
+let human = :false
+let target = :nil
+
+for ai in 1..context.argv:len() do
+ const a = context.argv[ai]
+ if (a:len() > 1 and a[0] == "-") do
+ for ci in 1..a:len() do
+ const c = a[ci]
+ if (c == "l") long = :true
+ if (c == "a") all = :true
+ if (c == "h") human = :true
+ end
+ end else do
+ target = a
+ end
+end
+
+const path = if (target == :nil) wd.path else expand_path(target)
+
+let records = {}
+let failed = :false
+
+if vfs:is_dir(path) do
+ const items = vfs:entries(path)
+ if items == :nil do
+ fail("ls: #{path}: cannot read\n")
+ failed = :true
+ end else do
+ for idx in 0..items:len() do
+ const e = items[idx]
+ if (all or (e.name[0] != "."))
+ records:push(e)
+ end
+ end
+end else do
+ const info = vfs:stat(path)
+ if info == :nil do
+ fail("ls: #{path}: no such file\n")
+ failed = :true
+ end else do
+ const parts = path:split("/")
+ records:push({ name = parts[parts:len() - 1], type = info.type, size = info.size, mode = info.mode, uid = info.uid })
+ end
+end
+
+fn decorate(record) -> string do
+ if (record.type == 2)
+ return record.name ~ "/"
+ return record.name
+end
+
+if failed == :false do
+ if records:len() == 0 do
+ puts(" (empty)\n")
+ end else if long do
+ # precompute
+ let owners = {}
+ let sizes = {}
+ let owner_w = 0
+ let size_w = 0
+
+ for idx in 0..records:len() do
+ const r = records[idx]
+ const owner = user_name(r.uid)
+ const sz = if (human) human_size(r.size) else "#{r.size}"
+ owners:push(owner)
+ sizes:push(sz)
+ if (owner:len() > owner_w) owner_w = owner:len()
+ if (sz:len() > size_w) size_w = sz:len()
+ end
-vfs:ls(target)
+ for idx in 0..records:len() do
+ const r = records[idx]
+ puts("#{mode_string(r.type, r.mode)} #{pad_right(owners[idx], owner_w)} #{pad_left(sizes[idx], size_w)} #{decorate(r)}\n")
+ end
+ end else do
+ for idx in 0..records:len() do
+ puts(" #{decorate(records[idx])}\n")
+ end
+ end
+end
diff --git a/gils/su.rv b/gils/su.rv
new file mode 100644
index 0000000..d7ab2d2
--- /dev/null
+++ b/gils/su.rv
@@ -0,0 +1,14 @@
+# i need to implement the same vulns as sudo
+
+const context = env.ctx
+
+if context.argv:len() < 2 do
+ fail("usage: su <user>\n")
+end else do
+ const uid = user_uid(context.argv[1])
+ if uid < 0 do
+ fail("su: #{context.argv[1]}: no such user\n")
+ end else do
+ session.uid = uid
+ end
+end
diff --git a/gils/whoami.rv b/gils/whoami.rv
new file mode 100644
index 0000000..8b6858d
--- /dev/null
+++ b/gils/whoami.rv
@@ -0,0 +1 @@
+puts("#{user_name(session.uid)}\n")