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[] =
18 #include <sys/types.h>
33 #include "bltin/bltin.h"
37 static void error(const char *, ...) __attribute__((__noreturn__))
41 error(const char *msg, ...)
51 /* test(1) accepts the following grammar:
52 oexpr ::= aexpr | aexpr "-o" oexpr ;
53 aexpr ::= nexpr | nexpr "-a" aexpr ;
54 nexpr ::= primary | "!" primary
55 primary ::= unary-operator operand
56 | operand binary-operator operand
60 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
61 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
63 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
65 operand ::= <any legal UNIX file name>
121 short op_num, op_type;
126 {"-e", FILEXIST,UNOP},
127 {"-f", FILREG, UNOP},
128 {"-d", FILDIR, UNOP},
129 {"-c", FILCDEV,UNOP},
130 {"-b", FILBDEV,UNOP},
131 {"-p", FILFIFO,UNOP},
132 {"-u", FILSUID,UNOP},
133 {"-g", FILSGID,UNOP},
134 {"-k", FILSTCK,UNOP},
139 {"-h", FILSYM, UNOP}, /* for backwards compat */
140 {"-O", FILUID, UNOP},
141 {"-G", FILGID, UNOP},
142 {"-L", FILSYM, UNOP},
143 {"-S", FILSOCK,UNOP},
145 {"!=", STRNE, BINOP},
148 {"-eq", INTEQ, BINOP},
149 {"-ne", INTNE, BINOP},
150 {"-ge", INTGE, BINOP},
151 {"-gt", INTGT, BINOP},
152 {"-le", INTLE, BINOP},
153 {"-lt", INTLT, BINOP},
154 {"-nt", FILNT, BINOP},
155 {"-ot", FILOT, BINOP},
156 {"-ef", FILEQ, BINOP},
158 {"-a", BAND, BBINOP},
160 {"(", LPAREN, PAREN},
161 {")", RPAREN, PAREN},
165 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 long long getq(const char *);
174 static int intcmp(const char *, const char *);
175 static int isoperand(void);
176 static int newerf(const char *, const char *);
177 static int nexpr(enum token);
178 static int oexpr(enum token);
179 static int olderf(const char *, const char *);
180 static int primary(enum token);
181 static void syntax(const char *, const char *);
182 static enum token t_lex(char *);
185 main(int argc, char **argv)
192 * XXX copy the whole contents of argv to a newly allocated
193 * space with two extra cells filled with NULL's - this source
194 * code totally depends on their presence.
196 if ((nargv = (char **)malloc((argc + 2) * sizeof(char *))) == NULL)
197 error("Out of space");
199 for (i = 0; i < argc; i++)
202 nargv[i] = nargv[i + 1] = NULL;
205 if ((p = rindex(argv[0], '/')) == NULL)
209 if (strcmp(p, "[") == 0) {
210 if (strcmp(argv[--argc], "]") != 0)
216 (void)setlocale(LC_CTYPE, "");
219 res = !oexpr(t_lex(*t_wp));
221 if (*t_wp != NULL && *++t_wp != NULL)
222 syntax(*t_wp, "unexpected operator");
228 syntax(const char *op, const char *msg)
232 error("%s: %s", op, msg);
243 if (t_lex(*++t_wp) == BOR)
244 return oexpr(t_lex(*++t_wp)) || res;
255 if (t_lex(*++t_wp) == BAND)
256 return aexpr(t_lex(*++t_wp)) && res;
265 return !nexpr(t_lex(*++t_wp));
270 primary(enum token n)
276 return 0; /* missing expression */
278 if ((nn = t_lex(*++t_wp)) == RPAREN)
279 return 0; /* missing expression */
281 if (t_lex(*++t_wp) != RPAREN)
282 syntax(NULL, "closing paren expected");
285 if (t_wp_op && t_wp_op->op_type == UNOP) {
286 /* unary expression */
288 syntax(t_wp_op->op_text, "argument expected");
291 return strlen(*t_wp) == 0;
293 return strlen(*t_wp) != 0;
295 return isatty(getn(*t_wp));
297 return filstat(*t_wp, n);
301 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
305 return strlen(*t_wp) > 0;
311 const char *opnd1, *opnd2;
312 struct t_op const *op;
315 (void) t_lex(*++t_wp);
318 if ((opnd2 = *++t_wp) == NULL)
319 syntax(op->op_text, "argument expected");
321 switch (op->op_num) {
323 return strcmp(opnd1, opnd2) == 0;
325 return strcmp(opnd1, opnd2) != 0;
327 return strcmp(opnd1, opnd2) < 0;
329 return strcmp(opnd1, opnd2) > 0;
331 return intcmp(opnd1, opnd2) == 0;
333 return intcmp(opnd1, opnd2) != 0;
335 return intcmp(opnd1, opnd2) >= 0;
337 return intcmp(opnd1, opnd2) > 0;
339 return intcmp(opnd1, opnd2) <= 0;
341 return intcmp(opnd1, opnd2) < 0;
343 return newerf (opnd1, opnd2);
345 return olderf (opnd1, opnd2);
347 return equalf (opnd1, opnd2);
355 filstat(char *nm, enum token mode)
359 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
364 return (eaccess(nm, R_OK) == 0);
366 return (eaccess(nm, W_OK) == 0);
368 /* XXX work around eaccess(2) false positives for superuser */
369 if (eaccess(nm, X_OK) != 0)
371 if (S_ISDIR(s.st_mode) || geteuid() != 0)
373 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
375 return (eaccess(nm, F_OK) == 0);
377 return S_ISREG(s.st_mode);
379 return S_ISDIR(s.st_mode);
381 return S_ISCHR(s.st_mode);
383 return S_ISBLK(s.st_mode);
385 return S_ISFIFO(s.st_mode);
387 return S_ISSOCK(s.st_mode);
389 return S_ISLNK(s.st_mode);
391 return (s.st_mode & S_ISUID) != 0;
393 return (s.st_mode & S_ISGID) != 0;
395 return (s.st_mode & S_ISVTX) != 0;
397 return s.st_size > (off_t)0;
399 return s.st_uid == geteuid();
401 return s.st_gid == getegid();
410 struct t_op const *op = ops;
416 while (op->op_text) {
417 if (strcmp(s, op->op_text) == 0) {
418 if ((op->op_type == UNOP && isoperand()) ||
419 (op->op_num == LPAREN && *(t_wp+1) == 0))
433 struct t_op const *op = ops;
437 if ((s = *(t_wp+1)) == 0)
439 if ((t = *(t_wp+2)) == 0)
441 while (op->op_text) {
442 if (strcmp(s, op->op_text) == 0)
443 return op->op_type == BINOP &&
444 (t[0] != ')' || t[1] != '\0');
450 /* atoi with error detection */
458 r = strtol(s, &p, 10);
461 error("%s: bad number", s);
464 error((errno == EINVAL) ? "%s: bad number" :
465 "%s: out of range", s);
467 while (isspace((unsigned char)*p))
471 error("%s: bad number", s);
476 /* atoi with error detection and 64 bit range */
484 r = strtoll(s, &p, 10);
487 error("%s: bad number", s);
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);
503 intcmp (const char *s1, const char *s2)
521 newerf (const char *f1, const char *f2)
525 return (stat (f1, &b1) == 0 &&
526 stat (f2, &b2) == 0 &&
527 b1.st_mtime > b2.st_mtime);
531 olderf (const char *f1, const char *f2)
535 return (stat (f1, &b1) == 0 &&
536 stat (f2, &b2) == 0 &&
537 b1.st_mtime < b2.st_mtime);
541 equalf (const char *f1, const char *f2)
545 return (stat (f1, &b1) == 0 &&
546 stat (f2, &b2) == 0 &&
547 b1.st_dev == b2.st_dev &&
548 b1.st_ino == b2.st_ino);