diff options
-rw-r--r-- | cmds/cmds.go | 9 | ||||
-rw-r--r-- | cmds/test.go | 73 | ||||
-rw-r--r-- | global/global.go | 2 |
3 files changed, 83 insertions, 1 deletions
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 +} diff --git a/global/global.go b/global/global.go index 610fa38..f84a340 100644 --- a/global/global.go +++ b/global/global.go @@ -1,7 +1,9 @@ package global var Options []string +var ReturnCode int func init() { Options = make([]string, 0) + ReturnCode = 0 } |