1 /* vi: set sw=4 ts=4: */
3 * test implementation for busybox
5 * Copyright (c) by a whole pile of folks:
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
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.
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.
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
30 * Original copyright notice states:
31 * "This program is in the Public Domain."
35 #include <sys/types.h>
43 /* test(1) accepts the following grammar:
44 oexpr ::= aexpr | aexpr "-o" oexpr ;
45 aexpr ::= nexpr | nexpr "-a" aexpr ;
46 nexpr ::= primary | "!" primary
47 primary ::= unary-operator operand
48 | operand binary-operator operand
52 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
53 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
55 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
57 operand ::= <any legal UNIX file name>
113 short op_num, op_type;
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},
131 {"-h", FILSYM, UNOP}, /* for backwards compat */
132 {"-O", FILUID, UNOP},
133 {"-G", FILGID, UNOP},
134 {"-L", FILSYM, UNOP},
135 {"-S", FILSOCK,UNOP},
137 {"!=", STRNE, BINOP},
140 {"-eq", INTEQ, BINOP},
141 {"-ne", INTNE, BINOP},
142 {"-ge", INTGE, BINOP},
143 {"-gt", INTGT, BINOP},
144 {"-le", INTLE, BINOP},
145 {"-lt", INTLT, BINOP},
146 {"-nt", FILNT, BINOP},
147 {"-ot", FILOT, BINOP},
148 {"-ef", FILEQ, BINOP},
150 {"-a", BAND, BBINOP},
152 {"(", LPAREN, PAREN},
153 {")", RPAREN, PAREN},
158 struct t_op const *t_wp_op;
159 static gid_t *group_array = NULL;
162 static enum token t_lex();
167 static int primary();
168 static int filstat();
173 static void syntax();
174 static int test_eaccess();
175 static int is_a_group_member();
176 static void initialize_group_array();
179 test_main(int argc, char** argv)
183 if (strcmp(argv[0], "[") == 0) {
184 if (strcmp(argv[--argc], "]"))
185 fatalError("missing ]");
188 if (strcmp(argv[1], "--help") == 0) {
189 usage("test EXPRESSION\n"
190 "or [ EXPRESSION ]\n\n"
191 "Checks file types and compares values returning an exit\n"
192 "code determined by the value of EXPRESSION.\n");
195 /* Implement special cases from POSIX.2, section 4.62.4 */
200 exit (*argv[1] == '\0');
202 if (argv[1][0] == '!' && argv[1][1] == '\0') {
203 exit (!(*argv[2] == '\0'));
207 if (argv[1][0] != '!' || argv[1][1] != '\0') {
209 t_wp_op && t_wp_op->op_type == BINOP) {
216 if (argv[1][0] == '!' && argv[1][1] == '\0') {
218 t_wp_op && t_wp_op->op_type == BINOP) {
220 exit (!(binop() == 0));
227 res = !oexpr(t_lex(*t_wp));
229 if (*t_wp != NULL && *++t_wp != NULL)
230 syntax(*t_wp, "unknown operand");
241 fatalError("%s: %s", op, msg);
243 fatalError("%s", msg);
253 if (t_lex(*++t_wp) == BOR)
254 return oexpr(t_lex(*++t_wp)) || res;
266 if (t_lex(*++t_wp) == BAND)
267 return aexpr(t_lex(*++t_wp)) && res;
274 enum token n; /* token */
277 return !nexpr(t_lex(*++t_wp));
288 syntax(NULL, "argument expected");
290 res = oexpr(t_lex(*++t_wp));
291 if (t_lex(*++t_wp) != RPAREN)
292 syntax(NULL, "closing paren expected");
295 if (t_wp_op && t_wp_op->op_type == UNOP) {
296 /* unary expression */
298 syntax(t_wp_op->op_text, "argument expected");
301 return strlen(*t_wp) == 0;
303 return strlen(*t_wp) != 0;
305 return isatty(getn(*t_wp));
307 return filstat(*t_wp, n);
311 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
315 return strlen(*t_wp) > 0;
321 const char *opnd1, *opnd2;
322 struct t_op const *op;
325 (void) t_lex(*++t_wp);
328 if ((opnd2 = *++t_wp) == (char *)0)
329 syntax(op->op_text, "argument expected");
331 switch (op->op_num) {
333 return strcmp(opnd1, opnd2) == 0;
335 return strcmp(opnd1, opnd2) != 0;
337 return strcmp(opnd1, opnd2) < 0;
339 return strcmp(opnd1, opnd2) > 0;
341 return getn(opnd1) == getn(opnd2);
343 return getn(opnd1) != getn(opnd2);
345 return getn(opnd1) >= getn(opnd2);
347 return getn(opnd1) > getn(opnd2);
349 return getn(opnd1) <= getn(opnd2);
351 return getn(opnd1) < getn(opnd2);
353 return newerf (opnd1, opnd2);
355 return olderf (opnd1, opnd2);
357 return equalf (opnd1, opnd2);
371 if (mode == FILSYM) {
373 if (lstat(nm, &s) == 0) {
381 if (stat(nm, &s) != 0)
386 return test_eaccess(nm, R_OK) == 0;
388 return test_eaccess(nm, W_OK) == 0;
390 return test_eaccess(nm, X_OK) == 0;
429 return s.st_size > 0L;
431 return s.st_uid == geteuid();
433 return s.st_gid == getegid();
439 return ((s.st_mode & S_IFMT) == i);
442 return ((s.st_mode & i) != 0);
449 struct t_op const *op = ops;
452 t_wp_op = (struct t_op *)0;
455 while (op->op_text) {
456 if (strcmp(s, op->op_text) == 0) {
462 t_wp_op = (struct t_op *)0;
466 /* atoi with error detection */
475 r = strtol(s, &p, 10);
478 fatalError("%s: out of range", s);
484 fatalError("%s: bad number", s);
495 return (stat (f1, &b1) == 0 &&
496 stat (f2, &b2) == 0 &&
497 b1.st_mtime > b2.st_mtime);
506 return (stat (f1, &b1) == 0 &&
507 stat (f2, &b2) == 0 &&
508 b1.st_mtime < b2.st_mtime);
517 return (stat (f1, &b1) == 0 &&
518 stat (f2, &b2) == 0 &&
519 b1.st_dev == b2.st_dev &&
520 b1.st_ino == b2.st_ino);
523 /* Do the same thing access(2) does, but use the effective uid and gid,
524 and don't make the mistake of telling root that any file is
527 test_eaccess (path, mode)
532 int euid = geteuid();
534 if (stat (path, &st) < 0)
538 /* Root can read or write any file. */
542 /* Root can execute any file that has any one of the execute
544 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
548 if (st.st_uid == euid) /* owner */
550 else if (is_a_group_member (st.st_gid))
553 if (st.st_mode & mode)
560 initialize_group_array ()
562 ngroups = getgroups(0, NULL);
563 if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
564 fatalError("Out of space");
566 getgroups(ngroups, group_array);
569 /* Return non-zero if GID is one that we have in our groups list. */
571 is_a_group_member (gid)
576 /* Short-circuit if possible, maybe saving a call to getgroups(). */
577 if (gid == getgid() || gid == getegid())
581 initialize_group_array ();
583 /* Search through the list looking for GID. */
584 for (i = 0; i < ngroups; i++)
585 if (gid == group_array[i])