sync
[bitrig.git] / bin / test / test.c
blob23f9757eb2839b0b1a3dbb2dd7aa416064f85381
1 /* $OpenBSD: test.c,v 1.11 2009/10/27 23:59:22 deraadt Exp $ */
2 /* $NetBSD: test.c,v 1.15 1995/03/21 07:04:06 cgd Exp $ */
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.
11 * This program is in the Public Domain.
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <err.h>
24 /* test(1) accepts the following grammar:
25 oexpr ::= aexpr | aexpr "-o" oexpr ;
26 aexpr ::= nexpr | nexpr "-a" aexpr ;
27 nexpr ::= primary | "!" primary
28 primary ::= unary-operator operand
29 | operand binary-operator operand
30 | operand
31 | "(" oexpr ")"
33 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
34 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
36 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
37 "-nt"|"-ot"|"-ef";
38 operand ::= <any legal UNIX file name>
41 enum token {
42 EOI,
43 FILRD,
44 FILWR,
45 FILEX,
46 FILEXIST,
47 FILREG,
48 FILDIR,
49 FILCDEV,
50 FILBDEV,
51 FILFIFO,
52 FILSOCK,
53 FILSYM,
54 FILGZ,
55 FILTT,
56 FILSUID,
57 FILSGID,
58 FILSTCK,
59 FILNT,
60 FILOT,
61 FILEQ,
62 FILUID,
63 FILGID,
64 STREZ,
65 STRNZ,
66 STREQ,
67 STRNE,
68 STRLT,
69 STRGT,
70 INTEQ,
71 INTNE,
72 INTGE,
73 INTGT,
74 INTLE,
75 INTLT,
76 UNOT,
77 BAND,
78 BOR,
79 LPAREN,
80 RPAREN,
81 OPERAND
84 enum token_types {
85 UNOP,
86 BINOP,
87 BUNOP,
88 BBINOP,
89 PAREN
92 struct t_op {
93 const char *op_text;
94 short op_num, op_type;
95 } const ops [] = {
96 {"-r", FILRD, UNOP},
97 {"-w", FILWR, UNOP},
98 {"-x", FILEX, UNOP},
99 {"-e", FILEXIST,UNOP},
100 {"-f", FILREG, UNOP},
101 {"-d", FILDIR, UNOP},
102 {"-c", FILCDEV,UNOP},
103 {"-b", FILBDEV,UNOP},
104 {"-p", FILFIFO,UNOP},
105 {"-u", FILSUID,UNOP},
106 {"-g", FILSGID,UNOP},
107 {"-k", FILSTCK,UNOP},
108 {"-s", FILGZ, UNOP},
109 {"-t", FILTT, UNOP},
110 {"-z", STREZ, UNOP},
111 {"-n", STRNZ, UNOP},
112 {"-h", FILSYM, UNOP}, /* for backwards compat */
113 {"-O", FILUID, UNOP},
114 {"-G", FILGID, UNOP},
115 {"-L", FILSYM, UNOP},
116 {"-S", FILSOCK,UNOP},
117 {"=", STREQ, BINOP},
118 {"!=", STRNE, BINOP},
119 {"<", STRLT, BINOP},
120 {">", STRGT, BINOP},
121 {"-eq", INTEQ, BINOP},
122 {"-ne", INTNE, BINOP},
123 {"-ge", INTGE, BINOP},
124 {"-gt", INTGT, BINOP},
125 {"-le", INTLE, BINOP},
126 {"-lt", INTLT, BINOP},
127 {"-nt", FILNT, BINOP},
128 {"-ot", FILOT, BINOP},
129 {"-ef", FILEQ, BINOP},
130 {"!", UNOT, BUNOP},
131 {"-a", BAND, BBINOP},
132 {"-o", BOR, BBINOP},
133 {"(", LPAREN, PAREN},
134 {")", RPAREN, PAREN},
135 {0, 0, 0}
138 char **t_wp;
139 struct t_op const *t_wp_op;
141 static enum token t_lex(char *);
142 static enum token t_lex_type(char *);
143 static int oexpr(enum token n);
144 static int aexpr(enum token n);
145 static int nexpr(enum token n);
146 static int binop(void);
147 static int primary(enum token n);
148 static int filstat(char *nm, enum token mode);
149 static int getn(const char *s);
150 static int newerf(const char *, const char *);
151 static int olderf(const char *, const char *);
152 static int equalf(const char *, const char *);
153 static void syntax(const char *op, char *msg);
156 main(int argc, char *argv[])
158 int res;
160 if (strcmp(argv[0], "[") == 0) {
161 if (strcmp(argv[--argc], "]"))
162 errx(2, "missing ]");
163 argv[argc] = NULL;
166 /* Implement special cases from POSIX.2, section 4.62.4 */
167 switch (argc) {
168 case 1:
169 return 1;
170 case 2:
171 return (*argv[1] == '\0');
172 case 3:
173 if (argv[1][0] == '!' && argv[1][1] == '\0') {
174 return !(*argv[2] == '\0');
176 break;
177 case 4:
178 if (argv[1][0] != '!' || argv[1][1] != '\0') {
179 if (t_lex(argv[2]),
180 t_wp_op && t_wp_op->op_type == BINOP) {
181 t_wp = &argv[1];
182 return (binop() == 0);
185 break;
186 case 5:
187 if (argv[1][0] == '!' && argv[1][1] == '\0') {
188 if (t_lex(argv[3]),
189 t_wp_op && t_wp_op->op_type == BINOP) {
190 t_wp = &argv[2];
191 return !(binop() == 0);
194 break;
197 t_wp = &argv[1];
198 res = !oexpr(t_lex(*t_wp));
200 if (*t_wp != NULL && *++t_wp != NULL)
201 syntax(*t_wp, "unknown operand");
203 return res;
206 static __dead void
207 syntax(const char *op, char *msg)
209 if (op && *op)
210 errx(2, "%s: %s", op, msg);
211 else
212 errx(2, "%s", msg);
215 static int
216 oexpr(enum token n)
218 int res;
220 res = aexpr(n);
221 if (t_lex(*++t_wp) == BOR)
222 return oexpr(t_lex(*++t_wp)) || res;
223 t_wp--;
224 return res;
227 static int
228 aexpr(enum token n)
230 int res;
232 res = nexpr(n);
233 if (t_lex(*++t_wp) == BAND)
234 return aexpr(t_lex(*++t_wp)) && res;
235 t_wp--;
236 return res;
239 static int
240 nexpr(enum token n)
242 if (n == UNOT)
243 return !nexpr(t_lex(*++t_wp));
244 return primary(n);
247 static int
248 primary(enum token n)
250 int res;
252 if (n == EOI)
253 syntax(NULL, "argument expected");
254 if (n == LPAREN) {
255 res = oexpr(t_lex(*++t_wp));
256 if (t_lex(*++t_wp) != RPAREN)
257 syntax(NULL, "closing paren expected");
258 return res;
261 * We need this, if not binary operations with more than 4
262 * arguments will always fall into unary.
264 if(t_lex_type(t_wp[1]) == BINOP) {
265 t_lex(t_wp[1]);
266 if (t_wp_op && t_wp_op->op_type == BINOP)
267 return binop();
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);
286 return strlen(*t_wp) > 0;
289 static int
290 binop(void)
292 const char *opnd1, *opnd2;
293 struct t_op const *op;
295 opnd1 = *t_wp;
296 (void) t_lex(*++t_wp);
297 op = t_wp_op;
299 if ((opnd2 = *++t_wp) == NULL)
300 syntax(op->op_text, "argument expected");
302 switch (op->op_num) {
303 case STREQ:
304 return strcmp(opnd1, opnd2) == 0;
305 case STRNE:
306 return strcmp(opnd1, opnd2) != 0;
307 case STRLT:
308 return strcmp(opnd1, opnd2) < 0;
309 case STRGT:
310 return strcmp(opnd1, opnd2) > 0;
311 case INTEQ:
312 return getn(opnd1) == getn(opnd2);
313 case INTNE:
314 return getn(opnd1) != getn(opnd2);
315 case INTGE:
316 return getn(opnd1) >= getn(opnd2);
317 case INTGT:
318 return getn(opnd1) > getn(opnd2);
319 case INTLE:
320 return getn(opnd1) <= getn(opnd2);
321 case INTLT:
322 return getn(opnd1) < getn(opnd2);
323 case FILNT:
324 return newerf(opnd1, opnd2);
325 case FILOT:
326 return olderf(opnd1, opnd2);
327 case FILEQ:
328 return equalf(opnd1, opnd2);
330 /* NOTREACHED */
333 static enum token
334 t_lex_type(char *s)
336 struct t_op const *op = ops;
338 if (s == NULL)
339 return -1;
341 while (op->op_text) {
342 if (strcmp(s, op->op_text) == 0)
343 return op->op_type;
344 op++;
346 return -1;
349 static int
350 filstat(char *nm, enum token mode)
352 struct stat s;
353 mode_t i;
355 if (mode == FILSYM) {
356 #ifdef S_IFLNK
357 if (lstat(nm, &s) == 0) {
358 i = S_IFLNK;
359 goto filetype;
361 #endif
362 return 0;
365 if (stat(nm, &s) != 0)
366 return 0;
368 switch (mode) {
369 case FILRD:
370 return access(nm, R_OK) == 0;
371 case FILWR:
372 return access(nm, W_OK) == 0;
373 case FILEX:
374 return access(nm, X_OK) == 0;
375 case FILEXIST:
376 return access(nm, F_OK) == 0;
377 case FILREG:
378 i = S_IFREG;
379 goto filetype;
380 case FILDIR:
381 i = S_IFDIR;
382 goto filetype;
383 case FILCDEV:
384 i = S_IFCHR;
385 goto filetype;
386 case FILBDEV:
387 i = S_IFBLK;
388 goto filetype;
389 case FILFIFO:
390 #ifdef S_IFIFO
391 i = S_IFIFO;
392 goto filetype;
393 #else
394 return 0;
395 #endif
396 case FILSOCK:
397 #ifdef S_IFSOCK
398 i = S_IFSOCK;
399 goto filetype;
400 #else
401 return 0;
402 #endif
403 case FILSUID:
404 i = S_ISUID;
405 goto filebit;
406 case FILSGID:
407 i = S_ISGID;
408 goto filebit;
409 case FILSTCK:
410 i = S_ISVTX;
411 goto filebit;
412 case FILGZ:
413 return s.st_size > 0L;
414 case FILUID:
415 return s.st_uid == geteuid();
416 case FILGID:
417 return s.st_gid == getegid();
418 default:
419 return 1;
422 filetype:
423 return ((s.st_mode & S_IFMT) == i);
425 filebit:
426 return ((s.st_mode & i) != 0);
429 static enum token
430 t_lex(char *s)
432 struct t_op const *op = ops;
434 if (s == 0) {
435 t_wp_op = NULL;
436 return EOI;
438 while (op->op_text) {
439 if (strcmp(s, op->op_text) == 0) {
440 t_wp_op = op;
441 return op->op_num;
443 op++;
445 t_wp_op = NULL;
446 return OPERAND;
449 /* atoi with error detection */
450 static int
451 getn(const char *s)
453 char *p;
454 long r;
456 errno = 0;
457 r = strtol(s, &p, 10);
459 if (errno != 0)
460 errx(2, "%s: out of range", s);
462 while (isspace(*p))
463 p++;
465 if (*p)
466 errx(2, "%s: bad number", s);
468 return (int) r;
471 static int
472 newerf(const char *f1, const char *f2)
474 struct stat b1, b2;
476 return (stat(f1, &b1) == 0 &&
477 stat(f2, &b2) == 0 &&
478 b1.st_mtime > b2.st_mtime);
481 static int
482 olderf(const char *f1, const char *f2)
484 struct stat b1, b2;
486 return (stat(f1, &b1) == 0 &&
487 stat(f2, &b2) == 0 &&
488 b1.st_mtime < b2.st_mtime);
491 static int
492 equalf(const char *f1, const char *f2)
494 struct stat b1, b2;
496 return (stat(f1, &b1) == 0 &&
497 stat(f2, &b2) == 0 &&
498 b1.st_dev == b2.st_dev &&
499 b1.st_ino == b2.st_ino);