From 6dfd54fcdf17ce732efa14e9c629f804115436fd Mon Sep 17 00:00:00 2001 From: bpc2003 Date: Sat, 24 May 2025 21:17:18 -0400 Subject: Get started on test command --- cmds/cmds.go | 9 +++++++- cmds/test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 cmds/test.go (limited to 'cmds') diff --git a/cmds/cmds.go b/cmds/cmds.go index 0cfee5e..f5b0081 100644 --- a/cmds/cmds.go +++ b/cmds/cmds.go @@ -1,7 +1,11 @@ // cmds: handles system commands package cmds -import "strings" +import ( + "strings" + + "gosh/global" +) // Eval: evaluates a provided string into a command // if it can't find the command or the arguments are incorrect @@ -31,6 +35,9 @@ func Eval(cmd string) error { printEnv() } break + case "test", "[": + global.ReturnCode = test(args) + break case "exit": if err := exit(args); err != nil { return err diff --git a/cmds/test.go b/cmds/test.go new file mode 100644 index 0000000..77534a3 --- /dev/null +++ b/cmds/test.go @@ -0,0 +1,73 @@ +package cmds + +import ( + "os" + "os/user" + "strconv" + "syscall" +) + +func test(args []string) int { + if len(args) <= 2 { + return 1 + } + + fi, err := os.Lstat(args[2]) + c, _ := user.Current() + if err != nil { + return 1 + } + + switch args[1] { + case "-e": + return 0 + case "-f": + if !fi.IsDir() { + return 0 + } + break + case "-d": + if fi.IsDir() { + return 0 + } + break + case "-r": + if fi.Mode().Perm() & 0400 != 0 { + return 0 + } + break + case "-w": + if fi.Mode().Perm() & 0200 != 0 { + return 0 + } + break + case "-x": + if fi.Mode().Perm() & 0100 != 0 { + return 0 + } + break + case "-S": + if fi.Size() > 0 { + return 0 + } + break + case "-L": + if fi.Mode() & os.ModeSymlink != 0 { + return 0 + } + break + case "-O": + s := fi.Sys().(*syscall.Stat_t) + if c.Uid == strconv.Itoa(int(s.Uid)) { + return 0 + } + break + case "-G": + s := fi.Sys().(*syscall.Stat_t) + if c.Gid == strconv.Itoa(int(s.Gid)) { + return 0 + } + break + } + return 1 +} -- cgit v1.2.3