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.
12 * $FreeBSD: head/bin/test/test.c 298232 2016-04-19 00:38:07Z araujo $
15 * Important: This file is used both as a standalone program /bin/test and
16 * as a builtin for /bin/sh (#define SHELL).
19 #include <sys/types.h>
35 #include "bltin/bltin.h"
39 static void error(const char *, ...) __dead2 __printf0like(1, 2);
42 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)
123 static const struct t_op {
150 {"h", FILSYM}, /* for backwards compat */
174 static int parenlevel;
176 static int aexpr(enum token);
177 static int binop(enum token);
178 static int equalf(const char *, const char *);
179 static int filstat(char *, enum token);
180 static int getn(const char *);
181 static intmax_t getq(const char *);
182 static int intcmp(const char *, const char *);
183 static int isunopoperand(void);
184 static int islparenoperand(void);
185 static int isrparenoperand(void);
186 static int newerf(const char *, const char *);
187 static int nexpr(enum token);
188 static int oexpr(enum token);
189 static int olderf(const char *, const char *);
190 static int primary(enum token);
191 static void syntax(const char *, const char *);
192 static enum token t_lex(char *);
195 main(int argc, char **argv)
200 if ((p = strrchr(argv[0], '/')) == NULL)
204 if (strcmp(p, "[") == 0) {
205 if (strcmp(argv[--argc], "]") != 0)
210 /* no expression => false */
215 setlocale(LC_CTYPE, "");
220 if (nargc == 4 && strcmp(*t_wp, "!") == 0) {
221 /* Things like ! "" -o x do not fit in the normal grammar. */
224 res = oexpr(t_lex(*t_wp));
226 res = !oexpr(t_lex(*t_wp));
229 syntax(*t_wp, "unexpected operator");
235 syntax(const char *op, const char *msg)
239 error("%s: %s", op, msg);
250 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
251 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
264 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
265 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
276 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
281 primary(enum token n)
287 return 0; /* missing expression */
290 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
293 return 0; /* missing expression */
296 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
297 syntax(NULL, "closing paren expected");
301 if (TOKEN_TYPE(n) == UNOP) {
302 /* unary expression */
304 syntax(NULL, "argument expected"); /* impossible */
307 return strlen(*++t_wp) == 0;
309 return strlen(*++t_wp) != 0;
311 return isatty(getn(*++t_wp));
313 return filstat(*++t_wp, n);
317 nn = t_lex(nargc > 0 ? t_wp[1] : NULL);
318 if (TOKEN_TYPE(nn) == BINOP)
321 return strlen(*t_wp) > 0;
327 const char *opnd1, *op, *opnd2;
330 op = nargc > 0 ? (--nargc, *++t_wp) : NULL;
332 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
333 syntax(op, "argument expected");
337 return strcmp(opnd1, opnd2) == 0;
339 return strcmp(opnd1, opnd2) != 0;
341 return strcmp(opnd1, opnd2) < 0;
343 return strcmp(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 intcmp(opnd1, opnd2) < 0;
357 return newerf (opnd1, opnd2);
359 return olderf (opnd1, opnd2);
361 return equalf (opnd1, opnd2);
369 filstat(char *nm, enum token mode)
373 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
378 return (eaccess(nm, R_OK) == 0);
380 return (eaccess(nm, W_OK) == 0);
382 /* XXX work around eaccess(2) false positives for superuser */
383 if (eaccess(nm, X_OK) != 0)
385 if (S_ISDIR(s.st_mode) || geteuid() != 0)
387 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
389 return (eaccess(nm, F_OK) == 0);
391 return S_ISREG(s.st_mode);
393 return S_ISDIR(s.st_mode);
395 return S_ISCHR(s.st_mode);
397 return S_ISBLK(s.st_mode);
399 return S_ISFIFO(s.st_mode);
401 return S_ISSOCK(s.st_mode);
403 return S_ISLNK(s.st_mode);
405 return (s.st_mode & S_ISUID) != 0;
407 return (s.st_mode & S_ISGID) != 0;
409 return (s.st_mode & S_ISVTX) != 0;
411 return s.st_size > (off_t)0;
413 return s.st_uid == geteuid();
415 return s.st_gid == getegid();
422 find_op_1char(const struct t_op *op, const struct t_op *end, const char *s)
428 if (c == *op->op_text)
436 find_op_2char(const struct t_op *op, const struct t_op *end, const char *s)
439 if (s[0] == op->op_text[0] && s[1] == op->op_text[1])
447 find_op(const char *s)
451 else if (s[1] == '\0')
452 return find_op_1char(ops1, (&ops1)[1], s);
453 else if (s[2] == '\0')
454 return s[0] == '-' ? find_op_1char(opsm1, (&opsm1)[1], s + 1) :
455 find_op_2char(ops2, (&ops2)[1], s);
456 else if (s[3] == '\0')
457 return s[0] == '-' ? find_op_2char(opsm2, (&opsm2)[1], s + 1) :
472 if (((TOKEN_TYPE(num) == UNOP || TOKEN_TYPE(num) == BUNOP)
473 && isunopoperand()) ||
474 (num == LPAREN && islparenoperand()) ||
475 (num == RPAREN && isrparenoperand()))
491 return parenlevel == 1 && strcmp(s, ")") == 0;
494 return TOKEN_TYPE(num) == BINOP &&
495 (parenlevel == 0 || t[0] != ')' || t[1] != '\0');
499 islparenoperand(void)
508 return parenlevel == 1 && strcmp(s, ")") == 0;
512 return TOKEN_TYPE(num) == BINOP;
516 isrparenoperand(void)
524 return parenlevel == 1 && strcmp(s, ")") == 0;
528 /* atoi with error detection */
536 r = strtol(s, &p, 10);
539 error("%s: bad number", s);
542 error((errno == EINVAL) ? "%s: bad number" :
543 "%s: out of range", s);
545 while (isspace((unsigned char)*p))
549 error("%s: bad number", s);
554 /* atoi with error detection and 64 bit range */
562 r = strtoimax(s, &p, 10);
565 error("%s: bad number", s);
568 error((errno == EINVAL) ? "%s: bad number" :
569 "%s: out of range", s);
571 while (isspace((unsigned char)*p))
575 error("%s: bad number", s);
581 intcmp (const char *s1, const char *s2)
599 newerf (const char *f1, const char *f2)
603 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
606 if (b1.st_mtim.tv_sec > b2.st_mtim.tv_sec)
608 if (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec)
611 return (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec);
615 olderf (const char *f1, const char *f2)
617 return (newerf(f2, f1));
621 equalf (const char *f1, const char *f2)
625 return (stat (f1, &b1) == 0 &&
626 stat (f2, &b2) == 0 &&
627 b1.st_dev == b2.st_dev &&
628 b1.st_ino == b2.st_ino);