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