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: head/bin/test/test.c 251208 2013-05-31 22:54:20Z jilles $
15 * Important: This file is used both as a standalone program /bin/test and
16 * as a builtin for /bin/sh (#define SHELL).
19 #include <sys/types.h>
35 #include "bltin/bltin.h"
40 static void error(const char *, ...) __dead2 __printf0like(1, 2);
43 error(const char *msg, ...)
54 /* test(1) accepts the following grammar:
55 oexpr ::= aexpr | aexpr "-o" oexpr ;
56 aexpr ::= nexpr | nexpr "-a" aexpr ;
57 nexpr ::= primary | "!" primary
58 primary ::= unary-operator operand
59 | operand binary-operator operand
63 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
64 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
66 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
68 operand ::= <any legal UNIX file name>
124 short op_num, op_type;
129 {"-e", FILEXIST,UNOP},
130 {"-f", FILREG, UNOP},
131 {"-d", FILDIR, UNOP},
132 {"-c", FILCDEV,UNOP},
133 {"-b", FILBDEV,UNOP},
134 {"-p", FILFIFO,UNOP},
135 {"-u", FILSUID,UNOP},
136 {"-g", FILSGID,UNOP},
137 {"-k", FILSTCK,UNOP},
142 {"-h", FILSYM, UNOP}, /* for backwards compat */
143 {"-O", FILUID, UNOP},
144 {"-G", FILGID, UNOP},
145 {"-L", FILSYM, UNOP},
146 {"-S", FILSOCK,UNOP},
148 {"==", STREQ, BINOP},
149 {"!=", STRNE, BINOP},
152 {"-eq", INTEQ, BINOP},
153 {"-ne", INTNE, BINOP},
154 {"-ge", INTGE, BINOP},
155 {"-gt", INTGT, BINOP},
156 {"-le", INTLE, BINOP},
157 {"-lt", INTLT, BINOP},
158 {"-nt", FILNT, BINOP},
159 {"-ot", FILOT, BINOP},
160 {"-ef", FILEQ, BINOP},
162 {"-a", BAND, BBINOP},
164 {"(", LPAREN, PAREN},
165 {")", RPAREN, PAREN},
169 static struct t_op const *t_wp_op;
172 static int parenlevel;
174 static int aexpr(enum token);
175 static int binop(void);
176 static int equalf(const char *, const char *);
177 static int filstat(char *, enum token);
178 static int getn(const char *);
179 static intmax_t getq(const char *);
180 static int intcmp(const char *, const char *);
181 static int isunopoperand(void);
182 static int islparenoperand(void);
183 static int isrparenoperand(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 *);
193 main(int argc, char **argv)
198 if ((p = strrchr(argv[0], '/')) == NULL)
202 if (strcmp(p, "[") == 0) {
203 if (strcmp(argv[--argc], "]") != 0)
208 /* no expression => false */
213 setlocale(LC_CTYPE, "");
218 if (nargc == 4 && strcmp(*t_wp, "!") == 0) {
219 /* Things like ! "" -o x do not fit in the normal grammar. */
222 res = oexpr(t_lex(*t_wp));
224 res = !oexpr(t_lex(*t_wp));
227 syntax(*t_wp, "unexpected operator");
233 syntax(const char *op, const char *msg)
237 error("%s: %s", op, msg);
248 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
249 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
262 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
263 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
274 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
279 primary(enum token n)
285 return 0; /* missing expression */
288 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
291 return 0; /* missing expression */
294 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
295 syntax(NULL, "closing paren expected");
299 if (t_wp_op && t_wp_op->op_type == UNOP) {
300 /* unary expression */
302 syntax(t_wp_op->op_text, "argument expected");
305 return strlen(*++t_wp) == 0;
307 return strlen(*++t_wp) != 0;
309 return isatty(getn(*++t_wp));
311 return filstat(*++t_wp, n);
315 if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type ==
320 return strlen(*t_wp) > 0;
326 const char *opnd1, *opnd2;
327 struct t_op const *op;
330 t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
333 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
334 syntax(op->op_text, "argument expected");
336 switch (op->op_num) {
338 return strcmp(opnd1, opnd2) == 0;
340 return strcmp(opnd1, opnd2) != 0;
342 return strcmp(opnd1, opnd2) < 0;
344 return strcmp(opnd1, opnd2) > 0;
346 return intcmp(opnd1, opnd2) == 0;
348 return intcmp(opnd1, opnd2) != 0;
350 return intcmp(opnd1, opnd2) >= 0;
352 return intcmp(opnd1, opnd2) > 0;
354 return intcmp(opnd1, opnd2) <= 0;
356 return intcmp(opnd1, opnd2) < 0;
358 return newerf (opnd1, opnd2);
360 return olderf (opnd1, opnd2);
362 return equalf (opnd1, opnd2);
370 filstat(char *nm, enum token mode)
374 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
379 return (eaccess(nm, R_OK) == 0);
381 return (eaccess(nm, W_OK) == 0);
383 /* XXX work around eaccess(2) false positives for superuser */
384 if (eaccess(nm, X_OK) != 0)
386 if (S_ISDIR(s.st_mode) || geteuid() != 0)
388 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
390 return (eaccess(nm, F_OK) == 0);
392 return S_ISREG(s.st_mode);
394 return S_ISDIR(s.st_mode);
396 return S_ISCHR(s.st_mode);
398 return S_ISBLK(s.st_mode);
400 return S_ISFIFO(s.st_mode);
402 return S_ISSOCK(s.st_mode);
404 return S_ISLNK(s.st_mode);
406 return (s.st_mode & S_ISUID) != 0;
408 return (s.st_mode & S_ISGID) != 0;
410 return (s.st_mode & S_ISVTX) != 0;
412 return s.st_size > (off_t)0;
414 return s.st_uid == geteuid();
416 return s.st_gid == getegid();
425 struct t_op const *op = ops;
431 while (*op->op_text) {
432 if (strcmp(s, op->op_text) == 0) {
433 if (((op->op_type == UNOP || op->op_type == BUNOP)
434 && isunopoperand()) ||
435 (op->op_num == LPAREN && islparenoperand()) ||
436 (op->op_num == RPAREN && isrparenoperand()))
450 struct t_op const *op = ops;
458 return parenlevel == 1 && strcmp(s, ")") == 0;
460 while (*op->op_text) {
461 if (strcmp(s, op->op_text) == 0)
462 return op->op_type == BINOP &&
463 (parenlevel == 0 || t[0] != ')' || t[1] != '\0');
470 islparenoperand(void)
472 struct t_op const *op = ops;
479 return parenlevel == 1 && strcmp(s, ")") == 0;
482 while (*op->op_text) {
483 if (strcmp(s, op->op_text) == 0)
484 return op->op_type == BINOP;
491 isrparenoperand(void)
499 return parenlevel == 1 && strcmp(s, ")") == 0;
503 /* atoi with error detection */
511 r = strtol(s, &p, 10);
514 error("%s: bad number", s);
517 error((errno == EINVAL) ? "%s: bad number" :
518 "%s: out of range", s);
520 while (isspace((unsigned char)*p))
524 error("%s: bad number", s);
529 /* atoi with error detection and 64 bit range */
537 r = strtoimax(s, &p, 10);
540 error("%s: bad number", s);
543 error((errno == EINVAL) ? "%s: bad number" :
544 "%s: out of range", s);
546 while (isspace((unsigned char)*p))
550 error("%s: bad number", s);
556 intcmp (const char *s1, const char *s2)
574 newerf (const char *f1, const char *f2)
578 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
581 if (b1.st_mtim.tv_sec > b2.st_mtim.tv_sec)
583 if (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec)
586 return (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec);
590 olderf (const char *f1, const char *f2)
592 return (newerf(f2, f1));
596 equalf (const char *f1, const char *f2)
600 return (stat (f1, &b1) == 0 &&
601 stat (f2, &b2) == 0 &&
602 b1.st_dev == b2.st_dev &&
603 b1.st_ino == b2.st_ino);