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