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 * Important: This file is used both as a standalone program /bin/test and
14 * as a builtin for /bin/sh (#define SHELL).
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/types.h>
36 #include "bltin/bltin.h"
40 static void error(const char *, ...) __dead2 __printf0like(1, 2);
43 error(const char *msg, ...)
53 /* test(1) accepts the following grammar:
54 oexpr ::= aexpr | aexpr "-o" oexpr ;
55 aexpr ::= nexpr | nexpr "-a" aexpr ;
56 nexpr ::= primary | "!" primary
57 primary ::= unary-operator operand
58 | operand binary-operator operand
62 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
63 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
65 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
67 operand ::= <any legal UNIX file name>
121 #define TOKEN_TYPE(token) ((token) & 0xff00)
143 {"-h", FILSYM}, /* for backwards compat */
172 static int parenlevel;
174 static int aexpr(enum token);
175 static int binop(enum token);
176 static int equalf(const char *, const char *);
177 static int filstat(char *, enum token);
178 static int getn(const char *);
179 static intmax_t getq(const char *);
180 static int intcmp(const char *, const char *);
181 static int isunopoperand(void);
182 static int islparenoperand(void);
183 static int isrparenoperand(void);
184 static int newerf(const char *, const char *);
185 static int nexpr(enum token);
186 static int oexpr(enum token);
187 static int olderf(const char *, const char *);
188 static int primary(enum token);
189 static void syntax(const char *, const char *);
190 static enum token t_lex(char *);
193 main(int argc, char **argv)
198 if ((p = strrchr(argv[0], '/')) == NULL)
202 if (strcmp(p, "[") == 0) {
203 if (strcmp(argv[--argc], "]") != 0)
208 /* no expression => false */
213 (void)setlocale(LC_CTYPE, "");
218 if (nargc == 4 && strcmp(*t_wp, "!") == 0) {
219 /* Things like ! "" -o x do not fit in the normal grammar. */
222 res = oexpr(t_lex(*t_wp));
224 res = !oexpr(t_lex(*t_wp));
227 syntax(*t_wp, "unexpected operator");
233 syntax(const char *op, const char *msg)
237 error("%s: %s", op, msg);
248 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
249 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
262 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
263 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
274 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
279 primary(enum token n)
285 return 0; /* missing expression */
288 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
291 return 0; /* missing expression */
294 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
295 syntax(NULL, "closing paren expected");
299 if (TOKEN_TYPE(n) == UNOP) {
300 /* unary expression */
302 syntax(NULL, "argument expected"); /* impossible */
305 return strlen(*++t_wp) == 0;
307 return strlen(*++t_wp) != 0;
309 return isatty(getn(*++t_wp));
311 return filstat(*++t_wp, n);
315 nn = t_lex(nargc > 0 ? t_wp[1] : NULL);
316 if (TOKEN_TYPE(nn) == BINOP)
319 return strlen(*t_wp) > 0;
325 const char *opnd1, *op, *opnd2;
328 op = nargc > 0 ? (--nargc, *++t_wp) : NULL;
330 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
331 syntax(op, "argument expected");
335 return strcmp(opnd1, opnd2) == 0;
337 return strcmp(opnd1, opnd2) != 0;
339 return strcmp(opnd1, opnd2) < 0;
341 return strcmp(opnd1, opnd2) > 0;
343 return intcmp(opnd1, opnd2) == 0;
345 return intcmp(opnd1, opnd2) != 0;
347 return intcmp(opnd1, opnd2) >= 0;
349 return intcmp(opnd1, opnd2) > 0;
351 return intcmp(opnd1, opnd2) <= 0;
353 return intcmp(opnd1, opnd2) < 0;
355 return newerf (opnd1, opnd2);
357 return olderf (opnd1, opnd2);
359 return equalf (opnd1, opnd2);
367 filstat(char *nm, enum token mode)
371 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
376 return (eaccess(nm, R_OK) == 0);
378 return (eaccess(nm, W_OK) == 0);
380 /* XXX work around eaccess(2) false positives for superuser */
381 if (eaccess(nm, X_OK) != 0)
383 if (S_ISDIR(s.st_mode) || geteuid() != 0)
385 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
387 return (eaccess(nm, F_OK) == 0);
389 return S_ISREG(s.st_mode);
391 return S_ISDIR(s.st_mode);
393 return S_ISCHR(s.st_mode);
395 return S_ISBLK(s.st_mode);
397 return S_ISFIFO(s.st_mode);
399 return S_ISSOCK(s.st_mode);
401 return S_ISLNK(s.st_mode);
403 return (s.st_mode & S_ISUID) != 0;
405 return (s.st_mode & S_ISGID) != 0;
407 return (s.st_mode & S_ISVTX) != 0;
409 return s.st_size > (off_t)0;
411 return s.st_uid == geteuid();
413 return s.st_gid == getegid();
422 struct t_op const *op = ops;
427 while (*op->op_text) {
428 if (strcmp(s, op->op_text) == 0) {
429 if (((TOKEN_TYPE(op->op_num) == UNOP ||
430 TOKEN_TYPE(op->op_num) == BUNOP)
431 && isunopoperand()) ||
432 (op->op_num == LPAREN && islparenoperand()) ||
433 (op->op_num == RPAREN && isrparenoperand()))
445 struct t_op const *op = ops;
453 return parenlevel == 1 && strcmp(s, ")") == 0;
455 while (*op->op_text) {
456 if (strcmp(s, op->op_text) == 0)
457 return TOKEN_TYPE(op->op_num) == BINOP &&
458 (parenlevel == 0 || t[0] != ')' || t[1] != '\0');
465 islparenoperand(void)
467 struct t_op const *op = ops;
474 return parenlevel == 1 && strcmp(s, ")") == 0;
477 while (*op->op_text) {
478 if (strcmp(s, op->op_text) == 0)
479 return TOKEN_TYPE(op->op_num) == BINOP;
486 isrparenoperand(void)
494 return parenlevel == 1 && strcmp(s, ")") == 0;
498 /* atoi with error detection */
506 r = strtol(s, &p, 10);
509 error("%s: bad number", s);
512 error((errno == EINVAL) ? "%s: bad number" :
513 "%s: out of range", s);
515 while (isspace((unsigned char)*p))
519 error("%s: bad number", s);
524 /* atoi with error detection and 64 bit range */
532 r = strtoimax(s, &p, 10);
535 error("%s: bad number", s);
538 error((errno == EINVAL) ? "%s: bad number" :
539 "%s: out of range", s);
541 while (isspace((unsigned char)*p))
545 error("%s: bad number", s);
551 intcmp (const char *s1, const char *s2)
569 newerf (const char *f1, const char *f2)
573 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
576 if (b1.st_mtim.tv_sec > b2.st_mtim.tv_sec)
578 if (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec)
581 return (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec);
585 olderf (const char *f1, const char *f2)
587 return (newerf(f2, f1));
591 equalf (const char *f1, const char *f2)
595 return (stat (f1, &b1) == 0 &&
596 stat (f2, &b2) == 0 &&
597 b1.st_dev == b2.st_dev &&
598 b1.st_ino == b2.st_ino);