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: src/bin/test/test.c,v 1.29.2.7 2002/09/10 09:10:57 maxim Exp $
13 * $DragonFly: src/bin/test/test.c,v 1.7 2004/11/07 19:42:16 eirikn Exp $
16 #include <sys/types.h>
31 #include "bltin/bltin.h"
33 static void error(const char *, ...) __attribute__((__noreturn__));
36 error(const char *msg, ...)
47 /* test(1) accepts the following grammar:
48 oexpr ::= aexpr | aexpr "-o" oexpr ;
49 aexpr ::= nexpr | nexpr "-a" aexpr ;
50 nexpr ::= primary | "!" primary
51 primary ::= unary-operator operand
52 | operand binary-operator operand
56 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
57 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
59 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
61 operand ::= <any legal UNIX file name>
117 short op_num, op_type;
122 {"-e", FILEXIST,UNOP},
123 {"-f", FILREG, UNOP},
124 {"-d", FILDIR, UNOP},
125 {"-c", FILCDEV,UNOP},
126 {"-b", FILBDEV,UNOP},
127 {"-p", FILFIFO,UNOP},
128 {"-u", FILSUID,UNOP},
129 {"-g", FILSGID,UNOP},
130 {"-k", FILSTCK,UNOP},
135 {"-h", FILSYM, UNOP}, /* for backwards compat */
136 {"-O", FILUID, UNOP},
137 {"-G", FILGID, UNOP},
138 {"-L", FILSYM, UNOP},
139 {"-S", FILSOCK,UNOP},
141 {"!=", STRNE, BINOP},
144 {"-eq", INTEQ, BINOP},
145 {"-ne", INTNE, BINOP},
146 {"-ge", INTGE, BINOP},
147 {"-gt", INTGT, BINOP},
148 {"-le", INTLE, BINOP},
149 {"-lt", INTLT, BINOP},
150 {"-nt", FILNT, BINOP},
151 {"-ot", FILOT, BINOP},
152 {"-ef", FILEQ, BINOP},
154 {"-a", BAND, BBINOP},
156 {"(", LPAREN, PAREN},
157 {")", RPAREN, PAREN},
161 struct t_op const *t_wp_op;
165 static int aexpr (enum token);
166 static int binop (void);
167 static int equalf (const char *, const char *);
168 static int filstat (char *, enum token);
169 static int getn (const char *);
170 static long long getll (const char *);
171 static int intcmp (const char *, const char *);
172 static int isoperand (void);
173 static int newerf (const char *, const char *);
174 static int nexpr (enum token);
175 static int oexpr (enum token);
176 static int olderf (const char *, const char *);
177 static int primary (enum token);
178 static void syntax (const char *, const char *);
179 static enum token t_lex (char *);
182 main(int argc, char **argv)
189 if ((p = strrchr(argv[0], '/')) == NULL)
193 if (strcmp(p, "[") == 0) {
194 if (strcmp(argv[--argc], "]") != 0)
199 /* no expression => false */
203 /* XXX work around the absence of an eaccess(2) syscall */
213 res = !oexpr(t_lex(*t_wp));
216 syntax(*t_wp, "unexpected operator");
224 syntax(const char *op, const char *msg)
228 error("%s: %s", op, msg);
239 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
240 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
253 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
254 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
265 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
270 primary(enum token n)
276 return 0; /* missing expression */
278 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
280 return 0; /* missing expression */
282 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
283 syntax(NULL, "closing paren expected");
286 if (t_wp_op && t_wp_op->op_type == UNOP) {
287 /* unary expression */
289 syntax(t_wp_op->op_text, "argument expected");
292 return strlen(*++t_wp) == 0;
294 return strlen(*++t_wp) != 0;
296 return isatty(getn(*++t_wp));
298 return filstat(*++t_wp, n);
302 if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type ==
307 return strlen(*t_wp) > 0;
313 const char *opnd1, *opnd2;
314 struct t_op const *op;
317 t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
320 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
321 syntax(op->op_text, "argument expected");
323 switch (op->op_num) {
325 return strcmp(opnd1, opnd2) == 0;
327 return strcmp(opnd1, opnd2) != 0;
329 return strcmp(opnd1, opnd2) < 0;
331 return strcmp(opnd1, opnd2) > 0;
333 return intcmp(opnd1, opnd2) == 0;
335 return intcmp(opnd1, opnd2) != 0;
337 return intcmp(opnd1, opnd2) >= 0;
339 return intcmp(opnd1, opnd2) > 0;
341 return intcmp(opnd1, opnd2) <= 0;
343 return intcmp(opnd1, opnd2) < 0;
345 return newerf (opnd1, opnd2);
347 return olderf (opnd1, opnd2);
349 return equalf (opnd1, opnd2);
357 filstat(char *nm, enum token mode)
361 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
366 return access(nm, R_OK) == 0;
368 return access(nm, W_OK) == 0;
370 /* XXX work around access(2) false positives for superuser */
371 if (access(nm, X_OK) != 0)
373 if (S_ISDIR(s.st_mode) || getuid() != 0)
375 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
377 return access(nm, F_OK) == 0;
379 return S_ISREG(s.st_mode);
381 return S_ISDIR(s.st_mode);
383 return S_ISCHR(s.st_mode);
385 return S_ISBLK(s.st_mode);
387 return S_ISFIFO(s.st_mode);
389 return S_ISSOCK(s.st_mode);
391 return S_ISLNK(s.st_mode);
393 return (s.st_mode & S_ISUID) != 0;
395 return (s.st_mode & S_ISGID) != 0;
397 return (s.st_mode & S_ISVTX) != 0;
399 return s.st_size > (off_t)0;
401 return s.st_uid == geteuid();
403 return s.st_gid == getegid();
412 struct t_op const *op = ops;
418 while (op->op_text) {
419 if (strcmp(s, op->op_text) == 0) {
420 if ((op->op_type == UNOP && isoperand()) ||
421 (op->op_num == LPAREN && nargc == 1))
435 struct t_op const *op = ops;
445 while (op->op_text) {
446 if (strcmp(s, op->op_text) == 0)
447 return op->op_type == BINOP &&
448 (t[0] != ')' || t[1] != '\0');
454 /* atoi with error detection */
462 r = strtol(s, &p, 10);
465 error((errno == EINVAL) ? "%s: bad number" :
466 "%s: out of range", s);
468 while (isspace((unsigned char)*p))
472 error("%s: bad number", s);
477 /* atoi with error detection and 64 bit range */
485 r = strtoll(s, &p, 10);
488 error((errno == EINVAL) ? "%s: bad number" :
489 "%s: out of range", s);
491 while (isspace((unsigned char)*p))
495 error("%s: bad number", s);
501 intcmp (const char *s1, const char *s2)
519 newerf (const char *f1, const char *f2)
523 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
526 if (b1.st_mtimespec.tv_sec > b2.st_mtimespec.tv_sec)
528 if (b1.st_mtimespec.tv_sec < b2.st_mtimespec.tv_sec)
531 return (b1.st_mtimespec.tv_nsec > b2.st_mtimespec.tv_nsec);
535 olderf (const char *f1, const char *f2)
537 return (newerf(f2, f1));
541 equalf (const char *f1, const char *f2)
545 return (stat (f1, &b1) == 0 &&
546 stat (f2, &b2) == 0 &&
547 b1.st_dev == b2.st_dev &&
548 b1.st_ino == b2.st_ino);