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>
123 short op_num, op_type;
128 {"-e", FILEXIST,UNOP},
129 {"-f", FILREG, UNOP},
130 {"-d", FILDIR, UNOP},
131 {"-c", FILCDEV,UNOP},
132 {"-b", FILBDEV,UNOP},
133 {"-p", FILFIFO,UNOP},
134 {"-u", FILSUID,UNOP},
135 {"-g", FILSGID,UNOP},
136 {"-k", FILSTCK,UNOP},
141 {"-h", FILSYM, UNOP}, /* for backwards compat */
142 {"-O", FILUID, UNOP},
143 {"-G", FILGID, UNOP},
144 {"-L", FILSYM, UNOP},
145 {"-S", FILSOCK,UNOP},
147 {"==", STREQ, BINOP},
148 {"!=", STRNE, BINOP},
151 {"-eq", INTEQ, BINOP},
152 {"-ne", INTNE, BINOP},
153 {"-ge", INTGE, BINOP},
154 {"-gt", INTGT, BINOP},
155 {"-le", INTLE, BINOP},
156 {"-lt", INTLT, BINOP},
157 {"-nt", FILNT, BINOP},
158 {"-ot", FILOT, BINOP},
159 {"-ef", FILEQ, BINOP},
161 {"-a", BAND, BBINOP},
163 {"(", LPAREN, PAREN},
164 {")", RPAREN, PAREN},
168 struct t_op const *t_wp_op;
173 static int aexpr(enum token);
174 static int binop(void);
175 static int equalf(const char *, const char *);
176 static int filstat(char *, enum token);
177 static int getn(const char *);
178 static intmax_t getq(const char *);
179 static int intcmp(const char *, const char *);
180 static int isunopoperand(void);
181 static int islparenoperand(void);
182 static int isrparenoperand(void);
183 static int newerf(const char *, const char *);
184 static int nexpr(enum token);
185 static int oexpr(enum token);
186 static int olderf(const char *, const char *);
187 static int primary(enum token);
188 static void syntax(const char *, const char *);
189 static enum token t_lex(char *);
192 main(int argc, char **argv)
197 if ((p = strrchr(argv[0], '/')) == NULL)
201 if (strcmp(p, "[") == 0) {
202 if (strcmp(argv[--argc], "]") != 0)
207 /* no expression => false */
212 (void)setlocale(LC_CTYPE, "");
217 if (nargc == 4 && strcmp(*t_wp, "!") == 0) {
218 /* Things like ! "" -o x do not fit in the normal grammar. */
221 res = oexpr(t_lex(*t_wp));
223 res = !oexpr(t_lex(*t_wp));
226 syntax(*t_wp, "unexpected operator");
232 syntax(const char *op, const char *msg)
236 error("%s: %s", op, msg);
247 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
248 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
261 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
262 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
273 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
278 primary(enum token n)
284 return 0; /* missing expression */
287 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
290 return 0; /* missing expression */
293 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
294 syntax(NULL, "closing paren expected");
298 if (t_wp_op && t_wp_op->op_type == UNOP) {
299 /* unary expression */
301 syntax(t_wp_op->op_text, "argument expected");
304 return strlen(*++t_wp) == 0;
306 return strlen(*++t_wp) != 0;
308 return isatty(getn(*++t_wp));
310 return filstat(*++t_wp, n);
314 if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type ==
319 return strlen(*t_wp) > 0;
325 const char *opnd1, *opnd2;
326 struct t_op const *op;
329 (void) t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
332 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
333 syntax(op->op_text, "argument expected");
335 switch (op->op_num) {
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();
424 struct t_op const *op = ops;
430 while (op->op_text) {
431 if (strcmp(s, op->op_text) == 0) {
432 if (((op->op_type == UNOP || op->op_type == BUNOP)
433 && isunopoperand()) ||
434 (op->op_num == LPAREN && islparenoperand()) ||
435 (op->op_num == RPAREN && isrparenoperand()))
449 struct t_op const *op = ops;
457 return parenlevel == 1 && strcmp(s, ")") == 0;
459 while (op->op_text) {
460 if (strcmp(s, op->op_text) == 0)
461 return op->op_type == BINOP &&
462 (parenlevel == 0 || t[0] != ')' || t[1] != '\0');
469 islparenoperand(void)
471 struct t_op const *op = ops;
478 return parenlevel == 1 && strcmp(s, ")") == 0;
481 while (op->op_text) {
482 if (strcmp(s, op->op_text) == 0)
483 return op->op_type == BINOP;
490 isrparenoperand(void)
498 return parenlevel == 1 && strcmp(s, ")") == 0;
502 /* atoi with error detection */
510 r = strtol(s, &p, 10);
513 error("%s: bad number", s);
516 error((errno == EINVAL) ? "%s: bad number" :
517 "%s: out of range", s);
519 while (isspace((unsigned char)*p))
523 error("%s: bad number", s);
528 /* atoi with error detection and 64 bit range */
536 r = strtoimax(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);
555 intcmp (const char *s1, const char *s2)
573 newerf (const char *f1, const char *f2)
577 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
580 if (b1.st_mtim.tv_sec > b2.st_mtim.tv_sec)
582 if (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec)
585 return (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec);
589 olderf (const char *f1, const char *f2)
591 return (newerf(f2, f1));
595 equalf (const char *f1, const char *f2)
599 return (stat (f1, &b1) == 0 &&
600 stat (f2, &b2) == 0 &&
601 b1.st_dev == b2.st_dev &&
602 b1.st_ino == b2.st_ino);