summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbpc2003 <wpesfriendnva@gmail.com>2025-05-26 20:23:03 -0400
committerbpc2003 <wpesfriendnva@gmail.com>2025-05-26 20:23:03 -0400
commit64c5bf8daf8b71b6efc2b4d2be34b179c8c87059 (patch)
tree26888ac0fd0518dcb7828396d9cb39c565ac06c0
parente7aa9e6e78f712d2db095af99813f669b1cefe15 (diff)
Add numeric tests
-rw-r--r--cmds/exit.go4
-rw-r--r--cmds/test.go142
2 files changed, 65 insertions, 81 deletions
diff --git a/cmds/exit.go b/cmds/exit.go
index 5170b26..52f082a 100644
--- a/cmds/exit.go
+++ b/cmds/exit.go
@@ -4,12 +4,14 @@ import (
"errors"
"os"
"strconv"
+
+ "gosh/global"
)
// exit the shell and returns the provided status
// if no status is specified returns 0
func exit(args []string) error {
- status := 0
+ status := global.ReturnCode
if len(args) > 1 {
var err error
status, err = strconv.Atoi(args[1])
diff --git a/cmds/test.go b/cmds/test.go
index 98e182f..59989d3 100644
--- a/cmds/test.go
+++ b/cmds/test.go
@@ -1,7 +1,6 @@
package cmds
import (
- "fmt"
"os"
"os/user"
"strconv"
@@ -17,104 +16,87 @@ func test(args []string) int {
if strings.HasPrefix(args[1], "-") {
return genTest(args[1:])
} else {
- n, err := strconv.Atoi(args[1])
+ _, err := strconv.Atoi(args[1])
if err != nil {
return testString(args[1:])
} else {
- fmt.Println(n)
+ return testNumber(args[1:])
}
}
- return 1
}
func testString(args []string) int {
- ret := 1
- switch args[1] {
- case "=":
- if args[0] == args[2] {
- ret = 0
- }
- break
- case "!=":
- if args[0] != args[2] {
- ret = 0
- }
- break
+ if len(args) != 3 {
+ return 1
+ }
+ sTests := map[string]func() bool{
+ "=": func() bool { return args[0] == args[2] },
+ "!=": func() bool { return args[0] != args[2] },
+ "<": func() bool { return strings.Compare(args[0], args[2]) < 0 },
+ ">": func() bool { return strings.Compare(args[0], args[2]) > 0 },
}
- return ret
+ if sTest, ok := sTests[args[1]]; ok && sTest() {
+ return 0
+ }
+ return 1
+}
+
+func testNumber(args []string) int {
+ if len(args) != 3 {
+ return 1
+ }
+ a, _ := strconv.Atoi(args[0])
+ b, err := strconv.Atoi(args[2])
+ if err != nil {
+ return 1
+ }
+ nTests := map[string]func() bool{
+ "-eq": func() bool { return a == b },
+ "-ne": func() bool { return a != b },
+ "-gt": func() bool { return a > b },
+ "-ge": func() bool { return a >= b },
+ "-lt": func() bool { return a < b },
+ "-le": func() bool { return a <= b },
+ }
+ if nTest, ok := nTests[args[1]]; ok && nTest() {
+ return 0
+ }
+ return 1
}
func genTest(args []string) int {
- ret := 1
- switch args[0] {
- case "-n":
- if len(args[1]) > 0 {
- return 0
- }
- break
- case "-z":
- if len(args[1]) == 0 {
- return 0
- }
- break
+ sTests := map[string]func() bool{
+ "-n": func() bool { return len(args[1]) > 0 },
+ "-z": func() bool { return len(args[1]) == 0 },
+ }
+ if sTest, ok := sTests[args[0]]; ok && sTest() {
+ return 0
}
fi, err := os.Lstat(args[1])
c, _ := user.Current()
if err != nil {
- return ret
+ return 1
}
s := fi.Sys().(*syscall.Stat_t)
+ uid, _ := strconv.Atoi(c.Uid)
+ gid, _ := strconv.Atoi(c.Gid)
- switch args[0] {
- case "-e":
- ret = 0
- break
- case "-f":
- if !fi.IsDir() {
- ret = 0
- }
- break
- case "-d":
- if fi.IsDir() {
- ret = 0
- }
- break
- case "-r":
- if fi.Mode().Perm()&0400 != 0 {
- ret = 0
- }
- break
- case "-w":
- if fi.Mode().Perm()&0200 != 0 {
- ret = 0
- }
- break
- case "-x":
- if fi.Mode().Perm()&0100 != 0 {
- ret = 0
- }
- break
- case "-S":
- if fi.Size() > 0 {
- ret = 0
- }
- break
- case "-L":
- if fi.Mode()&os.ModeSymlink != 0 {
- ret = 0
- }
- break
- case "-O":
- if c.Uid == strconv.Itoa(int(s.Uid)) {
- ret = 0
- }
- break
- case "-G":
- if c.Gid == strconv.Itoa(int(s.Gid)) {
- ret = 0
- }
- break
+ fTests := map[string]func() bool{
+ "-e": func() bool { return true },
+ "-f": func() bool { return !fi.IsDir() },
+ "-d": func() bool { return fi.IsDir() },
+ "-r": func() bool { return fi.Mode().Perm()&0400 != 0 },
+ "-w": func() bool { return fi.Mode().Perm()&0200 != 0 },
+ "-x": func() bool { return fi.Mode().Perm()&0100 != 0 },
+ "-S": func() bool { return fi.Size() > 0 },
+ "-L": func() bool { return fi.Mode()&os.ModeSymlink != 0 },
+ "-O": func() bool { return uid == int(s.Uid) },
+ "-G": func() bool { return gid == int(s.Gid) },
+ }
+
+ if fTest, ok := fTests[args[0]]; ok && fTest() {
+ return 0
}
- return ret
+ return 1
}