1 /* $NetBSD: test.c,v 1.21 1999/04/05 09:48:38 kleink Exp $ */
4 * test(1); version 7-like -- author Erik Baalbergen
5 * modified by Eric Gisin to be used as built-in.
6 * modified by Arnold Robbins to add SVR3 compatibility
7 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
8 * modified by J.T. Conklin for NetBSD.
10 * This program is in the Public Domain.
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
16 #include <sys/types.h>
32 #include "bltin/bltin.h"
36 static void error(const char *, ...) __dead2 __printf0like(1, 2);
39 error(const char *msg, ...)
49 /* test(1) accepts the following grammar:
50 oexpr ::= aexpr | aexpr "-o" oexpr ;
51 aexpr ::= nexpr | nexpr "-a" aexpr ;
52 nexpr ::= primary | "!" primary
53 primary ::= unary-operator operand
54 | operand binary-operator operand
58 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
59 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
61 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
63 operand ::= <any legal UNIX file name>
119 short op_num, op_type;
124 {"-e", FILEXIST,UNOP},
125 {"-f", FILREG, UNOP},
126 {"-d", FILDIR, UNOP},
127 {"-c", FILCDEV,UNOP},
128 {"-b", FILBDEV,UNOP},
129 {"-p", FILFIFO,UNOP},
130 {"-u", FILSUID,UNOP},
131 {"-g", FILSGID,UNOP},
132 {"-k", FILSTCK,UNOP},
137 {"-h", FILSYM, UNOP}, /* for backwards compat */
138 {"-O", FILUID, UNOP},
139 {"-G", FILGID, UNOP},
140 {"-L", FILSYM, UNOP},
141 {"-S", FILSOCK,UNOP},
143 {"!=", STRNE, BINOP},
146 {"-eq", INTEQ, BINOP},
147 {"-ne", INTNE, BINOP},
148 {"-ge", INTGE, BINOP},
149 {"-gt", INTGT, BINOP},
150 {"-le", INTLE, BINOP},
151 {"-lt", INTLT, BINOP},
152 {"-nt", FILNT, BINOP},
153 {"-ot", FILOT, BINOP},
154 {"-ef", FILEQ, BINOP},
156 {"-a", BAND, BBINOP},
158 {"(", LPAREN, PAREN},
159 {")", RPAREN, PAREN},
163 struct t_op const *t_wp_op;
167 static int aexpr(enum token);
168 static int binop(void);
169 static int equalf(const char *, const char *);
170 static int filstat(char *, enum token);
171 static int getn(const char *);
172 static intmax_t getq(const char *);
173 static int intcmp(const char *, const char *);
174 static int isoperand(void);
175 static int newerf(const char *, const char *);
176 static int nexpr(enum token);
177 static int oexpr(enum token);
178 static int olderf(const char *, const char *);
179 static int primary(enum token);
180 static void syntax(const char *, const char *);
181 static enum token t_lex(char *);
184 main(int argc, char **argv)
189 if ((p = rindex(argv[0], '/')) == NULL)
193 if (strcmp(p, "[") == 0) {
194 if (strcmp(argv[--argc], "]") != 0)
199 /* no expression => false */
204 (void)setlocale(LC_CTYPE, "");
208 res = !oexpr(t_lex(*t_wp));
211 syntax(*t_wp, "unexpected operator");
217 syntax(const char *op, const char *msg)
221 error("%s: %s", op, msg);
232 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
233 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
246 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
247 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
258 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
263 primary(enum token n)
269 return 0; /* missing expression */
271 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
273 return 0; /* missing expression */
275 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
276 syntax(NULL, "closing paren expected");
279 if (t_wp_op && t_wp_op->op_type == UNOP) {
280 /* unary expression */
282 syntax(t_wp_op->op_text, "argument expected");
285 return strlen(*++t_wp) == 0;
287 return strlen(*++t_wp) != 0;
289 return isatty(getn(*++t_wp));
291 return filstat(*++t_wp, n);
295 if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type ==
300 return strlen(*t_wp) > 0;
306 const char *opnd1, *opnd2;
307 struct t_op const *op;
310 (void) t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
313 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
314 syntax(op->op_text, "argument expected");
316 switch (op->op_num) {
318 return strcmp(opnd1, opnd2) == 0;
320 return strcmp(opnd1, opnd2) != 0;
322 return strcmp(opnd1, opnd2) < 0;
324 return strcmp(opnd1, opnd2) > 0;
326 return intcmp(opnd1, opnd2) == 0;
328 return intcmp(opnd1, opnd2) != 0;
330 return intcmp(opnd1, opnd2) >= 0;
332 return intcmp(opnd1, opnd2) > 0;
334 return intcmp(opnd1, opnd2) <= 0;
336 return intcmp(opnd1, opnd2) < 0;
338 return newerf (opnd1, opnd2);
340 return olderf (opnd1, opnd2);
342 return equalf (opnd1, opnd2);
350 filstat(char *nm, enum token mode)
354 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
359 return (eaccess(nm, R_OK) == 0);
361 return (eaccess(nm, W_OK) == 0);
363 /* XXX work around eaccess(2) false positives for superuser */
364 if (eaccess(nm, X_OK) != 0)
366 if (S_ISDIR(s.st_mode) || geteuid() != 0)
368 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
370 return (eaccess(nm, F_OK) == 0);
372 return S_ISREG(s.st_mode);
374 return S_ISDIR(s.st_mode);
376 return S_ISCHR(s.st_mode);
378 return S_ISBLK(s.st_mode);
380 return S_ISFIFO(s.st_mode);
382 return S_ISSOCK(s.st_mode);
384 return S_ISLNK(s.st_mode);
386 return (s.st_mode & S_ISUID) != 0;
388 return (s.st_mode & S_ISGID) != 0;
390 return (s.st_mode & S_ISVTX) != 0;
392 return s.st_size > (off_t)0;
394 return s.st_uid == geteuid();
396 return s.st_gid == getegid();
405 struct t_op const *op = ops;
411 while (op->op_text) {
412 if (strcmp(s, op->op_text) == 0) {
413 if ((op->op_type == UNOP && isoperand()) ||
414 (op->op_num == LPAREN && nargc == 1))
428 struct t_op const *op = ops;
438 while (op->op_text) {
439 if (strcmp(s, op->op_text) == 0)
440 return op->op_type == BINOP &&
441 (t[0] != ')' || t[1] != '\0');
447 /* atoi with error detection */
455 r = strtol(s, &p, 10);
458 error("%s: bad number", s);
461 error((errno == EINVAL) ? "%s: bad number" :
462 "%s: out of range", s);
464 while (isspace((unsigned char)*p))
468 error("%s: bad number", s);
473 /* atoi with error detection and 64 bit range */
481 r = strtoimax(s, &p, 10);
484 error("%s: bad number", s);
487 error((errno == EINVAL) ? "%s: bad number" :
488 "%s: out of range", s);
490 while (isspace((unsigned char)*p))
494 error("%s: bad number", s);
500 intcmp (const char *s1, const char *s2)
518 newerf (const char *f1, const char *f2)
522 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
525 if (b1.st_mtimespec.tv_sec > b2.st_mtimespec.tv_sec)
527 if (b1.st_mtimespec.tv_sec < b2.st_mtimespec.tv_sec)
530 return (b1.st_mtimespec.tv_nsec > b2.st_mtimespec.tv_nsec);
534 olderf (const char *f1, const char *f2)
536 return (newerf(f2, f1));
540 equalf (const char *f1, const char *f2)
544 return (stat (f1, &b1) == 0 &&
545 stat (f2, &b2) == 0 &&
546 b1.st_dev == b2.st_dev &&
547 b1.st_ino == b2.st_ino);