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(void);
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 if (TOKEN_TYPE(t_lex(nargc > 0 ? t_wp[1] : NULL)) == BINOP)
318 return strlen(*t_wp) > 0;
324 const char *opnd1, *op, *opnd2;
328 op = nargc > 0 ? t_wp[1] : NULL;
329 n = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
331 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
332 syntax(op, "argument expected");
336 return strcmp(opnd1, opnd2) == 0;
338 return strcmp(opnd1, opnd2) != 0;
340 return strcmp(opnd1, opnd2) < 0;
342 return strcmp(opnd1, opnd2) > 0;
344 return intcmp(opnd1, opnd2) == 0;
346 return intcmp(opnd1, opnd2) != 0;
348 return intcmp(opnd1, opnd2) >= 0;
350 return intcmp(opnd1, opnd2) > 0;
352 return intcmp(opnd1, opnd2) <= 0;
354 return intcmp(opnd1, opnd2) < 0;
356 return newerf (opnd1, opnd2);
358 return olderf (opnd1, opnd2);
360 return equalf (opnd1, opnd2);
368 filstat(char *nm, enum token mode)
372 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
377 return (eaccess(nm, R_OK) == 0);
379 return (eaccess(nm, W_OK) == 0);
381 /* XXX work around eaccess(2) false positives for superuser */
382 if (eaccess(nm, X_OK) != 0)
384 if (S_ISDIR(s.st_mode) || geteuid() != 0)
386 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
388 return (eaccess(nm, F_OK) == 0);
390 return S_ISREG(s.st_mode);
392 return S_ISDIR(s.st_mode);
394 return S_ISCHR(s.st_mode);
396 return S_ISBLK(s.st_mode);
398 return S_ISFIFO(s.st_mode);
400 return S_ISSOCK(s.st_mode);
402 return S_ISLNK(s.st_mode);
404 return (s.st_mode & S_ISUID) != 0;
406 return (s.st_mode & S_ISGID) != 0;
408 return (s.st_mode & S_ISVTX) != 0;
410 return s.st_size > (off_t)0;
412 return s.st_uid == geteuid();
414 return s.st_gid == getegid();
423 struct t_op const *op = ops;
428 while (*op->op_text) {
429 if (strcmp(s, op->op_text) == 0) {
430 if (((TOKEN_TYPE(op->op_num) == UNOP ||
431 TOKEN_TYPE(op->op_num) == BUNOP)
432 && isunopoperand()) ||
433 (op->op_num == LPAREN && islparenoperand()) ||
434 (op->op_num == RPAREN && isrparenoperand()))
446 struct t_op const *op = ops;
454 return parenlevel == 1 && strcmp(s, ")") == 0;
456 while (*op->op_text) {
457 if (strcmp(s, op->op_text) == 0)
458 return TOKEN_TYPE(op->op_num) == BINOP &&
459 (parenlevel == 0 || t[0] != ')' || t[1] != '\0');
466 islparenoperand(void)
468 struct t_op const *op = ops;
475 return parenlevel == 1 && strcmp(s, ")") == 0;
478 while (*op->op_text) {
479 if (strcmp(s, op->op_text) == 0)
480 return TOKEN_TYPE(op->op_num) == BINOP;
487 isrparenoperand(void)
495 return parenlevel == 1 && strcmp(s, ")") == 0;
499 /* atoi with error detection */
507 r = strtol(s, &p, 10);
510 error("%s: bad number", s);
513 error((errno == EINVAL) ? "%s: bad number" :
514 "%s: out of range", s);
516 while (isspace((unsigned char)*p))
520 error("%s: bad number", s);
525 /* atoi with error detection and 64 bit range */
533 r = strtoimax(s, &p, 10);
536 error("%s: bad number", s);
539 error((errno == EINVAL) ? "%s: bad number" :
540 "%s: out of range", s);
542 while (isspace((unsigned char)*p))
546 error("%s: bad number", s);
552 intcmp (const char *s1, const char *s2)
570 newerf (const char *f1, const char *f2)
574 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
577 if (b1.st_mtim.tv_sec > b2.st_mtim.tv_sec)
579 if (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec)
582 return (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec);
586 olderf (const char *f1, const char *f2)
588 return (newerf(f2, f1));
592 equalf (const char *f1, const char *f2)
596 return (stat (f1, &b1) == 0 &&
597 stat (f2, &b2) == 0 &&
598 b1.st_dev == b2.st_dev &&
599 b1.st_ino == b2.st_ino);