xref: /openbsd-src/bin/test/test.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: test.c,v 1.10 2009/03/01 20:11:06 otto Exp $	*/
2 /*	$NetBSD: test.c,v 1.15 1995/03/21 07:04:06 cgd Exp $	*/
3 
4 /*
5  * test(1); version 7-like  --  author Erik Baalbergen
6  * modified by Eric Gisin to be used as built-in.
7  * modified by Arnold Robbins to add SVR3 compatibility
8  * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
9  * modified by J.T. Conklin for NetBSD.
10  *
11  * This program is in the Public Domain.
12  */
13 
14 #ifndef lint
15 static char rcsid[] = "$OpenBSD: test.c,v 1.10 2009/03/01 20:11:06 otto Exp $";
16 #endif
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <err.h>
27 
28 /* test(1) accepts the following grammar:
29 	oexpr	::= aexpr | aexpr "-o" oexpr ;
30 	aexpr	::= nexpr | nexpr "-a" aexpr ;
31 	nexpr	::= primary | "!" primary
32 	primary	::= unary-operator operand
33 		| operand binary-operator operand
34 		| operand
35 		| "(" oexpr ")"
36 		;
37 	unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
38 		"-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
39 
40 	binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
41 			"-nt"|"-ot"|"-ef";
42 	operand ::= <any legal UNIX file name>
43 */
44 
45 enum token {
46 	EOI,
47 	FILRD,
48 	FILWR,
49 	FILEX,
50 	FILEXIST,
51 	FILREG,
52 	FILDIR,
53 	FILCDEV,
54 	FILBDEV,
55 	FILFIFO,
56 	FILSOCK,
57 	FILSYM,
58 	FILGZ,
59 	FILTT,
60 	FILSUID,
61 	FILSGID,
62 	FILSTCK,
63 	FILNT,
64 	FILOT,
65 	FILEQ,
66 	FILUID,
67 	FILGID,
68 	STREZ,
69 	STRNZ,
70 	STREQ,
71 	STRNE,
72 	STRLT,
73 	STRGT,
74 	INTEQ,
75 	INTNE,
76 	INTGE,
77 	INTGT,
78 	INTLE,
79 	INTLT,
80 	UNOT,
81 	BAND,
82 	BOR,
83 	LPAREN,
84 	RPAREN,
85 	OPERAND
86 };
87 
88 enum token_types {
89 	UNOP,
90 	BINOP,
91 	BUNOP,
92 	BBINOP,
93 	PAREN
94 };
95 
96 struct t_op {
97 	const char *op_text;
98 	short op_num, op_type;
99 } const ops [] = {
100 	{"-r",	FILRD,	UNOP},
101 	{"-w",	FILWR,	UNOP},
102 	{"-x",	FILEX,	UNOP},
103 	{"-e",	FILEXIST,UNOP},
104 	{"-f",	FILREG,	UNOP},
105 	{"-d",	FILDIR,	UNOP},
106 	{"-c",	FILCDEV,UNOP},
107 	{"-b",	FILBDEV,UNOP},
108 	{"-p",	FILFIFO,UNOP},
109 	{"-u",	FILSUID,UNOP},
110 	{"-g",	FILSGID,UNOP},
111 	{"-k",	FILSTCK,UNOP},
112 	{"-s",	FILGZ,	UNOP},
113 	{"-t",	FILTT,	UNOP},
114 	{"-z",	STREZ,	UNOP},
115 	{"-n",	STRNZ,	UNOP},
116 	{"-h",	FILSYM,	UNOP},		/* for backwards compat */
117 	{"-O",	FILUID,	UNOP},
118 	{"-G",	FILGID,	UNOP},
119 	{"-L",	FILSYM,	UNOP},
120 	{"-S",	FILSOCK,UNOP},
121 	{"=",	STREQ,	BINOP},
122 	{"!=",	STRNE,	BINOP},
123 	{"<",	STRLT,	BINOP},
124 	{">",	STRGT,	BINOP},
125 	{"-eq",	INTEQ,	BINOP},
126 	{"-ne",	INTNE,	BINOP},
127 	{"-ge",	INTGE,	BINOP},
128 	{"-gt",	INTGT,	BINOP},
129 	{"-le",	INTLE,	BINOP},
130 	{"-lt",	INTLT,	BINOP},
131 	{"-nt",	FILNT,	BINOP},
132 	{"-ot",	FILOT,	BINOP},
133 	{"-ef",	FILEQ,	BINOP},
134 	{"!",	UNOT,	BUNOP},
135 	{"-a",	BAND,	BBINOP},
136 	{"-o",	BOR,	BBINOP},
137 	{"(",	LPAREN,	PAREN},
138 	{")",	RPAREN,	PAREN},
139 	{0,	0,	0}
140 };
141 
142 char **t_wp;
143 struct t_op const *t_wp_op;
144 
145 static enum token t_lex(char *);
146 static enum token t_lex_type(char *);
147 static int oexpr(enum token n);
148 static int aexpr(enum token n);
149 static int nexpr(enum token n);
150 static int binop(void);
151 static int primary(enum token n);
152 static int filstat(char *nm, enum token mode);
153 static int getn(const char *s);
154 static int newerf(const char *, const char *);
155 static int olderf(const char *, const char *);
156 static int equalf(const char *, const char *);
157 static void syntax(const char *op, char *msg);
158 
159 int
160 main(int argc, char *argv[])
161 {
162 	int	res;
163 
164 	if (strcmp(argv[0], "[") == 0) {
165 		if (strcmp(argv[--argc], "]"))
166 			errx(2, "missing ]");
167 		argv[argc] = NULL;
168 	}
169 
170 	/* Implement special cases from POSIX.2, section 4.62.4 */
171 	switch (argc) {
172 	case 1:
173 		return 1;
174 	case 2:
175 		return (*argv[1] == '\0');
176 	case 3:
177 		if (argv[1][0] == '!' && argv[1][1] == '\0') {
178 			return !(*argv[2] == '\0');
179 		}
180 		break;
181 	case 4:
182 		if (argv[1][0] != '!' || argv[1][1] != '\0') {
183 			if (t_lex(argv[2]),
184 			    t_wp_op && t_wp_op->op_type == BINOP) {
185 				t_wp = &argv[1];
186 				return (binop() == 0);
187 			}
188 		}
189 		break;
190 	case 5:
191 		if (argv[1][0] == '!' && argv[1][1] == '\0') {
192 			if (t_lex(argv[3]),
193 			    t_wp_op && t_wp_op->op_type == BINOP) {
194 				t_wp = &argv[2];
195 				return !(binop() == 0);
196 			}
197 		}
198 		break;
199 	}
200 
201 	t_wp = &argv[1];
202 	res = !oexpr(t_lex(*t_wp));
203 
204 	if (*t_wp != NULL && *++t_wp != NULL)
205 		syntax(*t_wp, "unknown operand");
206 
207 	return res;
208 }
209 
210 static __dead void
211 syntax(const char *op, char *msg)
212 {
213 	if (op && *op)
214 		errx(2, "%s: %s", op, msg);
215 	else
216 		errx(2, "%s", msg);
217 }
218 
219 static int
220 oexpr(enum token n)
221 {
222 	int res;
223 
224 	res = aexpr(n);
225 	if (t_lex(*++t_wp) == BOR)
226 		return oexpr(t_lex(*++t_wp)) || res;
227 	t_wp--;
228 	return res;
229 }
230 
231 static int
232 aexpr(enum token n)
233 {
234 	int res;
235 
236 	res = nexpr(n);
237 	if (t_lex(*++t_wp) == BAND)
238 		return aexpr(t_lex(*++t_wp)) && res;
239 	t_wp--;
240 	return res;
241 }
242 
243 static int
244 nexpr(enum token n)
245 {
246 	if (n == UNOT)
247 		return !nexpr(t_lex(*++t_wp));
248 	return primary(n);
249 }
250 
251 static int
252 primary(enum token n)
253 {
254 	int res;
255 
256 	if (n == EOI)
257 		syntax(NULL, "argument expected");
258 	if (n == LPAREN) {
259 		res = oexpr(t_lex(*++t_wp));
260 		if (t_lex(*++t_wp) != RPAREN)
261 			syntax(NULL, "closing paren expected");
262 		return res;
263 	}
264 	/*
265 	 * We need this, if not binary operations with more than 4
266 	 * arguments will always fall into unary.
267 	 */
268 	if(t_lex_type(t_wp[1]) == BINOP) {
269 		t_lex(t_wp[1]);
270 		if (t_wp_op && t_wp_op->op_type == BINOP)
271 			return binop();
272 	}
273 
274 	if (t_wp_op && t_wp_op->op_type == UNOP) {
275 		/* unary expression */
276 		if (*++t_wp == NULL)
277 			syntax(t_wp_op->op_text, "argument expected");
278 		switch (n) {
279 		case STREZ:
280 			return strlen(*t_wp) == 0;
281 		case STRNZ:
282 			return strlen(*t_wp) != 0;
283 		case FILTT:
284 			return isatty(getn(*t_wp));
285 		default:
286 			return filstat(*t_wp, n);
287 		}
288 	}
289 
290 	return strlen(*t_wp) > 0;
291 }
292 
293 static int
294 binop(void)
295 {
296 	const char *opnd1, *opnd2;
297 	struct t_op const *op;
298 
299 	opnd1 = *t_wp;
300 	(void) t_lex(*++t_wp);
301 	op = t_wp_op;
302 
303 	if ((opnd2 = *++t_wp) == NULL)
304 		syntax(op->op_text, "argument expected");
305 
306 	switch (op->op_num) {
307 	case STREQ:
308 		return strcmp(opnd1, opnd2) == 0;
309 	case STRNE:
310 		return strcmp(opnd1, opnd2) != 0;
311 	case STRLT:
312 		return strcmp(opnd1, opnd2) < 0;
313 	case STRGT:
314 		return strcmp(opnd1, opnd2) > 0;
315 	case INTEQ:
316 		return getn(opnd1) == getn(opnd2);
317 	case INTNE:
318 		return getn(opnd1) != getn(opnd2);
319 	case INTGE:
320 		return getn(opnd1) >= getn(opnd2);
321 	case INTGT:
322 		return getn(opnd1) > getn(opnd2);
323 	case INTLE:
324 		return getn(opnd1) <= getn(opnd2);
325 	case INTLT:
326 		return getn(opnd1) < getn(opnd2);
327 	case FILNT:
328 		return newerf(opnd1, opnd2);
329 	case FILOT:
330 		return olderf(opnd1, opnd2);
331 	case FILEQ:
332 		return equalf(opnd1, opnd2);
333 	}
334 	/* NOTREACHED */
335 }
336 
337 static enum token
338 t_lex_type(char *s)
339 {
340 	struct t_op const *op = ops;
341 
342 	if (s == NULL)
343 		return -1;
344 
345 	while (op->op_text) {
346 		if (strcmp(s, op->op_text) == 0)
347 			return op->op_type;
348 		op++;
349 	}
350 	return -1;
351 }
352 
353 static int
354 filstat(char *nm, enum token mode)
355 {
356 	struct stat s;
357 	mode_t i;
358 
359 	if (mode == FILSYM) {
360 #ifdef S_IFLNK
361 		if (lstat(nm, &s) == 0) {
362 			i = S_IFLNK;
363 			goto filetype;
364 		}
365 #endif
366 		return 0;
367 	}
368 
369 	if (stat(nm, &s) != 0)
370 		return 0;
371 
372 	switch (mode) {
373 	case FILRD:
374 		return access(nm, R_OK) == 0;
375 	case FILWR:
376 		return access(nm, W_OK) == 0;
377 	case FILEX:
378 		return access(nm, X_OK) == 0;
379 	case FILEXIST:
380 		return access(nm, F_OK) == 0;
381 	case FILREG:
382 		i = S_IFREG;
383 		goto filetype;
384 	case FILDIR:
385 		i = S_IFDIR;
386 		goto filetype;
387 	case FILCDEV:
388 		i = S_IFCHR;
389 		goto filetype;
390 	case FILBDEV:
391 		i = S_IFBLK;
392 		goto filetype;
393 	case FILFIFO:
394 #ifdef S_IFIFO
395 		i = S_IFIFO;
396 		goto filetype;
397 #else
398 		return 0;
399 #endif
400 	case FILSOCK:
401 #ifdef S_IFSOCK
402 		i = S_IFSOCK;
403 		goto filetype;
404 #else
405 		return 0;
406 #endif
407 	case FILSUID:
408 		i = S_ISUID;
409 		goto filebit;
410 	case FILSGID:
411 		i = S_ISGID;
412 		goto filebit;
413 	case FILSTCK:
414 		i = S_ISVTX;
415 		goto filebit;
416 	case FILGZ:
417 		return s.st_size > 0L;
418 	case FILUID:
419 		return s.st_uid == geteuid();
420 	case FILGID:
421 		return s.st_gid == getegid();
422 	default:
423 		return 1;
424 	}
425 
426 filetype:
427 	return ((s.st_mode & S_IFMT) == i);
428 
429 filebit:
430 	return ((s.st_mode & i) != 0);
431 }
432 
433 static enum token
434 t_lex(char *s)
435 {
436 	struct t_op const *op = ops;
437 
438 	if (s == 0) {
439 		t_wp_op = NULL;
440 		return EOI;
441 	}
442 	while (op->op_text) {
443 		if (strcmp(s, op->op_text) == 0) {
444 			t_wp_op = op;
445 			return op->op_num;
446 		}
447 		op++;
448 	}
449 	t_wp_op = NULL;
450 	return OPERAND;
451 }
452 
453 /* atoi with error detection */
454 static int
455 getn(const char *s)
456 {
457 	char *p;
458 	long r;
459 
460 	errno = 0;
461 	r = strtol(s, &p, 10);
462 
463 	if (errno != 0)
464 		errx(2, "%s: out of range", s);
465 
466 	while (isspace(*p))
467 		p++;
468 
469 	if (*p)
470 		errx(2, "%s: bad number", s);
471 
472 	return (int) r;
473 }
474 
475 static int
476 newerf(const char *f1, const char *f2)
477 {
478 	struct stat b1, b2;
479 
480 	return (stat(f1, &b1) == 0 &&
481 	    stat(f2, &b2) == 0 &&
482 	    b1.st_mtime > b2.st_mtime);
483 }
484 
485 static int
486 olderf(const char *f1, const char *f2)
487 {
488 	struct stat b1, b2;
489 
490 	return (stat(f1, &b1) == 0 &&
491 	    stat(f2, &b2) == 0 &&
492 	    b1.st_mtime < b2.st_mtime);
493 }
494 
495 static int
496 equalf(const char *f1, const char *f2)
497 {
498 	struct stat b1, b2;
499 
500 	return (stat(f1, &b1) == 0 &&
501 	    stat(f2, &b2) == 0 &&
502 	    b1.st_dev == b2.st_dev &&
503 	    b1.st_ino == b2.st_ino);
504 }
505