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.
14 static const char rcsid[] =
15 "$FreeBSD: src/bin/test/test.c,v 1.29.2.7 2002/09/10 09:10:57 maxim Exp $";
18 #include <sys/types.h>
33 #include "bltin/bltin.h"
35 static void error(const char *, ...) __attribute__((__noreturn__));
39 error(const char *msg, ...)
50 msg = va_arg(ap, const char *);
60 /* test(1) accepts the following grammar:
61 oexpr ::= aexpr | aexpr "-o" oexpr ;
62 aexpr ::= nexpr | nexpr "-a" aexpr ;
63 nexpr ::= primary | "!" primary
64 primary ::= unary-operator operand
65 | operand binary-operator operand
69 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
70 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
72 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
74 operand ::= <any legal UNIX file name>
130 short op_num, op_type;
135 {"-e", FILEXIST,UNOP},
136 {"-f", FILREG, UNOP},
137 {"-d", FILDIR, UNOP},
138 {"-c", FILCDEV,UNOP},
139 {"-b", FILBDEV,UNOP},
140 {"-p", FILFIFO,UNOP},
141 {"-u", FILSUID,UNOP},
142 {"-g", FILSGID,UNOP},
143 {"-k", FILSTCK,UNOP},
148 {"-h", FILSYM, UNOP}, /* for backwards compat */
149 {"-O", FILUID, UNOP},
150 {"-G", FILGID, UNOP},
151 {"-L", FILSYM, UNOP},
152 {"-S", FILSOCK,UNOP},
154 {"!=", STRNE, BINOP},
157 {"-eq", INTEQ, BINOP},
158 {"-ne", INTNE, BINOP},
159 {"-ge", INTGE, BINOP},
160 {"-gt", INTGT, BINOP},
161 {"-le", INTLE, BINOP},
162 {"-lt", INTLT, BINOP},
163 {"-nt", FILNT, BINOP},
164 {"-ot", FILOT, BINOP},
165 {"-ef", FILEQ, BINOP},
167 {"-a", BAND, BBINOP},
169 {"(", LPAREN, PAREN},
170 {")", RPAREN, PAREN},
174 struct t_op const *t_wp_op;
178 static int aexpr __P((enum token));
179 static int binop __P((void));
180 static int equalf __P((const char *, const char *));
181 static int filstat __P((char *, enum token));
182 static int getn __P((const char *));
183 static quad_t getq __P((const char *));
184 static int intcmp __P((const char *, const char *));
185 static int isoperand __P((void));
186 static int newerf __P((const char *, const char *));
187 static int nexpr __P((enum token));
188 static int oexpr __P((enum token));
189 static int olderf __P((const char *, const char *));
190 static int primary __P((enum token));
191 static void syntax __P((const char *, const char *));
192 static enum token t_lex __P((char *));
204 if ((p = rindex(argv[0], '/')) == NULL)
208 if (strcmp(p, "[") == 0) {
209 if (strcmp(argv[--argc], "]") != 0)
214 /* no expression => false */
218 /* XXX work around the absence of an eaccess(2) syscall */
223 (void)setregid(egid, gid);
224 (void)setreuid(euid, uid);
228 res = !oexpr(t_lex(*t_wp));
231 syntax(*t_wp, "unexpected operator");
232 (void)setregid(gid, egid);
233 (void)setreuid(uid, euid);
245 error("%s: %s", op, msg);
257 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
258 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
272 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
273 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
282 enum token n; /* token */
285 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
297 return 0; /* missing expression */
299 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
301 return 0; /* missing expression */
303 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
304 syntax(NULL, "closing paren expected");
307 if (t_wp_op && t_wp_op->op_type == UNOP) {
308 /* unary expression */
310 syntax(t_wp_op->op_text, "argument expected");
313 return strlen(*++t_wp) == 0;
315 return strlen(*++t_wp) != 0;
317 return isatty(getn(*++t_wp));
319 return filstat(*++t_wp, n);
323 if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type ==
328 return strlen(*t_wp) > 0;
334 const char *opnd1, *opnd2;
335 struct t_op const *op;
338 (void) t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
341 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
342 syntax(op->op_text, "argument expected");
344 switch (op->op_num) {
346 return strcmp(opnd1, opnd2) == 0;
348 return strcmp(opnd1, opnd2) != 0;
350 return strcmp(opnd1, opnd2) < 0;
352 return strcmp(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 intcmp(opnd1, opnd2) < 0;
366 return newerf (opnd1, opnd2);
368 return olderf (opnd1, opnd2);
370 return equalf (opnd1, opnd2);
384 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
389 return access(nm, R_OK) == 0;
391 return access(nm, W_OK) == 0;
393 /* XXX work around access(2) false positives for superuser */
394 if (access(nm, X_OK) != 0)
396 if (S_ISDIR(s.st_mode) || getuid() != 0)
398 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
400 return access(nm, F_OK) == 0;
402 return S_ISREG(s.st_mode);
404 return S_ISDIR(s.st_mode);
406 return S_ISCHR(s.st_mode);
408 return S_ISBLK(s.st_mode);
410 return S_ISFIFO(s.st_mode);
412 return S_ISSOCK(s.st_mode);
414 return S_ISLNK(s.st_mode);
416 return (s.st_mode & S_ISUID) != 0;
418 return (s.st_mode & S_ISGID) != 0;
420 return (s.st_mode & S_ISVTX) != 0;
422 return s.st_size > (off_t)0;
424 return s.st_uid == geteuid();
426 return s.st_gid == getegid();
436 struct t_op const *op = ops;
442 while (op->op_text) {
443 if (strcmp(s, op->op_text) == 0) {
444 if ((op->op_type == UNOP && isoperand()) ||
445 (op->op_num == LPAREN && nargc == 1))
459 struct t_op const *op = ops;
469 while (op->op_text) {
470 if (strcmp(s, op->op_text) == 0)
471 return op->op_type == BINOP &&
472 (t[0] != ')' || t[1] != '\0');
478 /* atoi with error detection */
487 r = strtol(s, &p, 10);
490 error((errno == EINVAL) ? "%s: bad number" :
491 "%s: out of range", s);
493 while (isspace((unsigned char)*p))
497 error("%s: bad number", s);
502 /* atoi with error detection and 64 bit range */
511 r = strtoq(s, &p, 10);
514 error((errno == EINVAL) ? "%s: bad number" :
515 "%s: out of range", s);
517 while (isspace((unsigned char)*p))
521 error("%s: bad number", s);
551 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
554 if (b1.st_mtimespec.tv_sec > b2.st_mtimespec.tv_sec)
556 if (b1.st_mtimespec.tv_sec < b2.st_mtimespec.tv_sec)
559 return (b1.st_mtimespec.tv_nsec > b2.st_mtimespec.tv_nsec);
566 return (newerf(f2, f1));
575 return (stat (f1, &b1) == 0 &&
576 stat (f2, &b2) == 0 &&
577 b1.st_dev == b2.st_dev &&
578 b1.st_ino == b2.st_ino);