A number of additional fixed from Pavel Roskin, note some more bugs in the
[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 <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(argv[0], "[") == 0) {
187                 if (strcmp(argv[--argc], "]"))
188                         fatalError("missing ]\n");
189                 argv[argc] = NULL;
190         }
191         if (strcmp(argv[1], dash_dash_help) == 0) {
192                 usage("test EXPRESSION\n"
193                           "or   [ EXPRESSION ]\n"
194 #ifndef BB_FEATURE_TRIVIAL_HELP
195                                 "\nChecks file types and compares values returning an exit\n"
196                                 "code determined by the value of EXPRESSION.\n"
197 #endif
198                                 );
199         }
200
201         /* Implement special cases from POSIX.2, section 4.62.4 */
202         switch (argc) {
203         case 1:
204                 exit( 1);
205         case 2:
206                 exit (*argv[1] == '\0');
207         case 3:
208                 if (argv[1][0] == '!' && argv[1][1] == '\0') {
209                         exit (!(*argv[2] == '\0'));
210                 }
211                 break;
212         case 4:
213                 if (argv[1][0] != '!' || argv[1][1] != '\0') {
214                         if (t_lex(argv[2]), 
215                             t_wp_op && t_wp_op->op_type == BINOP) {
216                                 t_wp = &argv[1];
217                                 exit (binop() == 0);
218                         }
219                 }
220                 break;
221         case 5:
222                 if (argv[1][0] == '!' && argv[1][1] == '\0') {
223                         if (t_lex(argv[3]), 
224                             t_wp_op && t_wp_op->op_type == BINOP) {
225                                 t_wp = &argv[2];
226                                 exit (!(binop() == 0));
227                         }
228                 }
229                 break;
230         }
231
232         t_wp = &argv[1];
233         res = !oexpr(t_lex(*t_wp));
234
235         if (*t_wp != NULL && *++t_wp != NULL)
236                 syntax(*t_wp, "unknown operand");
237
238         exit( res);
239 }
240
241 static void
242 syntax(op, msg)
243         char    *op;
244         char    *msg;
245 {
246         if (op && *op)
247                 fatalError("%s: %s\n", op, msg);
248         else
249                 fatalError("%s\n", msg);
250 }
251
252 static int
253 oexpr(n)
254         enum token n;
255 {
256         int res;
257
258         res = aexpr(n);
259         if (t_lex(*++t_wp) == BOR)
260                 return oexpr(t_lex(*++t_wp)) || res;
261         t_wp--;
262         return res;
263 }
264
265 static int
266 aexpr(n)
267         enum token n;
268 {
269         int 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 int
279 nexpr(n)
280         enum token n;                   /* token */
281 {
282         if (n == UNOT)
283                 return !nexpr(t_lex(*++t_wp));
284         return primary(n);
285 }
286
287 static int
288 primary(n)
289         enum token n;
290 {
291         int res;
292
293         if (n == EOI)
294                 syntax(NULL, "argument expected");
295         if (n == LPAREN) {
296                 res = oexpr(t_lex(*++t_wp));
297                 if (t_lex(*++t_wp) != RPAREN)
298                         syntax(NULL, "closing paren expected");
299                 return res;
300         }
301         if (t_wp_op && t_wp_op->op_type == UNOP) {
302                 /* unary expression */
303                 if (*++t_wp == NULL)
304                         syntax(t_wp_op->op_text, "argument expected");
305                 switch (n) {
306                 case STREZ:
307                         return strlen(*t_wp) == 0;
308                 case STRNZ:
309                         return strlen(*t_wp) != 0;
310                 case FILTT:
311                         return isatty(getn(*t_wp));
312                 default:
313                         return filstat(*t_wp, n);
314                 }
315         }
316
317         if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
318                 return binop();
319         }         
320
321         return strlen(*t_wp) > 0;
322 }
323
324 static int
325 binop()
326 {
327         const char *opnd1, *opnd2;
328         struct t_op const *op;
329
330         opnd1 = *t_wp;
331         (void) t_lex(*++t_wp);
332         op = t_wp_op;
333
334         if ((opnd2 = *++t_wp) == (char *)0)
335                 syntax(op->op_text, "argument expected");
336                 
337         switch (op->op_num) {
338         case STREQ:
339                 return strcmp(opnd1, opnd2) == 0;
340         case STRNE:
341                 return strcmp(opnd1, opnd2) != 0;
342         case STRLT:
343                 return strcmp(opnd1, opnd2) < 0;
344         case STRGT:
345                 return strcmp(opnd1, opnd2) > 0;
346         case INTEQ:
347                 return getn(opnd1) == getn(opnd2);
348         case INTNE:
349                 return getn(opnd1) != getn(opnd2);
350         case INTGE:
351                 return getn(opnd1) >= getn(opnd2);
352         case INTGT:
353                 return getn(opnd1) > getn(opnd2);
354         case INTLE:
355                 return getn(opnd1) <= getn(opnd2);
356         case INTLT:
357                 return getn(opnd1) < getn(opnd2);
358         case FILNT:
359                 return newerf (opnd1, opnd2);
360         case FILOT:
361                 return olderf (opnd1, opnd2);
362         case FILEQ:
363                 return equalf (opnd1, opnd2);
364         }
365         /* NOTREACHED */
366         return 1;
367 }
368
369 static int
370 filstat(nm, mode)
371         char *nm;
372         enum token mode;
373 {
374         struct stat s;
375         int i;
376
377         if (mode == FILSYM) {
378 #ifdef S_IFLNK
379                 if (lstat(nm, &s) == 0) {
380                         i = S_IFLNK;
381                         goto filetype;
382                 }
383 #endif
384                 return 0;
385         }
386
387         if (stat(nm, &s) != 0) 
388                 return 0;
389
390         switch (mode) {
391         case FILRD:
392                 return test_eaccess(nm, R_OK) == 0;
393         case FILWR:
394                 return test_eaccess(nm, W_OK) == 0;
395         case FILEX:
396                 return test_eaccess(nm, X_OK) == 0;
397         case FILEXIST:
398                 return 1;
399         case FILREG:
400                 i = S_IFREG;
401                 goto filetype;
402         case FILDIR:
403                 i = S_IFDIR;
404                 goto filetype;
405         case FILCDEV:
406                 i = S_IFCHR;
407                 goto filetype;
408         case FILBDEV:
409                 i = S_IFBLK;
410                 goto filetype;
411         case FILFIFO:
412 #ifdef S_IFIFO
413                 i = S_IFIFO;
414                 goto filetype;
415 #else
416                 return 0;
417 #endif
418         case FILSOCK:
419 #ifdef S_IFSOCK
420                 i = S_IFSOCK;
421                 goto filetype;
422 #else
423                 return 0;
424 #endif
425         case FILSUID:
426                 i = S_ISUID;
427                 goto filebit;
428         case FILSGID:
429                 i = S_ISGID;
430                 goto filebit;
431         case FILSTCK:
432                 i = S_ISVTX;
433                 goto filebit;
434         case FILGZ:
435                 return s.st_size > 0L;
436         case FILUID:
437                 return s.st_uid == geteuid();
438         case FILGID:
439                 return s.st_gid == getegid();
440         default:
441                 return 1;
442         }
443
444 filetype:
445         return ((s.st_mode & S_IFMT) == i);
446
447 filebit:
448         return ((s.st_mode & i) != 0);
449 }
450
451 static enum token
452 t_lex(s)
453         char *s;
454 {
455         struct t_op const *op = ops;
456
457         if (s == 0) {
458                 t_wp_op = (struct t_op *)0;
459                 return EOI;
460         }
461         while (op->op_text) {
462                 if (strcmp(s, op->op_text) == 0) {
463                         t_wp_op = op;
464                         return op->op_num;
465                 }
466                 op++;
467         }
468         t_wp_op = (struct t_op *)0;
469         return OPERAND;
470 }
471
472 /* atoi with error detection */
473 static int
474 getn(s)
475         char *s;
476 {
477         char *p;
478         long r;
479
480         errno = 0;
481         r = strtol(s, &p, 10);
482
483         if (errno != 0)
484           fatalError("%s: out of range\n", s);
485
486         while (isspace(*p))
487           p++;
488         
489         if (*p)
490           fatalError("%s: bad number\n", s);
491
492         return (int) r;
493 }
494
495 static int
496 newerf (f1, f2)
497 char *f1, *f2;
498 {
499         struct stat b1, b2;
500
501         return (stat (f1, &b1) == 0 &&
502                 stat (f2, &b2) == 0 &&
503                 b1.st_mtime > b2.st_mtime);
504 }
505
506 static int
507 olderf (f1, f2)
508 char *f1, *f2;
509 {
510         struct stat b1, b2;
511
512         return (stat (f1, &b1) == 0 &&
513                 stat (f2, &b2) == 0 &&
514                 b1.st_mtime < b2.st_mtime);
515 }
516
517 static int
518 equalf (f1, f2)
519 char *f1, *f2;
520 {
521         struct stat b1, b2;
522
523         return (stat (f1, &b1) == 0 &&
524                 stat (f2, &b2) == 0 &&
525                 b1.st_dev == b2.st_dev &&
526                 b1.st_ino == b2.st_ino);
527 }
528
529 /* Do the same thing access(2) does, but use the effective uid and gid,
530    and don't make the mistake of telling root that any file is
531    executable. */
532 static int
533 test_eaccess (path, mode)
534 char *path;
535 int mode;
536 {
537         struct stat st;
538         int euid = geteuid();
539
540         if (stat (path, &st) < 0)
541                 return (-1);
542
543         if (euid == 0) {
544                 /* Root can read or write any file. */
545                 if (mode != X_OK)
546                 return (0);
547
548                 /* Root can execute any file that has any one of the execute
549                    bits set. */
550                 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
551                         return (0);
552         }
553
554         if (st.st_uid == euid)          /* owner */
555                 mode <<= 6;
556         else if (is_a_group_member (st.st_gid))
557                 mode <<= 3;
558
559         if (st.st_mode & mode)
560                 return (0);
561
562         return (-1);
563 }
564
565 static void
566 initialize_group_array ()
567 {
568         ngroups = getgroups(0, NULL);
569         if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
570                 fatalError("Out of space\n");
571
572         getgroups(ngroups, group_array);
573 }
574
575 /* Return non-zero if GID is one that we have in our groups list. */
576 static int
577 is_a_group_member (gid)
578 gid_t gid;
579 {
580         register int i;
581
582         /* Short-circuit if possible, maybe saving a call to getgroups(). */
583         if (gid == getgid() || gid == getegid())
584                 return (1);
585
586         if (ngroups == 0)
587                 initialize_group_array ();
588
589         /* Search through the list looking for GID. */
590         for (i = 0; i < ngroups; i++)
591                 if (gid == group_array[i])
592                         return (1);
593
594         return (0);
595 }