made "test" an ash built-in.
[platform/upstream/busybox.git] / coreutils / test.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * test implementation for busybox
4  *
5  * Copyright (c) by a whole pile of folks:
6  *
7  *     test(1); version 7-like  --  author Erik Baalbergen
8  *     modified by Eric Gisin to be used as built-in.
9  *     modified by Arnold Robbins to add SVR3 compatibility
10  *     (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
11  *     modified by J.T. Conklin for NetBSD.
12  *     modified by Herbert Xu to be used as built-in in ash.
13  *     modified by Erik Andersen <andersen@codepoet.org> to be used
14  *     in busybox.
15  *
16  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17  *
18  * Original copyright notice states:
19  *     "This program is in the Public Domain."
20  */
21
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <setjmp.h>
29 #include "busybox.h"
30
31 /* test(1) accepts the following grammar:
32         oexpr   ::= aexpr | aexpr "-o" oexpr ;
33         aexpr   ::= nexpr | nexpr "-a" aexpr ;
34         nexpr   ::= primary | "!" primary
35         primary ::= unary-operator operand
36                 | operand binary-operator operand
37                 | operand
38                 | "(" oexpr ")"
39                 ;
40         unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
41                 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
42
43         binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
44                         "-nt"|"-ot"|"-ef";
45         operand ::= <any legal UNIX file name>
46 */
47
48 enum token {
49         EOI,
50         FILRD,
51         FILWR,
52         FILEX,
53         FILEXIST,
54         FILREG,
55         FILDIR,
56         FILCDEV,
57         FILBDEV,
58         FILFIFO,
59         FILSOCK,
60         FILSYM,
61         FILGZ,
62         FILTT,
63         FILSUID,
64         FILSGID,
65         FILSTCK,
66         FILNT,
67         FILOT,
68         FILEQ,
69         FILUID,
70         FILGID,
71         STREZ,
72         STRNZ,
73         STREQ,
74         STRNE,
75         STRLT,
76         STRGT,
77         INTEQ,
78         INTNE,
79         INTGE,
80         INTGT,
81         INTLE,
82         INTLT,
83         UNOT,
84         BAND,
85         BOR,
86         LPAREN,
87         RPAREN,
88         OPERAND
89 };
90
91 enum token_types {
92         UNOP,
93         BINOP,
94         BUNOP,
95         BBINOP,
96         PAREN
97 };
98
99 static const struct t_op {
100         const char *op_text;
101         short op_num, op_type;
102 } ops[] = {
103         {
104         "-r", FILRD, UNOP}, {
105         "-w", FILWR, UNOP}, {
106         "-x", FILEX, UNOP}, {
107         "-e", FILEXIST, UNOP}, {
108         "-f", FILREG, UNOP}, {
109         "-d", FILDIR, UNOP}, {
110         "-c", FILCDEV, UNOP}, {
111         "-b", FILBDEV, UNOP}, {
112         "-p", FILFIFO, UNOP}, {
113         "-u", FILSUID, UNOP}, {
114         "-g", FILSGID, UNOP}, {
115         "-k", FILSTCK, UNOP}, {
116         "-s", FILGZ, UNOP}, {
117         "-t", FILTT, UNOP}, {
118         "-z", STREZ, UNOP}, {
119         "-n", STRNZ, UNOP}, {
120         "-h", FILSYM, UNOP},    /* for backwards compat */
121         {
122         "-O", FILUID, UNOP}, {
123         "-G", FILGID, UNOP}, {
124         "-L", FILSYM, UNOP}, {
125         "-S", FILSOCK, UNOP}, {
126         "=", STREQ, BINOP}, {
127         "==", STREQ, BINOP}, {
128         "!=", STRNE, BINOP}, {
129         "<", STRLT, BINOP}, {
130         ">", STRGT, BINOP}, {
131         "-eq", INTEQ, BINOP}, {
132         "-ne", INTNE, BINOP}, {
133         "-ge", INTGE, BINOP}, {
134         "-gt", INTGT, BINOP}, {
135         "-le", INTLE, BINOP}, {
136         "-lt", INTLT, BINOP}, {
137         "-nt", FILNT, BINOP}, {
138         "-ot", FILOT, BINOP}, {
139         "-ef", FILEQ, BINOP}, {
140         "!", UNOT, BUNOP}, {
141         "-a", BAND, BBINOP}, {
142         "-o", BOR, BBINOP}, {
143         "(", LPAREN, PAREN}, {
144         ")", RPAREN, PAREN}, {
145         0, 0, 0}
146 };
147
148 #ifdef CONFIG_FEATURE_TEST_64
149 typedef int64_t arith_t;
150 #else
151 typedef int arith_t;
152 #endif
153
154 static char **t_wp;
155 static struct t_op const *t_wp_op;
156 static gid_t *group_array = NULL;
157 static int ngroups;
158
159 static enum token t_lex(char *s);
160 static arith_t oexpr(enum token n);
161 static arith_t aexpr(enum token n);
162 static arith_t nexpr(enum token n);
163 static int binop(void);
164 static arith_t primary(enum token n);
165 static int filstat(char *nm, enum token mode);
166 static arith_t getn(const char *s);
167 static int newerf(const char *f1, const char *f2);
168 static int olderf(const char *f1, const char *f2);
169 static int equalf(const char *f1, const char *f2);
170 static int test_eaccess(char *path, int mode);
171 static int is_a_group_member(gid_t gid);
172 static void initialize_group_array(void);
173
174 static jmp_buf leaving;
175
176 int bb_test(int argc, char **argv)
177 {
178         int res;
179
180         if (strcmp(argv[0], "[") == 0) {
181                 if (strcmp(argv[--argc], "]")) {
182                         bb_error_msg("missing ]");
183                         return 2;
184                 }
185                 argv[argc] = NULL;
186         } else if (strcmp(argv[0], "[[") == 0) {
187                 if (strcmp(argv[--argc], "]]")) {
188                         bb_error_msg("missing ]]");
189                         return 2;
190                 }
191                 argv[argc] = NULL;
192         }
193
194         res = setjmp(leaving);
195         if (res)
196                 return res;
197
198         /* resetting ngroups is probably unnecessary.  it will
199          * force a new call to getgroups(), which prevents using
200          * group data fetched during a previous call.  but the
201          * only way the group data could be stale is if there's
202          * been an intervening call to setgroups(), and this
203          * isn't likely in the case of a shell.  paranoia
204          * prevails...
205          */
206          ngroups = 0;
207
208         /* Implement special cases from POSIX.2, section 4.62.4 */
209         switch (argc) {
210         case 1:
211                 return 1;
212         case 2:
213                 return *argv[1] == '\0';
214         case 3:
215                 if (argv[1][0] == '!' && argv[1][1] == '\0') {
216                         return *argv[2] != '\0';
217                 }
218                 break;
219         case 4:
220                 if (argv[1][0] != '!' || argv[1][1] != '\0') {
221                         if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) {
222                                 t_wp = &argv[1];
223                                 return binop() == 0;
224                         }
225                 }
226                 break;
227         case 5:
228                 if (argv[1][0] == '!' && argv[1][1] == '\0') {
229                         if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) {
230                                 t_wp = &argv[2];
231                                 return binop() != 0;
232                         }
233                 }
234                 break;
235         }
236
237         t_wp = &argv[1];
238         res = !oexpr(t_lex(*t_wp));
239
240         if (*t_wp != NULL && *++t_wp != NULL) {
241                 bb_error_msg("%s: unknown operand", *t_wp);
242                 return 2;
243         }
244         return res;
245 }
246
247 static void syntax(const char *op, const char *msg)
248 {
249         if (op && *op) {
250                 bb_error_msg("%s: %s", op, msg);
251         } else {
252                 bb_error_msg("%s", msg);
253         }
254         longjmp(leaving, 2);
255 }
256
257 static arith_t oexpr(enum token n)
258 {
259         arith_t res;
260
261         res = aexpr(n);
262         if (t_lex(*++t_wp) == BOR) {
263                 return oexpr(t_lex(*++t_wp)) || res;
264         }
265         t_wp--;
266         return res;
267 }
268
269 static arith_t aexpr(enum token n)
270 {
271         arith_t res;
272
273         res = nexpr(n);
274         if (t_lex(*++t_wp) == BAND)
275                 return aexpr(t_lex(*++t_wp)) && res;
276         t_wp--;
277         return res;
278 }
279
280 static arith_t nexpr(enum token n)
281 {
282         if (n == UNOT)
283                 return !nexpr(t_lex(*++t_wp));
284         return primary(n);
285 }
286
287 static arith_t primary(enum token n)
288 {
289         arith_t res;
290
291         if (n == EOI) {
292                 syntax(NULL, "argument expected");
293         }
294         if (n == LPAREN) {
295                 res = oexpr(t_lex(*++t_wp));
296                 if (t_lex(*++t_wp) != RPAREN)
297                         syntax(NULL, "closing paren expected");
298                 return res;
299         }
300         if (t_wp_op && t_wp_op->op_type == UNOP) {
301                 /* unary expression */
302                 if (*++t_wp == NULL)
303                         syntax(t_wp_op->op_text, "argument expected");
304                 switch (n) {
305                 case STREZ:
306                         return strlen(*t_wp) == 0;
307                 case STRNZ:
308                         return strlen(*t_wp) != 0;
309                 case FILTT:
310                         return isatty(getn(*t_wp));
311                 default:
312                         return filstat(*t_wp, n);
313                 }
314         }
315
316         if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
317                 return binop();
318         }
319
320         return strlen(*t_wp) > 0;
321 }
322
323 static int binop(void)
324 {
325         const char *opnd1, *opnd2;
326         struct t_op const *op;
327
328         opnd1 = *t_wp;
329         (void) t_lex(*++t_wp);
330         op = t_wp_op;
331
332         if ((opnd2 = *++t_wp) == (char *) 0)
333                 syntax(op->op_text, "argument expected");
334
335         switch (op->op_num) {
336         case STREQ:
337                 return strcmp(opnd1, opnd2) == 0;
338         case STRNE:
339                 return strcmp(opnd1, opnd2) != 0;
340         case STRLT:
341                 return strcmp(opnd1, opnd2) < 0;
342         case STRGT:
343                 return strcmp(opnd1, opnd2) > 0;
344         case INTEQ:
345                 return getn(opnd1) == getn(opnd2);
346         case INTNE:
347                 return getn(opnd1) != getn(opnd2);
348         case INTGE:
349                 return getn(opnd1) >= getn(opnd2);
350         case INTGT:
351                 return getn(opnd1) > getn(opnd2);
352         case INTLE:
353                 return getn(opnd1) <= getn(opnd2);
354         case INTLT:
355                 return getn(opnd1) < getn(opnd2);
356         case FILNT:
357                 return newerf(opnd1, opnd2);
358         case FILOT:
359                 return olderf(opnd1, opnd2);
360         case FILEQ:
361                 return equalf(opnd1, opnd2);
362         }
363         /* NOTREACHED */
364         return 1;
365 }
366
367 static int filstat(char *nm, enum token mode)
368 {
369         struct stat s;
370         unsigned int i;
371
372         if (mode == FILSYM) {
373 #ifdef S_IFLNK
374                 if (lstat(nm, &s) == 0) {
375                         i = S_IFLNK;
376                         goto filetype;
377                 }
378 #endif
379                 return 0;
380         }
381
382         if (stat(nm, &s) != 0)
383                 return 0;
384
385         switch (mode) {
386         case FILRD:
387                 return test_eaccess(nm, R_OK) == 0;
388         case FILWR:
389                 return test_eaccess(nm, W_OK) == 0;
390         case FILEX:
391                 return test_eaccess(nm, X_OK) == 0;
392         case FILEXIST:
393                 return 1;
394         case FILREG:
395                 i = S_IFREG;
396                 goto filetype;
397         case FILDIR:
398                 i = S_IFDIR;
399                 goto filetype;
400         case FILCDEV:
401                 i = S_IFCHR;
402                 goto filetype;
403         case FILBDEV:
404                 i = S_IFBLK;
405                 goto filetype;
406         case FILFIFO:
407 #ifdef S_IFIFO
408                 i = S_IFIFO;
409                 goto filetype;
410 #else
411                 return 0;
412 #endif
413         case FILSOCK:
414 #ifdef S_IFSOCK
415                 i = S_IFSOCK;
416                 goto filetype;
417 #else
418                 return 0;
419 #endif
420         case FILSUID:
421                 i = S_ISUID;
422                 goto filebit;
423         case FILSGID:
424                 i = S_ISGID;
425                 goto filebit;
426         case FILSTCK:
427                 i = S_ISVTX;
428                 goto filebit;
429         case FILGZ:
430                 return s.st_size > 0L;
431         case FILUID:
432                 return s.st_uid == geteuid();
433         case FILGID:
434                 return s.st_gid == getegid();
435         default:
436                 return 1;
437         }
438
439   filetype:
440         return ((s.st_mode & S_IFMT) == i);
441
442   filebit:
443         return ((s.st_mode & i) != 0);
444 }
445
446 static enum token t_lex(char *s)
447 {
448         struct t_op const *op = ops;
449
450         if (s == 0) {
451                 t_wp_op = (struct t_op *) 0;
452                 return EOI;
453         }
454         while (op->op_text) {
455                 if (strcmp(s, op->op_text) == 0) {
456                         t_wp_op = op;
457                         return op->op_num;
458                 }
459                 op++;
460         }
461         t_wp_op = (struct t_op *) 0;
462         return OPERAND;
463 }
464
465 /* atoi with error detection */
466 static arith_t getn(const char *s)
467 {
468         char *p;
469 #ifdef CONFIG_FEATURE_TEST_64
470         long long r;
471 #else
472         long r;
473 #endif
474
475         errno = 0;
476 #ifdef CONFIG_FEATURE_TEST_64
477         r = strtoll(s, &p, 10);
478 #else
479         r = strtol(s, &p, 10);
480 #endif
481
482         if (errno != 0)
483                 syntax(s, "out of range");
484
485         /*   p = bb_skip_whitespace(p); avoid const warning */
486         if (*(bb_skip_whitespace(p)))
487                 syntax(s, "bad number");
488
489         return r;
490 }
491
492 static int newerf(const char *f1, const char *f2)
493 {
494         struct stat b1, b2;
495
496         return (stat(f1, &b1) == 0 &&
497                         stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
498 }
499
500 static int olderf(const char *f1, const char *f2)
501 {
502         struct stat b1, b2;
503
504         return (stat(f1, &b1) == 0 &&
505                         stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
506 }
507
508 static int equalf(const char *f1, const char *f2)
509 {
510         struct stat b1, b2;
511
512         return (stat(f1, &b1) == 0 &&
513                         stat(f2, &b2) == 0 &&
514                         b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
515 }
516
517 /* Do the same thing access(2) does, but use the effective uid and gid,
518    and don't make the mistake of telling root that any file is
519    executable. */
520 static int test_eaccess(char *path, int mode)
521 {
522         struct stat st;
523         unsigned int euid = geteuid();
524
525         if (stat(path, &st) < 0)
526                 return (-1);
527
528         if (euid == 0) {
529                 /* Root can read or write any file. */
530                 if (mode != X_OK)
531                         return (0);
532
533                 /* Root can execute any file that has any one of the execute
534                    bits set. */
535                 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
536                         return (0);
537         }
538
539         if (st.st_uid == euid)  /* owner */
540                 mode <<= 6;
541         else if (is_a_group_member(st.st_gid))
542                 mode <<= 3;
543
544         if (st.st_mode & mode)
545                 return (0);
546
547         return (-1);
548 }
549
550 static void initialize_group_array(void)
551 {
552         ngroups = getgroups(0, NULL);
553         group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
554         getgroups(ngroups, group_array);
555 }
556
557 /* Return non-zero if GID is one that we have in our groups list. */
558 static int is_a_group_member(gid_t gid)
559 {
560         register int i;
561
562         /* Short-circuit if possible, maybe saving a call to getgroups(). */
563         if (gid == getgid() || gid == getegid())
564                 return (1);
565
566         if (ngroups == 0)
567                 initialize_group_array();
568
569         /* Search through the list looking for GID. */
570         for (i = 0; i < ngroups; i++)
571                 if (gid == group_array[i])
572                         return (1);
573
574         return (0);
575 }
576
577
578 /* applet entry point */
579
580 int test_main(int argc, char **argv)
581 {
582         exit(bb_test(argc, argv));
583 }
584