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"
34 static void error(const char *, ...) __dead2 __printflike(1, 2);
37 error(const char *msg, ...)
48 /* test(1) accepts the following grammar:
49 oexpr ::= aexpr | aexpr "-o" oexpr ;
50 aexpr ::= nexpr | nexpr "-a" aexpr ;
51 nexpr ::= primary | "!" primary
52 primary ::= unary-operator operand
53 | operand binary-operator operand
57 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
58 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
60 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
62 operand ::= <any legal UNIX file name>
118 short op_num, op_type;
123 {"-e", FILEXIST,UNOP},
124 {"-f", FILREG, UNOP},
125 {"-d", FILDIR, UNOP},
126 {"-c", FILCDEV,UNOP},
127 {"-b", FILBDEV,UNOP},
128 {"-p", FILFIFO,UNOP},
129 {"-u", FILSUID,UNOP},
130 {"-g", FILSGID,UNOP},
131 {"-k", FILSTCK,UNOP},
136 {"-h", FILSYM, UNOP}, /* for backwards compat */
137 {"-O", FILUID, UNOP},
138 {"-G", FILGID, UNOP},
139 {"-L", FILSYM, UNOP},
140 {"-S", FILSOCK,UNOP},
142 {"!=", STRNE, BINOP},
145 {"-eq", INTEQ, BINOP},
146 {"-ne", INTNE, BINOP},
147 {"-ge", INTGE, BINOP},
148 {"-gt", INTGT, BINOP},
149 {"-le", INTLE, BINOP},
150 {"-lt", INTLT, BINOP},
151 {"-nt", FILNT, BINOP},
152 {"-ot", FILOT, BINOP},
153 {"-ef", FILEQ, BINOP},
155 {"-a", BAND, BBINOP},
157 {"(", LPAREN, PAREN},
158 {")", RPAREN, PAREN},
162 struct t_op const *t_wp_op;
166 static int aexpr (enum token);
167 static int binop (void);
168 static int equalf (const char *, const char *);
169 static int filstat (char *, enum token);
170 static int getn (const char *);
171 static long long getll (const char *);
172 static int intcmp (const char *, const char *);
173 static int isoperand (void);
174 static int newerf (const char *, const char *);
175 static int nexpr (enum token);
176 static int oexpr (enum token);
177 static int olderf (const char *, const char *);
178 static int primary (enum token);
179 static void syntax (const char *, const char *);
180 static enum token t_lex (char *);
183 main(int argc, char **argv)
190 if ((p = strrchr(argv[0], '/')) == NULL)
194 if (strcmp(p, "[") == 0) {
195 if (strcmp(argv[--argc], "]") != 0)
200 /* no expression => false */
204 /* XXX work around the absence of an eaccess(2) syscall */
214 res = !oexpr(t_lex(*t_wp));
217 syntax(*t_wp, "unexpected operator");
225 syntax(const char *op, const char *msg)
229 error("%s: %s", op, msg);
240 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
241 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
254 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
255 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
266 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
271 primary(enum token n)
277 return 0; /* missing expression */
279 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
281 return 0; /* missing expression */
283 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
284 syntax(NULL, "closing paren expected");
287 if (t_wp_op && t_wp_op->op_type == UNOP) {
288 /* unary expression */
290 syntax(t_wp_op->op_text, "argument expected");
293 return strlen(*++t_wp) == 0;
295 return strlen(*++t_wp) != 0;
297 return isatty(getn(*++t_wp));
299 return filstat(*++t_wp, n);
303 if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type ==
308 return strlen(*t_wp) > 0;
314 const char *opnd1, *opnd2;
315 struct t_op const *op;
318 t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
321 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
322 syntax(op->op_text, "argument expected");
324 switch (op->op_num) {
326 return strcmp(opnd1, opnd2) == 0;
328 return strcmp(opnd1, opnd2) != 0;
330 return strcmp(opnd1, opnd2) < 0;
332 return strcmp(opnd1, opnd2) > 0;
334 return intcmp(opnd1, opnd2) == 0;
336 return intcmp(opnd1, opnd2) != 0;
338 return intcmp(opnd1, opnd2) >= 0;
340 return intcmp(opnd1, opnd2) > 0;
342 return intcmp(opnd1, opnd2) <= 0;
344 return intcmp(opnd1, opnd2) < 0;
346 return newerf (opnd1, opnd2);
348 return olderf (opnd1, opnd2);
350 return equalf (opnd1, opnd2);
358 filstat(char *nm, enum token mode)
362 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
367 return access(nm, R_OK) == 0;
369 return access(nm, W_OK) == 0;
371 /* XXX work around access(2) false positives for superuser */
372 if (access(nm, X_OK) != 0)
374 if (S_ISDIR(s.st_mode) || getuid() != 0)
376 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
378 return access(nm, F_OK) == 0;
380 return S_ISREG(s.st_mode);
382 return S_ISDIR(s.st_mode);
384 return S_ISCHR(s.st_mode);
386 return S_ISBLK(s.st_mode);
388 return S_ISFIFO(s.st_mode);
390 return S_ISSOCK(s.st_mode);
392 return S_ISLNK(s.st_mode);
394 return (s.st_mode & S_ISUID) != 0;
396 return (s.st_mode & S_ISGID) != 0;
398 return (s.st_mode & S_ISVTX) != 0;
400 return s.st_size > (off_t)0;
402 return s.st_uid == geteuid();
404 return s.st_gid == getegid();
413 struct t_op const *op = ops;
419 while (op->op_text) {
420 if (strcmp(s, op->op_text) == 0) {
421 if ((op->op_type == UNOP && isoperand()) ||
422 (op->op_num == LPAREN && nargc == 1))
436 struct t_op const *op = ops;
446 while (op->op_text) {
447 if (strcmp(s, op->op_text) == 0)
448 return op->op_type == BINOP &&
449 (t[0] != ')' || t[1] != '\0');
455 /* atoi with error detection */
463 r = strtol(s, &p, 10);
466 error((errno == EINVAL) ? "%s: bad number" :
467 "%s: out of range", s);
469 while (isspace((unsigned char)*p))
473 error("%s: bad number", s);
478 /* atoi with error detection and 64 bit range */
486 r = strtoll(s, &p, 10);
489 error((errno == EINVAL) ? "%s: bad number" :
490 "%s: out of range", s);
492 while (isspace((unsigned char)*p))
496 error("%s: bad number", s);
502 intcmp (const char *s1, const char *s2)
520 newerf (const char *f1, const char *f2)
524 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
527 if (b1.st_mtimespec.tv_sec > b2.st_mtimespec.tv_sec)
529 if (b1.st_mtimespec.tv_sec < b2.st_mtimespec.tv_sec)
532 return (b1.st_mtimespec.tv_nsec > b2.st_mtimespec.tv_nsec);
536 olderf (const char *f1, const char *f2)
538 return (newerf(f2, f1));
542 equalf (const char *f1, const char *f2)
546 return (stat (f1, &b1) == 0 &&
547 stat (f2, &b2) == 0 &&
548 b1.st_dev == b2.st_dev &&
549 b1.st_ino == b2.st_ino);