1 /* $NetBSD: test.c,v 1.21 1999/04/05 09:48:38 kleink Exp $ */
2
3 /*-
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.
9 *
10 * This program is in the Public Domain.
11 */
12 /*
13 * Important: This file is used both as a standalone program /bin/test and
14 * as a builtin for /bin/sh (#define SHELL).
15 */
16
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22
23 #include <ctype.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <inttypes.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #ifdef SHELL
33 #define main testcmd
34 #include "bltin/bltin.h"
35 #else
36 #include <locale.h>
37
38 static void error(const char *, ...) __dead2 __printf0like(1, 2);
39
40 static void
error(const char * msg,...)41 error(const char *msg, ...)
42 {
43 va_list ap;
44 va_start(ap, msg);
45 verrx(2, msg, ap);
46 /*NOTREACHED*/
47 va_end(ap);
48 }
49 #endif
50
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
57 | operand
58 | "(" oexpr ")"
59 ;
60 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
61 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
62
63 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
64 "-nt"|"-ot"|"-ef";
65 operand ::= <any legal UNIX file name>
66 */
67
68 enum token_types {
69 UNOP = 0x100,
70 BINOP = 0x200,
71 BUNOP = 0x300,
72 BBINOP = 0x400,
73 PAREN = 0x500
74 };
75
76 enum token {
77 EOI,
78 OPERAND,
79 FILRD = UNOP + 1,
80 FILWR,
81 FILEX,
82 FILEXIST,
83 FILREG,
84 FILDIR,
85 FILCDEV,
86 FILBDEV,
87 FILFIFO,
88 FILSOCK,
89 FILSYM,
90 FILGZ,
91 FILTT,
92 FILSUID,
93 FILSGID,
94 FILSTCK,
95 STREZ,
96 STRNZ,
97 FILUID,
98 FILGID,
99 FILNT = BINOP + 1,
100 FILOT,
101 FILEQ,
102 STREQ,
103 STRNE,
104 STRLT,
105 STRGT,
106 INTEQ,
107 INTNE,
108 INTGE,
109 INTGT,
110 INTLE,
111 INTLT,
112 UNOT = BUNOP + 1,
113 BAND = BBINOP + 1,
114 BOR,
115 LPAREN = PAREN + 1,
116 RPAREN
117 };
118
119 #define TOKEN_TYPE(token) ((token) & 0xff00)
120
121 static const struct t_op {
122 char op_text[2];
123 short op_num;
124 } ops1[] = {
125 {"=", STREQ},
126 {"<", STRLT},
127 {">", STRGT},
128 {"!", UNOT},
129 {"(", LPAREN},
130 {")", RPAREN},
131 }, opsm1[] = {
132 {"r", FILRD},
133 {"w", FILWR},
134 {"x", FILEX},
135 {"e", FILEXIST},
136 {"f", FILREG},
137 {"d", FILDIR},
138 {"c", FILCDEV},
139 {"b", FILBDEV},
140 {"p", FILFIFO},
141 {"u", FILSUID},
142 {"g", FILSGID},
143 {"k", FILSTCK},
144 {"s", FILGZ},
145 {"t", FILTT},
146 {"z", STREZ},
147 {"n", STRNZ},
148 {"h", FILSYM}, /* for backwards compat */
149 {"O", FILUID},
150 {"G", FILGID},
151 {"L", FILSYM},
152 {"S", FILSOCK},
153 {"a", BAND},
154 {"o", BOR},
155 }, ops2[] = {
156 {"==", STREQ},
157 {"!=", STRNE},
158 }, opsm2[] = {
159 {"eq", INTEQ},
160 {"ne", INTNE},
161 {"ge", INTGE},
162 {"gt", INTGT},
163 {"le", INTLE},
164 {"lt", INTLT},
165 {"nt", FILNT},
166 {"ot", FILOT},
167 {"ef", FILEQ},
168 };
169
170 static int nargc;
171 static char **t_wp;
172 static int parenlevel;
173
174 static int aexpr(enum token);
175 static int binop(enum token);
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 *);
191
192 int
main(int argc,char ** argv)193 main(int argc, char **argv)
194 {
195 int res;
196 char *p;
197
198 if ((p = strrchr(argv[0], '/')) == NULL)
199 p = argv[0];
200 else
201 p++;
202 if (strcmp(p, "[") == 0) {
203 if (strcmp(argv[--argc], "]") != 0)
204 error("missing ]");
205 argv[argc] = NULL;
206 }
207
208 /* no expression => false */
209 if (--argc <= 0)
210 return 1;
211
212 #ifndef SHELL
213 (void)setlocale(LC_CTYPE, "");
214 #endif
215 nargc = argc;
216 t_wp = &argv[1];
217 parenlevel = 0;
218 if (nargc == 4 && strcmp(*t_wp, "!") == 0) {
219 /* Things like ! "" -o x do not fit in the normal grammar. */
220 --nargc;
221 ++t_wp;
222 res = oexpr(t_lex(*t_wp));
223 } else
224 res = !oexpr(t_lex(*t_wp));
225
226 if (--nargc > 0)
227 syntax(*t_wp, "unexpected operator");
228
229 return res;
230 }
231
232 static void
syntax(const char * op,const char * msg)233 syntax(const char *op, const char *msg)
234 {
235
236 if (op && *op)
237 error("%s: %s", op, msg);
238 else
239 error("%s", msg);
240 }
241
242 static int
oexpr(enum token n)243 oexpr(enum token n)
244 {
245 int res;
246
247 res = aexpr(n);
248 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR)
249 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ||
250 res;
251 t_wp--;
252 nargc++;
253 return res;
254 }
255
256 static int
aexpr(enum token n)257 aexpr(enum token n)
258 {
259 int res;
260
261 res = nexpr(n);
262 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND)
263 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) &&
264 res;
265 t_wp--;
266 nargc++;
267 return res;
268 }
269
270 static int
nexpr(enum token n)271 nexpr(enum token n)
272 {
273 if (n == UNOT)
274 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL));
275 return primary(n);
276 }
277
278 static int
primary(enum token n)279 primary(enum token n)
280 {
281 enum token nn;
282 int res;
283
284 if (n == EOI)
285 return 0; /* missing expression */
286 if (n == LPAREN) {
287 parenlevel++;
288 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) ==
289 RPAREN) {
290 parenlevel--;
291 return 0; /* missing expression */
292 }
293 res = oexpr(nn);
294 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN)
295 syntax(NULL, "closing paren expected");
296 parenlevel--;
297 return res;
298 }
299 if (TOKEN_TYPE(n) == UNOP) {
300 /* unary expression */
301 if (--nargc == 0)
302 syntax(NULL, "argument expected"); /* impossible */
303 switch (n) {
304 case STREZ:
305 return strlen(*++t_wp) == 0;
306 case STRNZ:
307 return strlen(*++t_wp) != 0;
308 case FILTT:
309 return isatty(getn(*++t_wp));
310 default:
311 return filstat(*++t_wp, n);
312 }
313 }
314
315 nn = t_lex(nargc > 0 ? t_wp[1] : NULL);
316 if (TOKEN_TYPE(nn) == BINOP)
317 return binop(nn);
318
319 return strlen(*t_wp) > 0;
320 }
321
322 static int
binop(enum token n)323 binop(enum token n)
324 {
325 const char *opnd1, *op, *opnd2;
326
327 opnd1 = *t_wp;
328 op = nargc > 0 ? (--nargc, *++t_wp) : NULL;
329
330 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL)
331 syntax(op, "argument expected");
332
333 switch (n) {
334 case STREQ:
335 return strcmp(opnd1, opnd2) == 0;
336 case STRNE:
337 return strcmp(opnd1, opnd2) != 0;
338 case STRLT:
339 return strcmp(opnd1, opnd2) < 0;
340 case STRGT:
341 return strcmp(opnd1, opnd2) > 0;
342 case INTEQ:
343 return intcmp(opnd1, opnd2) == 0;
344 case INTNE:
345 return intcmp(opnd1, opnd2) != 0;
346 case INTGE:
347 return intcmp(opnd1, opnd2) >= 0;
348 case INTGT:
349 return intcmp(opnd1, opnd2) > 0;
350 case INTLE:
351 return intcmp(opnd1, opnd2) <= 0;
352 case INTLT:
353 return intcmp(opnd1, opnd2) < 0;
354 case FILNT:
355 return newerf (opnd1, opnd2);
356 case FILOT:
357 return olderf (opnd1, opnd2);
358 case FILEQ:
359 return equalf (opnd1, opnd2);
360 default:
361 abort();
362 /* NOTREACHED */
363 }
364 }
365
366 static int
filstat(char * nm,enum token mode)367 filstat(char *nm, enum token mode)
368 {
369 struct stat s;
370
371 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
372 return 0;
373
374 switch (mode) {
375 case FILRD:
376 return (eaccess(nm, R_OK) == 0);
377 case FILWR:
378 return (eaccess(nm, W_OK) == 0);
379 case FILEX:
380 /* XXX work around eaccess(2) false positives for superuser */
381 if (eaccess(nm, X_OK) != 0)
382 return 0;
383 if (S_ISDIR(s.st_mode) || geteuid() != 0)
384 return 1;
385 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
386 case FILEXIST:
387 return (eaccess(nm, F_OK) == 0);
388 case FILREG:
389 return S_ISREG(s.st_mode);
390 case FILDIR:
391 return S_ISDIR(s.st_mode);
392 case FILCDEV:
393 return S_ISCHR(s.st_mode);
394 case FILBDEV:
395 return S_ISBLK(s.st_mode);
396 case FILFIFO:
397 return S_ISFIFO(s.st_mode);
398 case FILSOCK:
399 return S_ISSOCK(s.st_mode);
400 case FILSYM:
401 return S_ISLNK(s.st_mode);
402 case FILSUID:
403 return (s.st_mode & S_ISUID) != 0;
404 case FILSGID:
405 return (s.st_mode & S_ISGID) != 0;
406 case FILSTCK:
407 return (s.st_mode & S_ISVTX) != 0;
408 case FILGZ:
409 return s.st_size > (off_t)0;
410 case FILUID:
411 return s.st_uid == geteuid();
412 case FILGID:
413 return s.st_gid == getegid();
414 default:
415 return 1;
416 }
417 }
418
419 static int
find_op_1char(const struct t_op * op,const struct t_op * end,const char * s)420 find_op_1char(const struct t_op *op, const struct t_op *end, const char *s)
421 {
422 char c;
423
424 c = s[0];
425 while (op != end) {
426 if (c == *op->op_text)
427 return op->op_num;
428 op++;
429 }
430 return OPERAND;
431 }
432
433 static int
find_op_2char(const struct t_op * op,const struct t_op * end,const char * s)434 find_op_2char(const struct t_op *op, const struct t_op *end, const char *s)
435 {
436 while (op != end) {
437 if (s[0] == op->op_text[0] && s[1] == op->op_text[1])
438 return op->op_num;
439 op++;
440 }
441 return OPERAND;
442 }
443
444 static int
find_op(const char * s)445 find_op(const char *s)
446 {
447 if (s[0] == '\0')
448 return OPERAND;
449 else if (s[1] == '\0')
450 return find_op_1char(ops1, (&ops1)[1], s);
451 else if (s[2] == '\0')
452 return s[0] == '-' ? find_op_1char(opsm1, (&opsm1)[1], s + 1) :
453 find_op_2char(ops2, (&ops2)[1], s);
454 else if (s[3] == '\0')
455 return s[0] == '-' ? find_op_2char(opsm2, (&opsm2)[1], s + 1) :
456 OPERAND;
457 else
458 return OPERAND;
459 }
460
461 static enum token
t_lex(char * s)462 t_lex(char *s)
463 {
464 int num;
465
466 if (s == NULL) {
467 return EOI;
468 }
469 num = find_op(s);
470 if (((TOKEN_TYPE(num) == UNOP || TOKEN_TYPE(num) == BUNOP)
471 && isunopoperand()) ||
472 (num == LPAREN && islparenoperand()) ||
473 (num == RPAREN && isrparenoperand()))
474 return OPERAND;
475 return num;
476 }
477
478 static int
isunopoperand(void)479 isunopoperand(void)
480 {
481 char *s;
482 char *t;
483 int num;
484
485 if (nargc == 1)
486 return 1;
487 s = *(t_wp + 1);
488 if (nargc == 2)
489 return parenlevel == 1 && strcmp(s, ")") == 0;
490 t = *(t_wp + 2);
491 num = find_op(s);
492 return TOKEN_TYPE(num) == BINOP &&
493 (parenlevel == 0 || t[0] != ')' || t[1] != '\0');
494 }
495
496 static int
islparenoperand(void)497 islparenoperand(void)
498 {
499 char *s;
500 int num;
501
502 if (nargc == 1)
503 return 1;
504 s = *(t_wp + 1);
505 if (nargc == 2)
506 return parenlevel == 1 && strcmp(s, ")") == 0;
507 if (nargc != 3)
508 return 0;
509 num = find_op(s);
510 return TOKEN_TYPE(num) == BINOP;
511 }
512
513 static int
isrparenoperand(void)514 isrparenoperand(void)
515 {
516 char *s;
517
518 if (nargc == 1)
519 return 0;
520 s = *(t_wp + 1);
521 if (nargc == 2)
522 return parenlevel == 1 && strcmp(s, ")") == 0;
523 return 0;
524 }
525
526 /* atoi with error detection */
527 static int
getn(const char * s)528 getn(const char *s)
529 {
530 char *p;
531 long r;
532
533 errno = 0;
534 r = strtol(s, &p, 10);
535
536 if (s == p)
537 error("%s: bad number", s);
538
539 if (errno != 0)
540 error((errno == EINVAL) ? "%s: bad number" :
541 "%s: out of range", s);
542
543 while (isspace((unsigned char)*p))
544 p++;
545
546 if (*p)
547 error("%s: bad number", s);
548
549 return (int) r;
550 }
551
552 /* atoi with error detection and 64 bit range */
553 static intmax_t
getq(const char * s)554 getq(const char *s)
555 {
556 char *p;
557 intmax_t r;
558
559 errno = 0;
560 r = strtoimax(s, &p, 10);
561
562 if (s == p)
563 error("%s: bad number", s);
564
565 if (errno != 0)
566 error((errno == EINVAL) ? "%s: bad number" :
567 "%s: out of range", s);
568
569 while (isspace((unsigned char)*p))
570 p++;
571
572 if (*p)
573 error("%s: bad number", s);
574
575 return r;
576 }
577
578 static int
intcmp(const char * s1,const char * s2)579 intcmp (const char *s1, const char *s2)
580 {
581 intmax_t q1, q2;
582
583
584 q1 = getq(s1);
585 q2 = getq(s2);
586
587 if (q1 > q2)
588 return 1;
589
590 if (q1 < q2)
591 return -1;
592
593 return 0;
594 }
595
596 static int
newerf(const char * f1,const char * f2)597 newerf (const char *f1, const char *f2)
598 {
599 struct stat b1, b2;
600
601 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0)
602 return 0;
603
604 if (b1.st_mtim.tv_sec > b2.st_mtim.tv_sec)
605 return 1;
606 if (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec)
607 return 0;
608
609 return (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec);
610 }
611
612 static int
olderf(const char * f1,const char * f2)613 olderf (const char *f1, const char *f2)
614 {
615 return (newerf(f2, f1));
616 }
617
618 static int
equalf(const char * f1,const char * f2)619 equalf (const char *f1, const char *f2)
620 {
621 struct stat b1, b2;
622
623 return (stat (f1, &b1) == 0 &&
624 stat (f2, &b2) == 0 &&
625 b1.st_dev == b2.st_dev &&
626 b1.st_ino == b2.st_ino);
627 }
628