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;
166 static int aexpr(enum token);
167 static int binop(void);
168 static int equalf(const char *, const char *);
169 static int filstat(char *, enum token);
170 static int getn(const char *);
171 static intmax_t getq(const char *);
172 static int intcmp(const char *, const char *);
173 static int isoperand(void);
174 static int newerf(const char *, const char *);
175 static int nexpr(enum token);
176 static int oexpr(enum token);
177 static int olderf(const char *, const char *);
178 static int primary(enum token);
179 static void syntax(const char *, const char *);
180 static enum token t_lex(char *);
183 main(int argc, char **argv)
190 * XXX copy the whole contents of argv to a newly allocated
191 * space with two extra cells filled with NULL's - this source
192 * code totally depends on their presence.
194 if ((nargv = (char **)malloc((argc + 2) * sizeof(char *))) == NULL)
195 error("Out of space");
197 for (i = 0; i < argc; i++)
200 nargv[i] = nargv[i + 1] = NULL;
203 if ((p = rindex(argv[0], '/')) == NULL)
207 if (strcmp(p, "[") == 0) {
208 if (strcmp(argv[--argc], "]") != 0)
214 (void)setlocale(LC_CTYPE, "");
217 res = !oexpr(t_lex(*t_wp));
219 if (*t_wp != NULL && *++t_wp != NULL)
220 syntax(*t_wp, "unexpected operator");
227 syntax(const char *op, const char *msg)
231 error("%s: %s", op, msg);
242 if (t_lex(*++t_wp) == BOR)
243 return oexpr(t_lex(*++t_wp)) || res;
254 if (t_lex(*++t_wp) == BAND)
255 return aexpr(t_lex(*++t_wp)) && res;
264 return !nexpr(t_lex(*++t_wp));
269 primary(enum token n)
275 return 0; /* missing expression */
277 if ((nn = t_lex(*++t_wp)) == RPAREN)
278 return 0; /* missing expression */
280 if (t_lex(*++t_wp) != RPAREN)
281 syntax(NULL, "closing paren expected");
284 if (t_wp_op && t_wp_op->op_type == UNOP) {
285 /* unary expression */
287 syntax(t_wp_op->op_text, "argument expected");
290 return strlen(*t_wp) == 0;
292 return strlen(*t_wp) != 0;
294 return isatty(getn(*t_wp));
296 return filstat(*t_wp, n);
300 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
304 return strlen(*t_wp) > 0;
310 const char *opnd1, *opnd2;
311 struct t_op const *op;
314 (void) t_lex(*++t_wp);
317 if ((opnd2 = *++t_wp) == NULL)
318 syntax(op->op_text, "argument expected");
320 switch (op->op_num) {
322 return strcmp(opnd1, opnd2) == 0;
324 return strcmp(opnd1, opnd2) != 0;
326 return strcmp(opnd1, opnd2) < 0;
328 return strcmp(opnd1, opnd2) > 0;
330 return intcmp(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 newerf (opnd1, opnd2);
344 return olderf (opnd1, opnd2);
346 return equalf (opnd1, opnd2);
354 filstat(char *nm, enum token mode)
358 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
363 return (eaccess(nm, R_OK) == 0);
365 return (eaccess(nm, W_OK) == 0);
367 /* XXX work around eaccess(2) false positives for superuser */
368 if (eaccess(nm, X_OK) != 0)
370 if (S_ISDIR(s.st_mode) || geteuid() != 0)
372 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
374 return (eaccess(nm, F_OK) == 0);
376 return S_ISREG(s.st_mode);
378 return S_ISDIR(s.st_mode);
380 return S_ISCHR(s.st_mode);
382 return S_ISBLK(s.st_mode);
384 return S_ISFIFO(s.st_mode);
386 return S_ISSOCK(s.st_mode);
388 return S_ISLNK(s.st_mode);
390 return (s.st_mode & S_ISUID) != 0;
392 return (s.st_mode & S_ISGID) != 0;
394 return (s.st_mode & S_ISVTX) != 0;
396 return s.st_size > (off_t)0;
398 return s.st_uid == geteuid();
400 return s.st_gid == getegid();
409 struct t_op const *op = ops;
415 while (op->op_text) {
416 if (strcmp(s, op->op_text) == 0) {
417 if ((op->op_type == UNOP && isoperand()) ||
418 (op->op_num == LPAREN && *(t_wp+1) == 0))
432 struct t_op const *op = ops;
436 if ((s = *(t_wp+1)) == 0)
438 if ((t = *(t_wp+2)) == 0)
440 while (op->op_text) {
441 if (strcmp(s, op->op_text) == 0)
442 return op->op_type == BINOP &&
443 (t[0] != ')' || t[1] != '\0');
449 /* atoi with error detection */
457 r = strtol(s, &p, 10);
460 error("%s: bad number", s);
463 error((errno == EINVAL) ? "%s: bad number" :
464 "%s: out of range", s);
466 while (isspace((unsigned char)*p))
470 error("%s: bad number", s);
475 /* atoi with error detection and 64 bit range */
483 r = strtoimax(s, &p, 10);
486 error("%s: bad number", s);
489 error((errno == EINVAL) ? "%s: bad number" :
490 "%s: out of range", s);
492 while (isspace((unsigned char)*p))
496 error("%s: bad number", s);
502 intcmp (const char *s1, const char *s2)
520 newerf (const char *f1, const char *f2)
524 return (stat (f1, &b1) == 0 &&
525 stat (f2, &b2) == 0 &&
526 b1.st_mtime > b2.st_mtime);
530 olderf (const char *f1, const char *f2)
534 return (stat (f1, &b1) == 0 &&
535 stat (f2, &b2) == 0 &&
536 b1.st_mtime < b2.st_mtime);
540 equalf (const char *f1, const char *f2)
544 return (stat (f1, &b1) == 0 &&
545 stat (f2, &b2) == 0 &&
546 b1.st_dev == b2.st_dev &&
547 b1.st_ino == b2.st_ino);