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.
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
16 #include <sys/types.h>
32 #include "bltin/bltin.h"
36 static void error(const char *, ...) __dead2 __printf0like(1, 2);
39 error(const char *msg, ...)
49 /* test(1) accepts the following grammar:
50 oexpr ::= aexpr | aexpr "-o" oexpr ;
51 aexpr ::= nexpr | nexpr "-a" aexpr ;
52 nexpr ::= primary | "!" primary
53 primary ::= unary-operator operand
54 | operand binary-operator operand
58 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
59 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
61 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
63 operand ::= <any legal UNIX file name>
119 short op_num, op_type;
124 {"-e", FILEXIST,UNOP},
125 {"-f", FILREG, UNOP},
126 {"-d", FILDIR, UNOP},
127 {"-c", FILCDEV,UNOP},
128 {"-b", FILBDEV,UNOP},
129 {"-p", FILFIFO,UNOP},
130 {"-u", FILSUID,UNOP},
131 {"-g", FILSGID,UNOP},
132 {"-k", FILSTCK,UNOP},
137 {"-h", FILSYM, UNOP}, /* for backwards compat */
138 {"-O", FILUID, UNOP},
139 {"-G", FILGID, UNOP},
140 {"-L", FILSYM, UNOP},
141 {"-S", FILSOCK,UNOP},
143 {"!=", STRNE, BINOP},
146 {"-eq", INTEQ, BINOP},
147 {"-ne", INTNE, BINOP},
148 {"-ge", INTGE, BINOP},
149 {"-gt", INTGT, BINOP},
150 {"-le", INTLE, BINOP},
151 {"-lt", INTLT, BINOP},
152 {"-nt", FILNT, BINOP},
153 {"-ot", FILOT, BINOP},
154 {"-ef", FILEQ, BINOP},
156 {"-a", BAND, BBINOP},
158 {"(", LPAREN, PAREN},
159 {")", RPAREN, PAREN},
163 struct t_op const *t_wp_op;
168 static int aexpr(enum token);
169 static int binop(void);
170 static int equalf(const char *, const char *);
171 static int filstat(char *, enum token);
172 static int getn(const char *);
173 static intmax_t getq(const char *);
174 static int intcmp(const char *, const char *);
175 static int isunopoperand(void);
176 static int islparenoperand(void);
177 static int isrparenoperand(void);
178 static int newerf(const char *, const char *);
179 static int nexpr(enum token);
180 static int oexpr(enum token);
181 static int olderf(const char *, const char *);
182 static int primary(enum token);
183 static void syntax(const char *, const char *);
184 static enum token t_lex(char *);
187 main(int argc, char **argv)
192 if ((p = rindex(argv[0], '/')) == NULL)
196 if (strcmp(p, "[") == 0) {
197 if (strcmp(argv[--argc], "]") != 0)
202 /* no expression => false */
207 (void)setlocale(LC_CTYPE, "");
212 if (nargc == 4 && strcmp(*t_wp, "!") == 0) {
213 /* Things like ! "" -o x do not fit in the normal grammar. */
216 res = oexpr(t_lex(*t_wp));
218 res = !oexpr(t_lex(*t_wp));
221 syntax(*t_wp, "unexpected operator");
227 syntax(const char *op, const char *msg)
231 error("%s: %s", op, msg);
242 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
243 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
256 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
257 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
268 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
273 primary(enum token n)
279 return 0; /* missing expression */
282 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
285 return 0; /* missing expression */
288 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
289 syntax(NULL, "closing paren expected");
293 if (t_wp_op && t_wp_op->op_type == UNOP) {
294 /* unary expression */
296 syntax(t_wp_op->op_text, "argument expected");
299 return strlen(*++t_wp) == 0;
301 return strlen(*++t_wp) != 0;
303 return isatty(getn(*++t_wp));
305 return filstat(*++t_wp, n);
309 if (t_lex(nargc > 0 ? t_wp[1] : NULL), t_wp_op && t_wp_op->op_type ==
314 return strlen(*t_wp) > 0;
320 const char *opnd1, *opnd2;
321 struct t_op const *op;
324 (void) t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL);
327 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
328 syntax(op->op_text, "argument expected");
330 switch (op->op_num) {
332 return strcmp(opnd1, opnd2) == 0;
334 return strcmp(opnd1, opnd2) != 0;
336 return strcmp(opnd1, opnd2) < 0;
338 return strcmp(opnd1, opnd2) > 0;
340 return intcmp(opnd1, opnd2) == 0;
342 return intcmp(opnd1, opnd2) != 0;
344 return intcmp(opnd1, opnd2) >= 0;
346 return intcmp(opnd1, opnd2) > 0;
348 return intcmp(opnd1, opnd2) <= 0;
350 return intcmp(opnd1, opnd2) < 0;
352 return newerf (opnd1, opnd2);
354 return olderf (opnd1, opnd2);
356 return equalf (opnd1, opnd2);
364 filstat(char *nm, enum token mode)
368 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
373 return (eaccess(nm, R_OK) == 0);
375 return (eaccess(nm, W_OK) == 0);
377 /* XXX work around eaccess(2) false positives for superuser */
378 if (eaccess(nm, X_OK) != 0)
380 if (S_ISDIR(s.st_mode) || geteuid() != 0)
382 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
384 return (eaccess(nm, F_OK) == 0);
386 return S_ISREG(s.st_mode);
388 return S_ISDIR(s.st_mode);
390 return S_ISCHR(s.st_mode);
392 return S_ISBLK(s.st_mode);
394 return S_ISFIFO(s.st_mode);
396 return S_ISSOCK(s.st_mode);
398 return S_ISLNK(s.st_mode);
400 return (s.st_mode & S_ISUID) != 0;
402 return (s.st_mode & S_ISGID) != 0;
404 return (s.st_mode & S_ISVTX) != 0;
406 return s.st_size > (off_t)0;
408 return s.st_uid == geteuid();
410 return s.st_gid == getegid();
419 struct t_op const *op = ops;
425 while (op->op_text) {
426 if (strcmp(s, op->op_text) == 0) {
427 if (((op->op_type == UNOP || op->op_type == BUNOP)
428 && isunopoperand()) ||
429 (op->op_num == LPAREN && islparenoperand()) ||
430 (op->op_num == RPAREN && isrparenoperand()))
444 struct t_op const *op = ops;
452 return parenlevel == 1 && strcmp(s, ")") == 0;
454 while (op->op_text) {
455 if (strcmp(s, op->op_text) == 0)
456 return op->op_type == BINOP &&
457 (parenlevel == 0 || t[0] != ')' || t[1] != '\0');
464 islparenoperand(void)
466 struct t_op const *op = ops;
473 return parenlevel == 1 && strcmp(s, ")") == 0;
476 while (op->op_text) {
477 if (strcmp(s, op->op_text) == 0)
478 return op->op_type == BINOP;
485 isrparenoperand(void)
493 return parenlevel == 1 && strcmp(s, ")") == 0;
497 /* atoi with error detection */
505 r = strtol(s, &p, 10);
508 error("%s: bad number", s);
511 error((errno == EINVAL) ? "%s: bad number" :
512 "%s: out of range", s);
514 while (isspace((unsigned char)*p))
518 error("%s: bad number", s);
523 /* atoi with error detection and 64 bit range */
531 r = strtoimax(s, &p, 10);
534 error("%s: bad number", s);
537 error((errno == EINVAL) ? "%s: bad number" :
538 "%s: out of range", s);
540 while (isspace((unsigned char)*p))
544 error("%s: bad number", s);
550 intcmp (const char *s1, const char *s2)
568 newerf (const char *f1, const char *f2)
572 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
575 if (b1.st_mtimespec.tv_sec > b2.st_mtimespec.tv_sec)
577 if (b1.st_mtimespec.tv_sec < b2.st_mtimespec.tv_sec)
580 return (b1.st_mtimespec.tv_nsec > b2.st_mtimespec.tv_nsec);
584 olderf (const char *f1, const char *f2)
586 return (newerf(f2, f1));
590 equalf (const char *f1, const char *f2)
594 return (stat (f1, &b1) == 0 &&
595 stat (f2, &b2) == 0 &&
596 b1.st_dev == b2.st_dev &&
597 b1.st_ino == b2.st_ino);