sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 8617b293552dbfe6842d6f8ec5027d0fe067d807
parent d8718d4159ccb85400baa518d7fdcc1f33558dc2
Author: sin <sin@2f30.org>
Date:   Tue, 15 Oct 2013 14:30:14 +0100

Implement the basic binary operations for test(1)

Diffstat:
Mtest.c | 38+++++++++++++++++++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/test.c b/test.c @@ -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, 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; }