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>
34 #include "bltin/bltin.h"
38 static void error(const char *, ...) __attribute__((__noreturn__))
42 error(const char *msg, ...)
52 /* test(1) accepts the following grammar:
53 oexpr ::= aexpr | aexpr "-o" oexpr ;
54 aexpr ::= nexpr | nexpr "-a" aexpr ;
55 nexpr ::= primary | "!" primary
56 primary ::= unary-operator operand
57 | operand binary-operator operand
61 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
62 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
64 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
66 operand ::= <any legal UNIX file name>
122 short op_num, op_type;
127 {"-e", FILEXIST,UNOP},
128 {"-f", FILREG, UNOP},
129 {"-d", FILDIR, UNOP},
130 {"-c", FILCDEV,UNOP},
131 {"-b", FILBDEV,UNOP},
132 {"-p", FILFIFO,UNOP},
133 {"-u", FILSUID,UNOP},
134 {"-g", FILSGID,UNOP},
135 {"-k", FILSTCK,UNOP},
140 {"-h", FILSYM, UNOP}, /* for backwards compat */
141 {"-O", FILUID, UNOP},
142 {"-G", FILGID, UNOP},
143 {"-L", FILSYM, UNOP},
144 {"-S", FILSOCK,UNOP},
146 {"!=", STRNE, BINOP},
149 {"-eq", INTEQ, BINOP},
150 {"-ne", INTNE, BINOP},
151 {"-ge", INTGE, BINOP},
152 {"-gt", INTGT, BINOP},
153 {"-le", INTLE, BINOP},
154 {"-lt", INTLT, BINOP},
155 {"-nt", FILNT, BINOP},
156 {"-ot", FILOT, BINOP},
157 {"-ef", FILEQ, BINOP},
159 {"-a", BAND, BBINOP},
161 {"(", LPAREN, PAREN},
162 {")", RPAREN, PAREN},
166 struct t_op const *t_wp_op;
169 static int aexpr(enum token);
170 static int binop(void);
171 static int equalf(const char *, const char *);
172 static int filstat(char *, enum token);
173 static int getn(const char *);
174 static intmax_t getq(const char *);
175 static int intcmp(const char *, const char *);
176 static int isoperand(void);
177 static int newerf(const char *, const char *);
178 static int nexpr(enum token);
179 static int oexpr(enum token);
180 static int olderf(const char *, const char *);
181 static int primary(enum token);
182 static void syntax(const char *, const char *);
183 static enum token t_lex(char *);
186 main(int argc, char **argv)
193 * XXX copy the whole contents of argv to a newly allocated
194 * space with two extra cells filled with NULL's - this source
195 * code totally depends on their presence.
197 if ((nargv = (char **)malloc((argc + 2) * sizeof(char *))) == NULL)
198 error("Out of space");
200 for (i = 0; i < argc; i++)
203 nargv[i] = nargv[i + 1] = NULL;
206 if ((p = rindex(argv[0], '/')) == NULL)
210 if (strcmp(p, "[") == 0) {
211 if (strcmp(argv[--argc], "]") != 0)
217 (void)setlocale(LC_CTYPE, "");
220 res = !oexpr(t_lex(*t_wp));
222 if (*t_wp != NULL && *++t_wp != NULL)
223 syntax(*t_wp, "unexpected operator");
229 syntax(const char *op, const char *msg)
233 error("%s: %s", op, msg);
244 if (t_lex(*++t_wp) == BOR)
245 return oexpr(t_lex(*++t_wp)) || res;
256 if (t_lex(*++t_wp) == BAND)
257 return aexpr(t_lex(*++t_wp)) && res;
266 return !nexpr(t_lex(*++t_wp));
271 primary(enum token n)
277 return 0; /* missing expression */
279 if ((nn = t_lex(*++t_wp)) == RPAREN)
280 return 0; /* missing expression */
282 if (t_lex(*++t_wp) != RPAREN)
283 syntax(NULL, "closing paren expected");
286 if (t_wp_op && t_wp_op->op_type == UNOP) {
287 /* unary expression */
289 syntax(t_wp_op->op_text, "argument expected");
292 return strlen(*t_wp) == 0;
294 return strlen(*t_wp) != 0;
296 return isatty(getn(*t_wp));
298 return filstat(*t_wp, n);
302 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
306 return strlen(*t_wp) > 0;
312 const char *opnd1, *opnd2;
313 struct t_op const *op;
316 (void) t_lex(*++t_wp);
319 if ((opnd2 = *++t_wp) == NULL)
320 syntax(op->op_text, "argument expected");
322 switch (op->op_num) {
324 return strcmp(opnd1, opnd2) == 0;
326 return strcmp(opnd1, opnd2) != 0;
328 return strcmp(opnd1, opnd2) < 0;
330 return strcmp(opnd1, opnd2) > 0;
332 return intcmp(opnd1, opnd2) == 0;
334 return intcmp(opnd1, opnd2) != 0;
336 return intcmp(opnd1, opnd2) >= 0;
338 return intcmp(opnd1, opnd2) > 0;
340 return intcmp(opnd1, opnd2) <= 0;
342 return intcmp(opnd1, opnd2) < 0;
344 return newerf (opnd1, opnd2);
346 return olderf (opnd1, opnd2);
348 return equalf (opnd1, opnd2);
356 filstat(char *nm, enum token mode)
360 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
365 return (eaccess(nm, R_OK) == 0);
367 return (eaccess(nm, W_OK) == 0);
369 /* XXX work around eaccess(2) false positives for superuser */
370 if (eaccess(nm, X_OK) != 0)
372 if (S_ISDIR(s.st_mode) || geteuid() != 0)
374 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
376 return (eaccess(nm, F_OK) == 0);
378 return S_ISREG(s.st_mode);
380 return S_ISDIR(s.st_mode);
382 return S_ISCHR(s.st_mode);
384 return S_ISBLK(s.st_mode);
386 return S_ISFIFO(s.st_mode);
388 return S_ISSOCK(s.st_mode);
390 return S_ISLNK(s.st_mode);
392 return (s.st_mode & S_ISUID) != 0;
394 return (s.st_mode & S_ISGID) != 0;
396 return (s.st_mode & S_ISVTX) != 0;
398 return s.st_size > (off_t)0;
400 return s.st_uid == geteuid();
402 return s.st_gid == getegid();
411 struct t_op const *op = ops;
417 while (op->op_text) {
418 if (strcmp(s, op->op_text) == 0) {
419 if ((op->op_type == UNOP && isoperand()) ||
420 (op->op_num == LPAREN && *(t_wp+1) == 0))
434 struct t_op const *op = ops;
438 if ((s = *(t_wp+1)) == 0)
440 if ((t = *(t_wp+2)) == 0)
442 while (op->op_text) {
443 if (strcmp(s, op->op_text) == 0)
444 return op->op_type == BINOP &&
445 (t[0] != ')' || t[1] != '\0');
451 /* atoi with error detection */
459 r = strtol(s, &p, 10);
462 error("%s: bad number", s);
465 error((errno == EINVAL) ? "%s: bad number" :
466 "%s: out of range", s);
468 while (isspace((unsigned char)*p))
472 error("%s: bad number", s);
477 /* atoi with error detection and 64 bit range */
485 r = strtoimax(s, &p, 10);
488 error("%s: bad number", s);
491 error((errno == EINVAL) ? "%s: bad number" :
492 "%s: out of range", s);
494 while (isspace((unsigned char)*p))
498 error("%s: bad number", s);
504 intcmp (const char *s1, const char *s2)
522 newerf (const char *f1, const char *f2)
526 return (stat (f1, &b1) == 0 &&
527 stat (f2, &b2) == 0 &&
528 b1.st_mtime > b2.st_mtime);
532 olderf (const char *f1, const char *f2)
536 return (stat (f1, &b1) == 0 &&
537 stat (f2, &b2) == 0 &&
538 b1.st_mtime < b2.st_mtime);
542 equalf (const char *f1, const char *f2)
546 return (stat (f1, &b1) == 0 &&
547 stat (f2, &b2) == 0 &&
548 b1.st_dev == b2.st_dev &&
549 b1.st_ino == b2.st_ino);