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