Extract usage information into a separate file.
[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(applet_name, "[") == 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_usage);
193
194         /* Implement special cases from POSIX.2, section 4.62.4 */
195         switch (argc) {
196         case 1:
197                 exit( 1);
198         case 2:
199                 exit (*argv[1] == '\0');
200         case 3:
201                 if (argv[1][0] == '!' && argv[1][1] == '\0') {
202                         exit (!(*argv[2] == '\0'));
203                 }
204                 break;
205         case 4:
206                 if (argv[1][0] != '!' || argv[1][1] != '\0') {
207                         if (t_lex(argv[2]), 
208                             t_wp_op && t_wp_op->op_type == BINOP) {
209                                 t_wp = &argv[1];
210                                 exit (binop() == 0);
211                         }
212                 }
213                 break;
214         case 5:
215                 if (argv[1][0] == '!' && argv[1][1] == '\0') {
216                         if (t_lex(argv[3]), 
217                             t_wp_op && t_wp_op->op_type == BINOP) {
218                                 t_wp = &argv[2];
219                                 exit (!(binop() == 0));
220                         }
221                 }
222                 break;
223         }
224
225         t_wp = &argv[1];
226         res = !oexpr(t_lex(*t_wp));
227
228         if (*t_wp != NULL && *++t_wp != NULL)
229                 syntax(*t_wp, "unknown operand");
230
231         return( res);
232 }
233
234 static void
235 syntax(op, msg)
236         char    *op;
237         char    *msg;
238 {
239         if (op && *op)
240                 fatalError("%s: %s\n", op, msg);
241         else
242                 fatalError("%s\n", msg);
243 }
244
245 static int
246 oexpr(n)
247         enum token n;
248 {
249         int res;
250
251         res = aexpr(n);
252         if (t_lex(*++t_wp) == BOR)
253                 return oexpr(t_lex(*++t_wp)) || res;
254         t_wp--;
255         return res;
256 }
257
258 static int
259 aexpr(n)
260         enum token n;
261 {
262         int res;
263
264         res = nexpr(n);
265         if (t_lex(*++t_wp) == BAND)
266                 return aexpr(t_lex(*++t_wp)) && res;
267         t_wp--;
268         return res;
269 }
270
271 static int
272 nexpr(n)
273         enum token n;                   /* token */
274 {
275         if (n == UNOT)
276                 return !nexpr(t_lex(*++t_wp));
277         return primary(n);
278 }
279
280 static int
281 primary(n)
282         enum token n;
283 {
284         int res;
285
286         if (n == EOI)
287                 syntax(NULL, "argument expected");
288         if (n == LPAREN) {
289                 res = oexpr(t_lex(*++t_wp));
290                 if (t_lex(*++t_wp) != RPAREN)
291                         syntax(NULL, "closing paren expected");
292                 return res;
293         }
294         if (t_wp_op && t_wp_op->op_type == UNOP) {
295                 /* unary expression */
296                 if (*++t_wp == NULL)
297                         syntax(t_wp_op->op_text, "argument expected");
298                 switch (n) {
299                 case STREZ:
300                         return strlen(*t_wp) == 0;
301                 case STRNZ:
302                         return strlen(*t_wp) != 0;
303                 case FILTT:
304                         return isatty(getn(*t_wp));
305                 default:
306                         return filstat(*t_wp, n);
307                 }
308         }
309
310         if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
311                 return binop();
312         }         
313
314         return strlen(*t_wp) > 0;
315 }
316
317 static int
318 binop()
319 {
320         const char *opnd1, *opnd2;
321         struct t_op const *op;
322
323         opnd1 = *t_wp;
324         (void) t_lex(*++t_wp);
325         op = t_wp_op;
326
327         if ((opnd2 = *++t_wp) == (char *)0)
328                 syntax(op->op_text, "argument expected");
329                 
330         switch (op->op_num) {
331         case STREQ:
332                 return strcmp(opnd1, opnd2) == 0;
333         case STRNE:
334                 return strcmp(opnd1, opnd2) != 0;
335         case STRLT:
336                 return strcmp(opnd1, opnd2) < 0;
337         case STRGT:
338                 return strcmp(opnd1, opnd2) > 0;
339         case INTEQ:
340                 return getn(opnd1) == getn(opnd2);
341         case INTNE:
342                 return getn(opnd1) != getn(opnd2);
343         case INTGE:
344                 return getn(opnd1) >= getn(opnd2);
345         case INTGT:
346                 return getn(opnd1) > getn(opnd2);
347         case INTLE:
348                 return getn(opnd1) <= getn(opnd2);
349         case INTLT:
350                 return getn(opnd1) < getn(opnd2);
351         case FILNT:
352                 return newerf (opnd1, opnd2);
353         case FILOT:
354                 return olderf (opnd1, opnd2);
355         case FILEQ:
356                 return equalf (opnd1, opnd2);
357         }
358         /* NOTREACHED */
359         return 1;
360 }
361
362 static int
363 filstat(nm, mode)
364         char *nm;
365         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
445 t_lex(s)
446         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 int
467 getn(s)
468         char *s;
469 {
470         char *p;
471         long r;
472
473         errno = 0;
474         r = strtol(s, &p, 10);
475
476         if (errno != 0)
477           fatalError("%s: out of range\n", s);
478
479         while (isspace(*p))
480           p++;
481         
482         if (*p)
483           fatalError("%s: bad number\n", s);
484
485         return (int) r;
486 }
487
488 static int
489 newerf (f1, f2)
490 char *f1, *f2;
491 {
492         struct stat b1, b2;
493
494         return (stat (f1, &b1) == 0 &&
495                 stat (f2, &b2) == 0 &&
496                 b1.st_mtime > b2.st_mtime);
497 }
498
499 static int
500 olderf (f1, f2)
501 char *f1, *f2;
502 {
503         struct stat b1, b2;
504
505         return (stat (f1, &b1) == 0 &&
506                 stat (f2, &b2) == 0 &&
507                 b1.st_mtime < b2.st_mtime);
508 }
509
510 static int
511 equalf (f1, f2)
512 char *f1, *f2;
513 {
514         struct stat b1, b2;
515
516         return (stat (f1, &b1) == 0 &&
517                 stat (f2, &b2) == 0 &&
518                 b1.st_dev == b2.st_dev &&
519                 b1.st_ino == b2.st_ino);
520 }
521
522 /* Do the same thing access(2) does, but use the effective uid and gid,
523    and don't make the mistake of telling root that any file is
524    executable. */
525 static int
526 test_eaccess (path, mode)
527 char *path;
528 int mode;
529 {
530         struct stat st;
531         unsigned int euid = geteuid();
532
533         if (stat (path, &st) < 0)
534                 return (-1);
535
536         if (euid == 0) {
537                 /* Root can read or write any file. */
538                 if (mode != X_OK)
539                 return (0);
540
541                 /* Root can execute any file that has any one of the execute
542                    bits set. */
543                 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
544                         return (0);
545         }
546
547         if (st.st_uid == euid)          /* owner */
548                 mode <<= 6;
549         else if (is_a_group_member (st.st_gid))
550                 mode <<= 3;
551
552         if (st.st_mode & mode)
553                 return (0);
554
555         return (-1);
556 }
557
558 static void
559 initialize_group_array ()
560 {
561         ngroups = getgroups(0, NULL);
562         if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
563                 fatalError("Out of space\n");
564
565         getgroups(ngroups, group_array);
566 }
567
568 /* Return non-zero if GID is one that we have in our groups list. */
569 static int
570 is_a_group_member (gid)
571 gid_t gid;
572 {
573         register int i;
574
575         /* Short-circuit if possible, maybe saving a call to getgroups(). */
576         if (gid == getgid() || gid == getegid())
577                 return (1);
578
579         if (ngroups == 0)
580                 initialize_group_array ();
581
582         /* Search through the list looking for GID. */
583         for (i = 0; i < ngroups; i++)
584                 if (gid == group_array[i])
585                         return (1);
586
587         return (0);
588 }