[hackers] [sbase] test: Minor cleanup || Evan Gates

From: <git_AT_suckless.org>
Date: Thu, 5 Mar 2015 09:06:39 +0100 (CET)

commit fdd6f3d4f4ad0cbbc0ee00ac4770e449e3926bbf
Author: Evan Gates <evan.gates_AT_gmail.com>
Date: Wed Mar 4 17:28:18 2015 -0800

    test: Minor cleanup
    
    1) use arg.h
    2) !strcmp
    3) **argv to *argv[]
    4) fix test to check if basename(argv0) == "[" but avoid
    basename(3p) as it may change the contents of the string
    passed to it and I didn't want to make a copy.

diff --git a/test.c b/test.c
index 8395d1d..b9d4834 100644
--- a/test.c
+++ b/test.c
_AT_@ -1,8 +1,7 @@
 /* See LICENSE file for copyright and license details. */
-#include <sys/stat.h>
-
 #include <string.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include "util.h"
 
_AT_@ -29,8 +28,8 @@ static int unary_x(char *s) { return access(s, X_OK); }
 
 static int unary_t(char *s) { int fd = enstrtonum(2, s, 0, INT_MAX); return isatty(fd); }
 
-static int binary_se(char *s1, char *s2) { return strcmp(s1, s2) == 0; }
-static int binary_sn(char *s1, char *s2) { return strcmp(s1, s2) != 0; }
+static int binary_se(char *s1, char *s2) { return !strcmp(s1, s2); }
+static int binary_sn(char *s1, char *s2) { return strcmp(s1, s2); }
 
 static int binary_eq(char *s1, char *s2) { long long a = STOI(s1), b = STOI(s2); return a == b; }
 static int binary_ne(char *s1, char *s2) { long long a = STOI(s1), b = STOI(s2); return a != b; }
_AT_@ -85,74 +84,78 @@ find_test(Test *tests, char *name)
 {
         Test *t;
 
- for (t = tests; t->name; ++t)
- if (strcmp(t->name, name) == 0)
+ for (t = tests; t->name; t++)
+ if (!strcmp(t->name, name))
                         return t;
         return NULL;
 }
 
 static int
-noarg(char **argv)
+noarg(char *argv[])
 {
         return 0;
 }
 
 static int
-onearg(char **argv)
+onearg(char *argv[])
 {
         return unary_n(argv[0]);
 }
 
 static int
-twoarg(char **argv)
+twoarg(char *argv[])
 {
- Test *t = find_test(unary, *argv);
+ Test *t;
 
- if (strcmp(argv[0], "!") == 0)
+ if (!strcmp(argv[0], "!"))
                 return !onearg(argv + 1);
 
- if (t)
+ if ((t = find_test(unary, *argv)))
                 return t->func(argv[1]);
 
         enprintf(2, "bad unary test %s\n", argv[0]);
- return 0; /* NOTREACHED */
+ return 0; /* not reached */
 }
 
 static int
-threearg(char **argv)
+threearg(char *argv[])
 {
         Test *t = find_test(binary, argv[1]);
 
         if (t)
                 return t->func(argv[0], argv[2]);
 
- if (strcmp(argv[0], "!") == 0)
+ if (!strcmp(argv[0], "!"))
                 return !twoarg(argv + 1);
 
         enprintf(2, "bad binary test %s\n", argv[1]);
- return 0; /* NOTREACHED */
+ return 0; /* not reached */
 }
 
 static int
-fourarg(char **argv)
+fourarg(char *argv[])
 {
- if (strcmp(argv[0], "!") == 0)
+ if (!strcmp(argv[0], "!"))
                 return !threearg(argv + 1);
 
         enprintf(2, "too many arguments\n");
- return 0; /* NOTREACHED */
+ return 0; /* not reached */
 }
 
 int
-main(int argc, char **argv)
+main(int argc, char *argv[])
 {
- int (*narg[])(char**) = { noarg, onearg, twoarg, threearg, fourarg };
- size_t len = strlen(argv[0]);
+ int (*narg[])(char *[]) = { noarg, onearg, twoarg, threearg, fourarg };
+ size_t len;
 
- if (len && argv[0][len - 1] == '[' && strcmp(argv[--argc], "]") != 0)
- enprintf(2, "no matching ]\n");
+ ARGBEGIN {
+ default:
+ break;
+ } ARGEND;
 
- --argc; ++argv;
+ len = strlen(argv0);
+ if (len && argv0[--len] == '[' && (!len || argv0[--len] == '/') && strcmp(argv[--argc], "]"))
+ enprintf(2, "no matching ]\n");
 
         if (argc > 4)
                 enprintf(2, "too many arguments\n");
Received on Thu Mar 05 2015 - 09:06:39 CET

This archive was generated by hypermail 2.3.0 : Thu Mar 05 2015 - 09:12:08 CET