[hackers] [sbase] test: Minor cleanup || Evan Gates
commit 796b85a2973c9e48c471a550209fa818efc427c8
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 Tue Mar 24 2015 - 23:54:09 CET
This archive was generated by hypermail 2.3.0
: Wed Mar 25 2015 - 00:11:24 CET