diff options
-rw-r--r-- | test.c | 23 |
1 files changed, 9 insertions, 14 deletions
@@ -115,7 +115,7 @@ static int test_s(struct test_arg a) static int test_t(struct test_arg a) { - return isatty((int)a.n) ? 0 : 1; + return a.n < INT_MAX && isatty((int)a.n) ? 0 : 1; } static int test_u(struct test_arg a) @@ -211,7 +211,7 @@ static struct unary_op un_ops[] = { /* { "", STRING, test_n }, */ }; -static struct test_arg test_arg(const char *s, enum test_arg_type type) +static struct test_arg test_arg(const char *base, const char *s, enum test_arg_type type) { struct test_arg a = { 0 }; a.s = s; @@ -220,16 +220,11 @@ static struct test_arg test_arg(const char *s, enum test_arg_type type) char *end = NULL; a.n = strtoimax(a.s, &end, 0); if (end && *end != '\0') { - fprintf(stderr, "test: error converting %s to integer\n", a.s); + fprintf(stderr, "%s: invalid integer '%s'\n", base, a.s); exit(2); } } - if (type == FILE_DESCRIPTOR && a.n > INT_MAX) { - fprintf(stderr, "test: invalid file descriptor %jd\n", a.n); - exit(2); - } - if (type == PATHNAME) { static struct stat st; if (stat(a.s, &st) == 0) { @@ -246,13 +241,13 @@ static int test_binary(const char *base, char *argv[], int first) { for (size_t i = 0; i < sizeof(bin_ops) / sizeof(bin_ops[0]); i++) { if (strcmp(argv[first + 1], bin_ops[i].op) == 0) { - struct test_arg a = test_arg(argv[first], bin_ops[i].type); - struct test_arg b = test_arg(argv[first + 2], bin_ops[i].type); + struct test_arg a = test_arg(base, argv[first], bin_ops[i].type); + struct test_arg b = test_arg(base, argv[first + 2], bin_ops[i].type); return bin_ops[i].fn(a, b); } } - fprintf(stderr, "%s: invalid binary operation '%s'\n", base, argv[first + 1]); + fprintf(stderr, "%s: unknown binary operation '%s'\n", base, argv[first + 1]); exit(2); } @@ -260,12 +255,12 @@ static int test_unary(const char *base, char *argv[], int first) { for (size_t i = 0; i < sizeof(un_ops) / sizeof(un_ops[0]); i++) { if (strcmp(argv[first], un_ops[i].op) == 0) { - struct test_arg a = test_arg(argv[first + 1], un_ops[i].type); + struct test_arg a = test_arg(base, argv[first + 1], un_ops[i].type); return un_ops[i].fn(a); } } - fprintf(stderr, "%s: invalid unary operations '%s'\n", base, argv[first]); + fprintf(stderr, "%s: unknown unary operations '%s'\n", base, argv[first]); exit(2); } @@ -300,7 +295,7 @@ int main(int argc, char *argv[]) } else if (argc - first == 2) { ret = test_unary(base, argv, first); } else if (argc - first == 1) { - struct test_arg a = test_arg(argv[first], STRING); + struct test_arg a = test_arg(base, argv[first], STRING); ret = test_n(a); } else { ret = 1; |