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.3 2003/09/21 04:27:16 drhodus Exp $
16 #include <sys/types.h>
31 #include "bltin/bltin.h"
33 static void error(const char *, ...) __attribute__((__noreturn__));
37 error(const char *msg, ...)
48 msg = va_arg(ap, const char *);
58 /* test(1) accepts the following grammar:
59 oexpr ::= aexpr | aexpr "-o" oexpr ;
60 aexpr ::= nexpr | nexpr "-a" aexpr ;
61 nexpr ::= primary | "!" primary
62 primary ::= unary-operator operand
63 | operand binary-operator operand
67 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
68 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
70 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
72 operand ::= <any legal UNIX file name>
128 short op_num, op_type;
133 {"-e", FILEXIST,UNOP},
134 {"-f", FILREG, UNOP},
135 {"-d", FILDIR, UNOP},
136 {"-c", FILCDEV,UNOP},
137 {"-b", FILBDEV,UNOP},
138 {"-p", FILFIFO,UNOP},
139 {"-u", FILSUID,UNOP},
140 {"-g", FILSGID,UNOP},
141 {"-k", FILSTCK,UNOP},
146 {"-h", FILSYM, UNOP}, /* for backwards compat */
147 {"-O", FILUID, UNOP},
148 {"-G", FILGID, UNOP},
149 {"-L", FILSYM, UNOP},
150 {"-S", FILSOCK,UNOP},
152 {"!=", STRNE, BINOP},
155 {"-eq", INTEQ, BINOP},
156 {"-ne", INTNE, BINOP},
157 {"-ge", INTGE, BINOP},
158 {"-gt", INTGT, BINOP},
159 {"-le", INTLE, BINOP},
160 {"-lt", INTLT, BINOP},
161 {"-nt", FILNT, BINOP},
162 {"-ot", FILOT, BINOP},
163 {"-ef", FILEQ, BINOP},
165 {"-a", BAND, BBINOP},
167 {"(", LPAREN, PAREN},
168 {")", RPAREN, PAREN},
172 struct t_op const *t_wp_op;
176 static int aexpr (enum token);
177 static int binop (void);
178 static int equalf (const char *, const char *);
179 static int filstat (char *, enum token);
180 static int getn (const char *);
181 static quad_t getq (const char *);
182 static int intcmp (const char *, const char *);
183 static int isoperand (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 *);
202 if ((p = rindex(argv[0], '/')) == NULL)
206 if (strcmp(p, "[") == 0) {
207 if (strcmp(argv[--argc], "]") != 0)
212 /* no expression => false */
216 /* XXX work around the absence of an eaccess(2) syscall */
221 (void)setregid(egid, gid);
222 (void)setreuid(euid, uid);
226 res = !oexpr(t_lex(*t_wp));
229 syntax(*t_wp, "unexpected operator");
230 (void)setregid(gid, egid);
231 (void)setreuid(uid, euid);
243 error("%s: %s", op, msg);
255 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
256 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
270 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
271 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
280 enum token n; /* token */
283 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
295 return 0; /* missing expression */
297 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
299 return 0; /* missing expression */
301 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
302 syntax(NULL, "closing paren expected");
305 if (t_wp_op && t_wp_op->op_type == UNOP) {
306 /* unary expression */
308 syntax(t_wp_op->op_text, "argument expected");
311 return strlen(*++t_wp) == 0;
313 return strlen(*++t_wp) != 0;
315 return isatty(getn(*++t_wp));
317 return filstat(*++t_wp, n);
321 if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type ==
326 return strlen(*t_wp) > 0;
332 const char *opnd1, *opnd2;
333 struct t_op const *op;
336 (void) t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
339 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
340 syntax(op->op_text, "argument expected");
342 switch (op->op_num) {
344 return strcmp(opnd1, opnd2) == 0;
346 return strcmp(opnd1, opnd2) != 0;
348 return strcmp(opnd1, opnd2) < 0;
350 return strcmp(opnd1, opnd2) > 0;
352 return intcmp(opnd1, opnd2) == 0;
354 return intcmp(opnd1, opnd2) != 0;
356 return intcmp(opnd1, opnd2) >= 0;
358 return intcmp(opnd1, opnd2) > 0;
360 return intcmp(opnd1, opnd2) <= 0;
362 return intcmp(opnd1, opnd2) < 0;
364 return newerf (opnd1, opnd2);
366 return olderf (opnd1, opnd2);
368 return equalf (opnd1, opnd2);
382 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
387 return access(nm, R_OK) == 0;
389 return access(nm, W_OK) == 0;
391 /* XXX work around access(2) false positives for superuser */
392 if (access(nm, X_OK) != 0)
394 if (S_ISDIR(s.st_mode) || getuid() != 0)
396 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
398 return access(nm, F_OK) == 0;
400 return S_ISREG(s.st_mode);
402 return S_ISDIR(s.st_mode);
404 return S_ISCHR(s.st_mode);
406 return S_ISBLK(s.st_mode);
408 return S_ISFIFO(s.st_mode);
410 return S_ISSOCK(s.st_mode);
412 return S_ISLNK(s.st_mode);
414 return (s.st_mode & S_ISUID) != 0;
416 return (s.st_mode & S_ISGID) != 0;
418 return (s.st_mode & S_ISVTX) != 0;
420 return s.st_size > (off_t)0;
422 return s.st_uid == geteuid();
424 return s.st_gid == getegid();
434 struct t_op const *op = ops;
440 while (op->op_text) {
441 if (strcmp(s, op->op_text) == 0) {
442 if ((op->op_type == UNOP && isoperand()) ||
443 (op->op_num == LPAREN && nargc == 1))
457 struct t_op const *op = ops;
467 while (op->op_text) {
468 if (strcmp(s, op->op_text) == 0)
469 return op->op_type == BINOP &&
470 (t[0] != ')' || t[1] != '\0');
476 /* atoi with error detection */
485 r = strtol(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);
500 /* atoi with error detection and 64 bit range */
509 r = strtoq(s, &p, 10);
512 error((errno == EINVAL) ? "%s: bad number" :
513 "%s: out of range", s);
515 while (isspace((unsigned char)*p))
519 error("%s: bad number", s);
549 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
552 if (b1.st_mtimespec.tv_sec > b2.st_mtimespec.tv_sec)
554 if (b1.st_mtimespec.tv_sec < b2.st_mtimespec.tv_sec)
557 return (b1.st_mtimespec.tv_nsec > b2.st_mtimespec.tv_nsec);
564 return (newerf(f2, f1));
573 return (stat (f1, &b1) == 0 &&
574 stat (f2, &b2) == 0 &&
575 b1.st_dev == b2.st_dev &&
576 b1.st_ino == b2.st_ino);