[hackers] [sbase] Implement the basic binary operations for test(1) || sin

From: <git_AT_suckless.org>
Date: Wed, 16 Oct 2013 14:45:15 +0200

commit 8617b293552dbfe6842d6f8ec5027d0fe067d807
Author: sin <sin_AT_2f30.org>
Date: Tue Oct 15 14:30:14 2013 +0100

    Implement the basic binary operations for test(1)

diff --git a/test.c b/test.c
index 0c5b969..91f7ecd 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
");
+ int i;
+ long narg1, narg2;
+ enum operator { EQ, GE, GT, LE, LT, NE, STREQ, STRNE } 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 STRNE:
+ 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;
 }
 
Received on Wed Oct 16 2013 - 14:45:15 CEST

This archive was generated by hypermail 2.3.0 : Wed Oct 16 2013 - 14:48:15 CEST