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