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