2011-11-08 18:35:38 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static bool unary(const char *, const char *);
|
|
|
|
static bool binary(const char *, const char *, const char *);
|
2014-04-22 13:44:16 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
const char *ket = (*argv0 == '[') ? " ]" : "";
|
|
|
|
|
|
|
|
eprintf("usage: %s string%s\n"
|
|
|
|
" %s [!] [-bcdefgkhLnprSstuwxz] string%s\n", argv0, ket, argv0, ket);
|
|
|
|
}
|
2011-11-08 18:35:38 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2012-04-07 03:37:36 +00:00
|
|
|
bool ret = false, not = false;
|
2011-11-08 18:35:38 +00:00
|
|
|
|
2012-05-14 20:28:41 +00:00
|
|
|
argv0 = argv[0];
|
2011-11-08 18:35:38 +00:00
|
|
|
|
|
|
|
/* [ ... ] alias */
|
|
|
|
if(!strcmp(argv[0], "[")) {
|
|
|
|
if(strcmp(argv[argc-1], "]") != 0)
|
2012-05-15 12:32:56 +00:00
|
|
|
usage();
|
2011-11-08 18:35:38 +00:00
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
if(argc > 1 && !strcmp(argv[1], "!")) {
|
|
|
|
not = true;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
switch(argc) {
|
|
|
|
case 2:
|
|
|
|
ret = *argv[1] != '\0';
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ret = unary(argv[1], argv[2]);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
ret = binary(argv[1], argv[2], argv[3]);
|
|
|
|
break;
|
|
|
|
default:
|
2013-11-11 11:43:18 +00:00
|
|
|
break;
|
2011-11-08 18:35:38 +00:00
|
|
|
}
|
|
|
|
if(not)
|
|
|
|
ret = !ret;
|
2014-10-02 22:46:04 +00:00
|
|
|
return ret ? 0 : 1;
|
2011-11-08 18:35:38 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static bool
|
2011-11-08 18:35:38 +00:00
|
|
|
unary(const char *op, const char *arg)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if(op[0] != '-' || op[1] == '\0' || op[2] != '\0')
|
2012-05-15 12:32:56 +00:00
|
|
|
usage();
|
2011-11-08 18:35:38 +00:00
|
|
|
switch(op[1]) {
|
2012-05-14 12:33:07 +00:00
|
|
|
case 'b': case 'c': case 'd': case 'f': case 'g':
|
2013-11-11 11:37:07 +00:00
|
|
|
case 'k': case 'p': case 'S': case 's': case 'u':
|
2011-11-08 18:35:38 +00:00
|
|
|
if((r = stat(arg, &st)) == -1)
|
|
|
|
return false; /* -e */
|
|
|
|
switch(op[1]) {
|
|
|
|
case 'b':
|
|
|
|
return S_ISBLK(st.st_mode);
|
|
|
|
case 'c':
|
|
|
|
return S_ISCHR(st.st_mode);
|
|
|
|
case 'd':
|
|
|
|
return S_ISDIR(st.st_mode);
|
|
|
|
case 'f':
|
|
|
|
return S_ISREG(st.st_mode);
|
|
|
|
case 'g':
|
|
|
|
return st.st_mode & S_ISGID;
|
2013-11-11 11:37:07 +00:00
|
|
|
case 'k':
|
|
|
|
return st.st_mode & S_ISVTX;
|
2011-11-08 18:35:38 +00:00
|
|
|
case 'p':
|
|
|
|
return S_ISFIFO(st.st_mode);
|
|
|
|
case 'S':
|
|
|
|
return S_ISSOCK(st.st_mode);
|
|
|
|
case 's':
|
|
|
|
return st.st_size > 0;
|
|
|
|
case 'u':
|
|
|
|
return st.st_mode & S_ISUID;
|
|
|
|
}
|
2012-05-14 12:33:07 +00:00
|
|
|
case 'e':
|
|
|
|
return access(arg, F_OK) == 0;
|
2011-11-08 18:35:38 +00:00
|
|
|
case 'r':
|
|
|
|
return access(arg, R_OK) == 0;
|
|
|
|
case 'w':
|
|
|
|
return access(arg, W_OK) == 0;
|
|
|
|
case 'x':
|
|
|
|
return access(arg, X_OK) == 0;
|
2012-05-14 12:33:07 +00:00
|
|
|
case 'h': case 'L':
|
|
|
|
return lstat(arg, &st) == 0 && S_ISLNK(st.st_mode);
|
|
|
|
case 't':
|
|
|
|
return isatty((int)estrtol(arg, 0));
|
|
|
|
case 'n':
|
|
|
|
return arg[0] != '\0';
|
2011-11-08 18:35:38 +00:00
|
|
|
case 'z':
|
|
|
|
return arg[0] == '\0';
|
|
|
|
default:
|
2012-05-15 12:32:56 +00:00
|
|
|
usage();
|
2011-11-08 18:35:38 +00:00
|
|
|
}
|
|
|
|
return false; /* should not reach */
|
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static bool
|
2011-11-08 18:35:38 +00:00
|
|
|
binary(const char *arg1, const char *op, const char *arg2)
|
|
|
|
{
|
2013-10-15 13:30:14 +00:00
|
|
|
int i;
|
|
|
|
long narg1, narg2;
|
|
|
|
enum operator { EQ, GE, GT, LE, LT, NE, STREQ, STRNE } oper;
|
2013-11-11 11:27:18 +00:00
|
|
|
char *optexts[] = {
|
|
|
|
"-eq", "-ge", "-gt",
|
|
|
|
"-le", "-lt", "-ne",
|
2013-10-15 13:30:14 +00:00
|
|
|
"=", "!="
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(optexts); i++) {
|
2013-11-11 11:26:29 +00:00
|
|
|
if (strcmp(op, optexts[i]) != 0)
|
|
|
|
continue;
|
|
|
|
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);
|
2013-10-15 13:30:14 +00:00
|
|
|
switch (oper) {
|
2013-11-11 11:26:29 +00:00
|
|
|
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;
|
2013-10-15 13:30:14 +00:00
|
|
|
default:
|
2013-11-11 11:26:29 +00:00
|
|
|
usage();
|
2013-10-15 13:30:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-08 18:35:38 +00:00
|
|
|
return false;
|
|
|
|
}
|