test: more efficient -e flag

This commit is contained in:
Connor Lane Smith 2012-05-14 13:33:07 +01:00
parent 1360f568bb
commit f1259218f2

20
test.c
View File

@ -58,8 +58,8 @@ unary(const char *op, const char *arg)
if(op[0] != '-' || op[1] == '\0' || op[2] != '\0') if(op[0] != '-' || op[1] == '\0' || op[2] != '\0')
usage(); usage();
switch(op[1]) { switch(op[1]) {
case 'b': case 'c': case 'd': case 'e': case 'f': case 'b': case 'c': case 'd': case 'f': case 'g':
case 'g': case 'p': case 'S': case 's': case 'u': case 'p': case 'S': case 's': case 'u':
if((r = stat(arg, &st)) == -1) if((r = stat(arg, &st)) == -1)
return false; /* -e */ return false; /* -e */
switch(op[1]) { switch(op[1]) {
@ -69,8 +69,6 @@ unary(const char *op, const char *arg)
return S_ISCHR(st.st_mode); return S_ISCHR(st.st_mode);
case 'd': case 'd':
return S_ISDIR(st.st_mode); return S_ISDIR(st.st_mode);
case 'e':
return true;
case 'f': case 'f':
return S_ISREG(st.st_mode); return S_ISREG(st.st_mode);
case 'g': case 'g':
@ -84,18 +82,20 @@ unary(const char *op, const char *arg)
case 'u': case 'u':
return st.st_mode & S_ISUID; return st.st_mode & S_ISUID;
} }
case 'h': case 'L': case 'e':
return lstat(arg, &st) == 0 && S_ISLNK(st.st_mode); return access(arg, F_OK) == 0;
case 'n':
return arg[0] != '\0';
case 't':
return isatty((int)estrtol(arg, 0));
case 'r': case 'r':
return access(arg, R_OK) == 0; return access(arg, R_OK) == 0;
case 'w': case 'w':
return access(arg, W_OK) == 0; return access(arg, W_OK) == 0;
case 'x': case 'x':
return access(arg, X_OK) == 0; return access(arg, X_OK) == 0;
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';
case 'z': case 'z':
return arg[0] == '\0'; return arg[0] == '\0';
default: default: