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