2011-11-08 18:35:38 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2016-03-26 14:12:53 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2016-03-31 01:03:37 +00:00
|
|
|
#include <fcntl.h>
|
2015-02-14 20:02:41 +00:00
|
|
|
#include <string.h>
|
2011-11-08 18:35:38 +00:00
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-11-08 18:35:38 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2016-03-24 23:18:03 +00:00
|
|
|
static int
|
|
|
|
intcmp(char *a, char *b)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int asign = *a == '-' ? -1 : 1;
|
|
|
|
int bsign = *b == '-' ? -1 : 1;
|
|
|
|
|
|
|
|
if (*a == '-' || *a == '+') a += 1;
|
|
|
|
if (*b == '-' || *b == '+') b += 1;
|
|
|
|
|
|
|
|
if (!*a || !*b)
|
|
|
|
goto noint;
|
|
|
|
for (s = a; *s; s++)
|
|
|
|
if (!isdigit(*s))
|
|
|
|
goto noint;
|
|
|
|
for (s = b; *s; s++)
|
|
|
|
if (!isdigit(*s))
|
|
|
|
goto noint;
|
|
|
|
|
|
|
|
while (*a == '0') a++;
|
|
|
|
while (*b == '0') b++;
|
|
|
|
asign *= !!*a;
|
|
|
|
bsign *= !!*b;
|
|
|
|
|
|
|
|
if (asign != bsign)
|
|
|
|
return asign < bsign ? -1 : 1;
|
|
|
|
else if (strlen(a) != strlen(b))
|
|
|
|
return asign * (strlen(a) < strlen(b) ? -1 : 1);
|
|
|
|
else
|
|
|
|
return asign * strcmp(a, b);
|
|
|
|
|
|
|
|
noint:
|
|
|
|
enprintf(2, "expected integer operands\n");
|
|
|
|
|
|
|
|
return 0; /* not reached */
|
|
|
|
}
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2016-03-24 23:18:02 +00:00
|
|
|
static int
|
|
|
|
mtimecmp(struct stat *buf1, struct stat *buf2)
|
|
|
|
{
|
|
|
|
if (buf1->st_mtime < buf2->st_mtime) return -1;
|
|
|
|
if (buf1->st_mtime > buf2->st_mtime) return +1;
|
|
|
|
#ifdef st_mtime
|
|
|
|
if (buf1->st_mtim.tv_nsec < buf2->st_mtim.tv_nsec) return -1;
|
|
|
|
if (buf1->st_mtim.tv_nsec > buf2->st_mtim.tv_nsec) return +1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int unary_b(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISBLK (buf.st_mode); }
|
|
|
|
static int unary_c(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISCHR (buf.st_mode); }
|
|
|
|
static int unary_d(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISDIR (buf.st_mode); }
|
|
|
|
static int unary_f(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISREG (buf.st_mode); }
|
|
|
|
static int unary_g(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISGID & buf.st_mode ; }
|
|
|
|
static int unary_h(char *s) { struct stat buf; if (lstat(s, &buf)) return 0; return S_ISLNK (buf.st_mode); }
|
2016-03-24 23:18:02 +00:00
|
|
|
static int unary_k(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISVTX & buf.st_mode ; }
|
2014-11-13 20:24:47 +00:00
|
|
|
static int unary_p(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISFIFO (buf.st_mode); }
|
|
|
|
static int unary_S(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISSOCK (buf.st_mode); }
|
|
|
|
static int unary_s(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return buf.st_size ; }
|
|
|
|
static int unary_u(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISUID & buf.st_mode ; }
|
|
|
|
|
2015-02-20 19:19:02 +00:00
|
|
|
static int unary_n(char *s) { return *s; }
|
|
|
|
static int unary_z(char *s) { return !*s; }
|
2014-11-13 20:24:47 +00:00
|
|
|
|
2016-03-31 01:03:37 +00:00
|
|
|
static int unary_e(char *s) { return !faccessat(AT_FDCWD, s, F_OK, AT_EACCESS); }
|
|
|
|
static int unary_r(char *s) { return !faccessat(AT_FDCWD, s, R_OK, AT_EACCESS); }
|
|
|
|
static int unary_w(char *s) { return !faccessat(AT_FDCWD, s, W_OK, AT_EACCESS); }
|
|
|
|
static int unary_x(char *s) { return !faccessat(AT_FDCWD, s, X_OK, AT_EACCESS); }
|
2014-11-13 20:24:47 +00:00
|
|
|
|
2015-02-09 21:21:23 +00:00
|
|
|
static int unary_t(char *s) { int fd = enstrtonum(2, s, 0, INT_MAX); return isatty(fd); }
|
2014-11-13 20:24:47 +00:00
|
|
|
|
2015-03-05 01:28:18 +00:00
|
|
|
static int binary_se(char *s1, char *s2) { return !strcmp(s1, s2); }
|
|
|
|
static int binary_sn(char *s1, char *s2) { return strcmp(s1, s2); }
|
2014-11-13 20:24:47 +00:00
|
|
|
|
2016-03-24 23:18:03 +00:00
|
|
|
static int binary_eq(char *s1, char *s2) { return intcmp(s1, s2) == 0; }
|
|
|
|
static int binary_ne(char *s1, char *s2) { return intcmp(s1, s2) != 0; }
|
|
|
|
static int binary_gt(char *s1, char *s2) { return intcmp(s1, s2) > 0; }
|
|
|
|
static int binary_ge(char *s1, char *s2) { return intcmp(s1, s2) >= 0; }
|
|
|
|
static int binary_lt(char *s1, char *s2) { return intcmp(s1, s2) < 0; }
|
|
|
|
static int binary_le(char *s1, char *s2) { return intcmp(s1, s2) <= 0; }
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2016-03-24 23:18:02 +00:00
|
|
|
static int
|
|
|
|
binary_ef(char *s1, char *s2)
|
|
|
|
{
|
|
|
|
struct stat buf1, buf2;
|
|
|
|
if (stat(s1, &buf1) || stat(s2, &buf2)) return 0;
|
|
|
|
return buf1.st_dev == buf2.st_dev && buf1.st_ino == buf2.st_ino;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
binary_ot(char *s1, char *s2)
|
|
|
|
{
|
|
|
|
struct stat buf1, buf2;
|
|
|
|
if (stat(s1, &buf1) || stat(s2, &buf2)) return 0;
|
|
|
|
return mtimecmp(&buf1, &buf2) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
binary_nt(char *s1, char *s2)
|
|
|
|
{
|
|
|
|
struct stat buf1, buf2;
|
|
|
|
if (stat(s1, &buf1) || stat(s2, &buf2)) return 0;
|
|
|
|
return mtimecmp(&buf1, &buf2) > 0;
|
|
|
|
}
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
struct test {
|
2014-10-17 14:44:29 +00:00
|
|
|
char *name;
|
2017-03-08 07:32:44 +00:00
|
|
|
union {
|
|
|
|
int (*u)(char *);
|
|
|
|
int (*b)(char *, char *);
|
|
|
|
} func;
|
2015-03-17 17:59:16 +00:00
|
|
|
};
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
static struct test unary[] = {
|
2017-03-08 07:32:44 +00:00
|
|
|
{ "-b", { .u = unary_b } },
|
|
|
|
{ "-c", { .u = unary_c } },
|
|
|
|
{ "-d", { .u = unary_d } },
|
|
|
|
{ "-e", { .u = unary_e } },
|
|
|
|
{ "-f", { .u = unary_f } },
|
|
|
|
{ "-g", { .u = unary_g } },
|
|
|
|
{ "-h", { .u = unary_h } },
|
|
|
|
{ "-k", { .u = unary_k } },
|
|
|
|
{ "-L", { .u = unary_h } },
|
|
|
|
{ "-n", { .u = unary_n } },
|
|
|
|
{ "-p", { .u = unary_p } },
|
|
|
|
{ "-r", { .u = unary_r } },
|
|
|
|
{ "-S", { .u = unary_S } },
|
|
|
|
{ "-s", { .u = unary_s } },
|
|
|
|
{ "-t", { .u = unary_t } },
|
|
|
|
{ "-u", { .u = unary_u } },
|
|
|
|
{ "-w", { .u = unary_w } },
|
|
|
|
{ "-x", { .u = unary_x } },
|
|
|
|
{ "-z", { .u = unary_z } },
|
|
|
|
|
|
|
|
{ NULL },
|
2014-10-16 09:10:00 +00:00
|
|
|
};
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
static struct test binary[] = {
|
2017-03-08 07:32:44 +00:00
|
|
|
{ "=" , { .b = binary_se } },
|
|
|
|
{ "!=" , { .b = binary_sn } },
|
|
|
|
{ "-eq", { .b = binary_eq } },
|
|
|
|
{ "-ne", { .b = binary_ne } },
|
|
|
|
{ "-gt", { .b = binary_gt } },
|
|
|
|
{ "-ge", { .b = binary_ge } },
|
|
|
|
{ "-lt", { .b = binary_lt } },
|
|
|
|
{ "-le", { .b = binary_le } },
|
|
|
|
{ "-ef", { .b = binary_ef } },
|
|
|
|
{ "-ot", { .b = binary_ot } },
|
|
|
|
{ "-nt", { .b = binary_nt } },
|
|
|
|
|
|
|
|
{ NULL },
|
2014-10-17 14:44:29 +00:00
|
|
|
};
|
|
|
|
|
2015-03-17 17:59:16 +00:00
|
|
|
static struct test *
|
|
|
|
find_test(struct test *tests, char *name)
|
2014-04-22 13:44:16 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct test *t;
|
2014-04-22 13:44:16 +00:00
|
|
|
|
2015-03-05 01:28:18 +00:00
|
|
|
for (t = tests; t->name; t++)
|
|
|
|
if (!strcmp(t->name, name))
|
2014-10-17 14:44:29 +00:00
|
|
|
return t;
|
2015-03-17 22:35:11 +00:00
|
|
|
|
2014-10-17 14:44:29 +00:00
|
|
|
return NULL;
|
2014-04-22 13:44:16 +00:00
|
|
|
}
|
2011-11-08 18:35:38 +00:00
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int
|
2015-03-05 01:28:18 +00:00
|
|
|
noarg(char *argv[])
|
2014-10-17 14:44:29 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int
|
2015-03-05 01:28:18 +00:00
|
|
|
onearg(char *argv[])
|
2011-11-08 18:35:38 +00:00
|
|
|
{
|
2015-02-22 19:37:48 +00:00
|
|
|
return unary_n(argv[0]);
|
2011-11-08 18:35:38 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int
|
2015-03-05 01:28:18 +00:00
|
|
|
twoarg(char *argv[])
|
2011-11-08 18:35:38 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct test *t;
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2015-03-05 01:28:18 +00:00
|
|
|
if (!strcmp(argv[0], "!"))
|
2014-10-17 14:44:29 +00:00
|
|
|
return !onearg(argv + 1);
|
|
|
|
|
2015-03-05 01:28:18 +00:00
|
|
|
if ((t = find_test(unary, *argv)))
|
2017-03-08 07:32:44 +00:00
|
|
|
return t->func.u(argv[1]);
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2015-02-25 17:01:20 +00:00
|
|
|
enprintf(2, "bad unary test %s\n", argv[0]);
|
2015-03-17 22:35:11 +00:00
|
|
|
|
2015-03-05 01:28:18 +00:00
|
|
|
return 0; /* not reached */
|
2011-11-08 18:35:38 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int
|
2015-03-05 01:28:18 +00:00
|
|
|
threearg(char *argv[])
|
2011-11-08 18:35:38 +00:00
|
|
|
{
|
2015-03-17 17:59:16 +00:00
|
|
|
struct test *t = find_test(binary, argv[1]);
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (t)
|
2017-03-08 07:32:44 +00:00
|
|
|
return t->func.b(argv[0], argv[2]);
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2015-03-05 01:28:18 +00:00
|
|
|
if (!strcmp(argv[0], "!"))
|
2014-10-17 14:44:29 +00:00
|
|
|
return !twoarg(argv + 1);
|
|
|
|
|
2015-02-25 17:01:20 +00:00
|
|
|
enprintf(2, "bad binary test %s\n", argv[1]);
|
2015-03-17 22:35:11 +00:00
|
|
|
|
2015-03-05 01:28:18 +00:00
|
|
|
return 0; /* not reached */
|
2014-10-17 14:44:29 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int
|
2015-03-05 01:28:18 +00:00
|
|
|
fourarg(char *argv[])
|
2014-10-17 14:44:29 +00:00
|
|
|
{
|
2015-03-05 01:28:18 +00:00
|
|
|
if (!strcmp(argv[0], "!"))
|
2014-10-17 14:44:29 +00:00
|
|
|
return !threearg(argv + 1);
|
|
|
|
|
2015-02-25 17:01:20 +00:00
|
|
|
enprintf(2, "too many arguments\n");
|
2015-03-17 22:35:11 +00:00
|
|
|
|
2015-03-05 01:28:18 +00:00
|
|
|
return 0; /* not reached */
|
2014-10-17 14:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-03-05 01:28:18 +00:00
|
|
|
main(int argc, char *argv[])
|
2014-10-17 14:44:29 +00:00
|
|
|
{
|
2015-03-05 01:28:18 +00:00
|
|
|
int (*narg[])(char *[]) = { noarg, onearg, twoarg, threearg, fourarg };
|
|
|
|
size_t len;
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2017-08-05 21:50:39 +00:00
|
|
|
argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2017-08-05 21:50:39 +00:00
|
|
|
len = argv0 ? strlen(argv0) : 0;
|
2015-03-05 01:28:18 +00:00
|
|
|
if (len && argv0[--len] == '[' && (!len || argv0[--len] == '/') && strcmp(argv[--argc], "]"))
|
|
|
|
enprintf(2, "no matching ]\n");
|
2014-10-17 14:44:29 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc > 4)
|
2014-10-17 14:44:29 +00:00
|
|
|
enprintf(2, "too many arguments\n");
|
|
|
|
|
|
|
|
return !narg[argc](argv);
|
2011-11-08 18:35:38 +00:00
|
|
|
}
|