xref: /netbsd-src/bin/test/test.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /* $NetBSD: test.c,v 1.26 2005/02/10 06:56:55 simonb 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.26 2005/02/10 06:56:55 simonb 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 
215 	if (op && *op)
216 		error("%s: %s", op, msg);
217 	else
218 		error("%s", msg);
219 }
220 
221 static int
222 oexpr(enum token n)
223 {
224 	int res;
225 
226 	res = aexpr(n);
227 	if (t_lex(*++t_wp) == BOR)
228 		return oexpr(t_lex(*++t_wp)) || res;
229 	t_wp--;
230 	return res;
231 }
232 
233 static int
234 aexpr(enum token n)
235 {
236 	int res;
237 
238 	res = nexpr(n);
239 	if (t_lex(*++t_wp) == BAND)
240 		return aexpr(t_lex(*++t_wp)) && res;
241 	t_wp--;
242 	return res;
243 }
244 
245 static int
246 nexpr(enum token n)
247 {
248 
249 	if (n == UNOT)
250 		return !nexpr(t_lex(*++t_wp));
251 	return primary(n);
252 }
253 
254 static int
255 primary(enum token n)
256 {
257 	enum token nn;
258 	int res;
259 
260 	if (n == EOI)
261 		return 0;		/* missing expression */
262 	if (n == LPAREN) {
263 		if ((nn = t_lex(*++t_wp)) == RPAREN)
264 			return 0;	/* missing expression */
265 		res = oexpr(nn);
266 		if (t_lex(*++t_wp) != RPAREN)
267 			syntax(NULL, "closing paren expected");
268 		return res;
269 	}
270 	if (t_wp_op && t_wp_op->op_type == UNOP) {
271 		/* unary expression */
272 		if (*++t_wp == NULL)
273 			syntax(t_wp_op->op_text, "argument expected");
274 		switch (n) {
275 		case STREZ:
276 			return strlen(*t_wp) == 0;
277 		case STRNZ:
278 			return strlen(*t_wp) != 0;
279 		case FILTT:
280 			return isatty(getn(*t_wp));
281 		default:
282 			return filstat(*t_wp, n);
283 		}
284 	}
285 
286 	if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
287 		return binop();
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 	default:
334 		abort();
335 		/* NOTREACHED */
336 	}
337 }
338 
339 static int
340 filstat(char *nm, enum token mode)
341 {
342 	struct stat s;
343 
344 	if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
345 		return 0;
346 
347 	switch (mode) {
348 	case FILRD:
349 		return access(nm, R_OK) == 0;
350 	case FILWR:
351 		return access(nm, W_OK) == 0;
352 	case FILEX:
353 		return access(nm, X_OK) == 0;
354 	case FILEXIST:
355 		return access(nm, F_OK) == 0;
356 	case FILREG:
357 		return S_ISREG(s.st_mode);
358 	case FILDIR:
359 		return S_ISDIR(s.st_mode);
360 	case FILCDEV:
361 		return S_ISCHR(s.st_mode);
362 	case FILBDEV:
363 		return S_ISBLK(s.st_mode);
364 	case FILFIFO:
365 		return S_ISFIFO(s.st_mode);
366 	case FILSOCK:
367 		return S_ISSOCK(s.st_mode);
368 	case FILSYM:
369 		return S_ISLNK(s.st_mode);
370 	case FILSUID:
371 		return (s.st_mode & S_ISUID) != 0;
372 	case FILSGID:
373 		return (s.st_mode & S_ISGID) != 0;
374 	case FILSTCK:
375 		return (s.st_mode & S_ISVTX) != 0;
376 	case FILGZ:
377 		return s.st_size > (off_t)0;
378 	case FILUID:
379 		return s.st_uid == geteuid();
380 	case FILGID:
381 		return s.st_gid == getegid();
382 	default:
383 		return 1;
384 	}
385 }
386 
387 static enum token
388 t_lex(char *s)
389 {
390 	struct t_op const *op;
391 
392 	op = ops;
393 
394 	if (s == 0) {
395 		t_wp_op = NULL;
396 		return EOI;
397 	}
398 	while (op->op_text) {
399 		if (strcmp(s, op->op_text) == 0) {
400 			if ((op->op_type == UNOP && isoperand()) ||
401 			    (op->op_num == LPAREN && *(t_wp+1) == 0))
402 				break;
403 			t_wp_op = op;
404 			return op->op_num;
405 		}
406 		op++;
407 	}
408 	t_wp_op = NULL;
409 	return OPERAND;
410 }
411 
412 static int
413 isoperand(void)
414 {
415 	struct t_op const *op;
416 	char *s, *t;
417 
418 	op = ops;
419 	if ((s  = *(t_wp+1)) == 0)
420 		return 1;
421 	if ((t = *(t_wp+2)) == 0)
422 		return 0;
423 	while (op->op_text) {
424 		if (strcmp(s, op->op_text) == 0)
425 	    		return op->op_type == BINOP &&
426 	    		    (t[0] != ')' || t[1] != '\0');
427 		op++;
428 	}
429 	return 0;
430 }
431 
432 /* atoi with error detection */
433 static int
434 getn(const char *s)
435 {
436 	char *p;
437 	long r;
438 
439 	errno = 0;
440 	r = strtol(s, &p, 10);
441 
442 	if (errno != 0)
443 	      error("%s: out of range", s);
444 
445 	while (isspace((unsigned char)*p))
446 	      p++;
447 
448 	if (*p)
449 	      error("%s: bad number", s);
450 
451 	return (int) r;
452 }
453 
454 static int
455 newerf(const char *f1, const char *f2)
456 {
457 	struct stat b1, b2;
458 
459 	return (stat(f1, &b1) == 0 &&
460 		stat(f2, &b2) == 0 &&
461 		b1.st_mtime > b2.st_mtime);
462 }
463 
464 static int
465 olderf(const char *f1, const char *f2)
466 {
467 	struct stat b1, b2;
468 
469 	return (stat(f1, &b1) == 0 &&
470 		stat(f2, &b2) == 0 &&
471 		b1.st_mtime < b2.st_mtime);
472 }
473 
474 static int
475 equalf(const char *f1, const char *f2)
476 {
477 	struct stat b1, b2;
478 
479 	return (stat(f1, &b1) == 0 &&
480 		stat(f2, &b2) == 0 &&
481 		b1.st_dev == b2.st_dev &&
482 		b1.st_ino == b2.st_ino);
483 }
484