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>
42 #define BB_DECLARE_EXTERN
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
55 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
56 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
58 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
60 operand ::= <any legal UNIX file name>
116 short op_num, op_type;
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},
134 {"-h", FILSYM, UNOP}, /* for backwards compat */
135 {"-O", FILUID, UNOP},
136 {"-G", FILGID, UNOP},
137 {"-L", FILSYM, UNOP},
138 {"-S", FILSOCK,UNOP},
140 {"!=", STRNE, 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},
153 {"-a", BAND, BBINOP},
155 {"(", LPAREN, PAREN},
156 {")", RPAREN, PAREN},
161 struct t_op const *t_wp_op;
162 static gid_t *group_array = NULL;
165 static enum token t_lex();
170 static int primary();
171 static int filstat();
176 static void syntax();
177 static int test_eaccess();
178 static int is_a_group_member();
179 static void initialize_group_array();
182 test_main(int argc, char** argv)
186 if (strcmp(argv[0], "[") == 0) {
187 if (strcmp(argv[--argc], "]"))
188 fatalError("missing ]");
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"
201 /* Implement special cases from POSIX.2, section 4.62.4 */
206 exit (*argv[1] == '\0');
208 if (argv[1][0] == '!' && argv[1][1] == '\0') {
209 exit (!(*argv[2] == '\0'));
213 if (argv[1][0] != '!' || argv[1][1] != '\0') {
215 t_wp_op && t_wp_op->op_type == BINOP) {
222 if (argv[1][0] == '!' && argv[1][1] == '\0') {
224 t_wp_op && t_wp_op->op_type == BINOP) {
226 exit (!(binop() == 0));
233 res = !oexpr(t_lex(*t_wp));
235 if (*t_wp != NULL && *++t_wp != NULL)
236 syntax(*t_wp, "unknown operand");
247 fatalError("%s: %s", op, msg);
249 fatalError("%s", msg);
259 if (t_lex(*++t_wp) == BOR)
260 return oexpr(t_lex(*++t_wp)) || res;
272 if (t_lex(*++t_wp) == BAND)
273 return aexpr(t_lex(*++t_wp)) && res;
280 enum token n; /* token */
283 return !nexpr(t_lex(*++t_wp));
294 syntax(NULL, "argument expected");
296 res = oexpr(t_lex(*++t_wp));
297 if (t_lex(*++t_wp) != RPAREN)
298 syntax(NULL, "closing paren expected");
301 if (t_wp_op && t_wp_op->op_type == UNOP) {
302 /* unary expression */
304 syntax(t_wp_op->op_text, "argument expected");
307 return strlen(*t_wp) == 0;
309 return strlen(*t_wp) != 0;
311 return isatty(getn(*t_wp));
313 return filstat(*t_wp, n);
317 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
321 return strlen(*t_wp) > 0;
327 const char *opnd1, *opnd2;
328 struct t_op const *op;
331 (void) t_lex(*++t_wp);
334 if ((opnd2 = *++t_wp) == (char *)0)
335 syntax(op->op_text, "argument expected");
337 switch (op->op_num) {
339 return strcmp(opnd1, opnd2) == 0;
341 return strcmp(opnd1, opnd2) != 0;
343 return strcmp(opnd1, opnd2) < 0;
345 return strcmp(opnd1, opnd2) > 0;
347 return getn(opnd1) == getn(opnd2);
349 return getn(opnd1) != getn(opnd2);
351 return getn(opnd1) >= getn(opnd2);
353 return getn(opnd1) > getn(opnd2);
355 return getn(opnd1) <= getn(opnd2);
357 return getn(opnd1) < getn(opnd2);
359 return newerf (opnd1, opnd2);
361 return olderf (opnd1, opnd2);
363 return equalf (opnd1, opnd2);
377 if (mode == FILSYM) {
379 if (lstat(nm, &s) == 0) {
387 if (stat(nm, &s) != 0)
392 return test_eaccess(nm, R_OK) == 0;
394 return test_eaccess(nm, W_OK) == 0;
396 return test_eaccess(nm, X_OK) == 0;
435 return s.st_size > 0L;
437 return s.st_uid == geteuid();
439 return s.st_gid == getegid();
445 return ((s.st_mode & S_IFMT) == i);
448 return ((s.st_mode & i) != 0);
455 struct t_op const *op = ops;
458 t_wp_op = (struct t_op *)0;
461 while (op->op_text) {
462 if (strcmp(s, op->op_text) == 0) {
468 t_wp_op = (struct t_op *)0;
472 /* atoi with error detection */
481 r = strtol(s, &p, 10);
484 fatalError("%s: out of range", s);
490 fatalError("%s: bad number", s);
501 return (stat (f1, &b1) == 0 &&
502 stat (f2, &b2) == 0 &&
503 b1.st_mtime > b2.st_mtime);
512 return (stat (f1, &b1) == 0 &&
513 stat (f2, &b2) == 0 &&
514 b1.st_mtime < b2.st_mtime);
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);
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
533 test_eaccess (path, mode)
538 int euid = geteuid();
540 if (stat (path, &st) < 0)
544 /* Root can read or write any file. */
548 /* Root can execute any file that has any one of the execute
550 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
554 if (st.st_uid == euid) /* owner */
556 else if (is_a_group_member (st.st_gid))
559 if (st.st_mode & mode)
566 initialize_group_array ()
568 ngroups = getgroups(0, NULL);
569 if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
570 fatalError("Out of space");
572 getgroups(ngroups, group_array);
575 /* Return non-zero if GID is one that we have in our groups list. */
577 is_a_group_member (gid)
582 /* Short-circuit if possible, maybe saving a call to getgroups(). */
583 if (gid == getgid() || gid == getegid())
587 initialize_group_array ();
589 /* Search through the list looking for GID. */
590 for (i = 0; i < ngroups; i++)
591 if (gid == group_array[i])