[PATCH] Implement the basic binary operations for test(1)

From: sin <sin_AT_2f30.org>
Date: Tue, 15 Oct 2013 14:30:14 +0100

---
 test.c |   38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
diff --git a/test.c b/test.c
index 0c5b969..a312839 100644
--- a/test.c
+++ b/test.c
_AT_@ -105,7 +105,43 @@ unary(const char *op, const char *arg)
 bool
 binary(const char *arg1, const char *op, const char *arg2)
 {
-	eprintf("not yet implemented\n");
+	int i;
+	long narg1, narg2;
+	enum operator { EQ, GE, GT, LE, LT, NE, STREQ, STRNOTEQ } oper;
+	char *optexts[] = { "-eq", "-ge", "-gt", "-le", "-lt", "-ne",
+		"=", "!="
+	};
+
+	for (i = 0; i < LEN(optexts); i++) {
+		if (strcmp(op, optexts[i]) == 0) {
+			oper = i;
+			switch (oper) {
+			case STREQ:
+				return strcmp(arg1, arg2) == 0;
+			case STRNOTEQ:
+				return strcmp(arg1, arg2) != 0;
+			default:
+				narg1 = estrtol(arg1, 0);
+				narg2 = estrtol(arg2, 0);
+				switch (oper) {
+				case EQ:
+					return narg1 == narg2;
+				case GE:
+					return narg1 >= narg2;
+				case GT:
+					return narg1 > narg2;
+				case LE:
+					return narg1 <= narg2;
+				case LT:
+					return narg1 < narg2;
+				case NE:
+					return narg1 != narg2;
+				default:
+					usage();
+				}
+			}
+		}
+	}
 	return false;
 }
 
-- 
1.7.10.4
--DocE+STaALJfprDB--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Tue Oct 15 2013 - 16:12:04 CEST