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();
181 const char test_usage[] =
183 "or [ EXPRESSION ]\n"
184 #ifndef BB_FEATURE_TRIVIAL_HELP
185 "\nChecks file types and compares values returning an exit\n"
186 "code determined by the value of EXPRESSION.\n"
191 test_main(int argc, char** argv)
195 if (strcmp(applet_name, "[") == 0) {
196 if (strcmp(argv[--argc], "]"))
197 fatalError("missing ]\n");
200 if (strcmp(argv[1], dash_dash_help) == 0)
203 /* Implement special cases from POSIX.2, section 4.62.4 */
208 exit (*argv[1] == '\0');
210 if (argv[1][0] == '!' && argv[1][1] == '\0') {
211 exit (!(*argv[2] == '\0'));
215 if (argv[1][0] != '!' || argv[1][1] != '\0') {
217 t_wp_op && t_wp_op->op_type == BINOP) {
224 if (argv[1][0] == '!' && argv[1][1] == '\0') {
226 t_wp_op && t_wp_op->op_type == BINOP) {
228 exit (!(binop() == 0));
235 res = !oexpr(t_lex(*t_wp));
237 if (*t_wp != NULL && *++t_wp != NULL)
238 syntax(*t_wp, "unknown operand");
249 fatalError("%s: %s\n", op, msg);
251 fatalError("%s\n", msg);
261 if (t_lex(*++t_wp) == BOR)
262 return oexpr(t_lex(*++t_wp)) || res;
274 if (t_lex(*++t_wp) == BAND)
275 return aexpr(t_lex(*++t_wp)) && res;
282 enum token n; /* token */
285 return !nexpr(t_lex(*++t_wp));
296 syntax(NULL, "argument expected");
298 res = oexpr(t_lex(*++t_wp));
299 if (t_lex(*++t_wp) != RPAREN)
300 syntax(NULL, "closing paren expected");
303 if (t_wp_op && t_wp_op->op_type == UNOP) {
304 /* unary expression */
306 syntax(t_wp_op->op_text, "argument expected");
309 return strlen(*t_wp) == 0;
311 return strlen(*t_wp) != 0;
313 return isatty(getn(*t_wp));
315 return filstat(*t_wp, n);
319 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
323 return strlen(*t_wp) > 0;
329 const char *opnd1, *opnd2;
330 struct t_op const *op;
333 (void) t_lex(*++t_wp);
336 if ((opnd2 = *++t_wp) == (char *)0)
337 syntax(op->op_text, "argument expected");
339 switch (op->op_num) {
341 return strcmp(opnd1, opnd2) == 0;
343 return strcmp(opnd1, opnd2) != 0;
345 return strcmp(opnd1, opnd2) < 0;
347 return strcmp(opnd1, opnd2) > 0;
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 getn(opnd1) < getn(opnd2);
361 return newerf (opnd1, opnd2);
363 return olderf (opnd1, opnd2);
365 return equalf (opnd1, opnd2);
379 if (mode == FILSYM) {
381 if (lstat(nm, &s) == 0) {
389 if (stat(nm, &s) != 0)
394 return test_eaccess(nm, R_OK) == 0;
396 return test_eaccess(nm, W_OK) == 0;
398 return test_eaccess(nm, X_OK) == 0;
437 return s.st_size > 0L;
439 return s.st_uid == geteuid();
441 return s.st_gid == getegid();
447 return ((s.st_mode & S_IFMT) == i);
450 return ((s.st_mode & i) != 0);
457 struct t_op const *op = ops;
460 t_wp_op = (struct t_op *)0;
463 while (op->op_text) {
464 if (strcmp(s, op->op_text) == 0) {
470 t_wp_op = (struct t_op *)0;
474 /* atoi with error detection */
483 r = strtol(s, &p, 10);
486 fatalError("%s: out of range\n", s);
492 fatalError("%s: bad number\n", s);
503 return (stat (f1, &b1) == 0 &&
504 stat (f2, &b2) == 0 &&
505 b1.st_mtime > b2.st_mtime);
514 return (stat (f1, &b1) == 0 &&
515 stat (f2, &b2) == 0 &&
516 b1.st_mtime < b2.st_mtime);
525 return (stat (f1, &b1) == 0 &&
526 stat (f2, &b2) == 0 &&
527 b1.st_dev == b2.st_dev &&
528 b1.st_ino == b2.st_ino);
531 /* Do the same thing access(2) does, but use the effective uid and gid,
532 and don't make the mistake of telling root that any file is
535 test_eaccess (path, mode)
540 unsigned int euid = geteuid();
542 if (stat (path, &st) < 0)
546 /* Root can read or write any file. */
550 /* Root can execute any file that has any one of the execute
552 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
556 if (st.st_uid == euid) /* owner */
558 else if (is_a_group_member (st.st_gid))
561 if (st.st_mode & mode)
568 initialize_group_array ()
570 ngroups = getgroups(0, NULL);
571 if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
572 fatalError("Out of space\n");
574 getgroups(ngroups, group_array);
577 /* Return non-zero if GID is one that we have in our groups list. */
579 is_a_group_member (gid)
584 /* Short-circuit if possible, maybe saving a call to getgroups(). */
585 if (gid == getgid() || gid == getegid())
589 initialize_group_array ();
591 /* Search through the list looking for GID. */
592 for (i = 0; i < ngroups; i++)
593 if (gid == group_array[i])