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."
34 #include <sys/types.h>
42 /* test(1) accepts the following grammar:
43 oexpr ::= aexpr | aexpr "-o" oexpr ;
44 aexpr ::= nexpr | nexpr "-a" aexpr ;
45 nexpr ::= primary | "!" primary
46 primary ::= unary-operator operand
47 | operand binary-operator operand
51 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
52 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
54 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
56 operand ::= <any legal UNIX file name>
110 static const struct t_op {
112 short op_num, op_type;
117 {"-e", FILEXIST,UNOP},
118 {"-f", FILREG, UNOP},
119 {"-d", FILDIR, UNOP},
120 {"-c", FILCDEV,UNOP},
121 {"-b", FILBDEV,UNOP},
122 {"-p", FILFIFO,UNOP},
123 {"-u", FILSUID,UNOP},
124 {"-g", FILSGID,UNOP},
125 {"-k", FILSTCK,UNOP},
130 {"-h", FILSYM, UNOP}, /* for backwards compat */
131 {"-O", FILUID, UNOP},
132 {"-G", FILGID, UNOP},
133 {"-L", FILSYM, UNOP},
134 {"-S", FILSOCK,UNOP},
136 {"!=", STRNE, BINOP},
139 {"-eq", INTEQ, BINOP},
140 {"-ne", INTNE, BINOP},
141 {"-ge", INTGE, BINOP},
142 {"-gt", INTGT, BINOP},
143 {"-le", INTLE, BINOP},
144 {"-lt", INTLT, BINOP},
145 {"-nt", FILNT, BINOP},
146 {"-ot", FILOT, BINOP},
147 {"-ef", FILEQ, BINOP},
149 {"-a", BAND, BBINOP},
151 {"(", LPAREN, PAREN},
152 {")", RPAREN, PAREN},
157 static struct t_op const *t_wp_op;
158 static gid_t *group_array = NULL;
161 static enum token t_lex();
166 static int primary();
167 static int filstat();
172 static void syntax();
173 static int test_eaccess();
174 static int is_a_group_member();
175 static void initialize_group_array();
178 test_main(int argc, char** argv)
182 if (strcmp(applet_name, "[") == 0) {
183 if (strcmp(argv[--argc], "]"))
184 error_msg_and_die("missing ]");
187 /* Implement special cases from POSIX.2, section 4.62.4 */
192 exit (*argv[1] == '\0');
194 if (argv[1][0] == '!' && argv[1][1] == '\0') {
195 exit (!(*argv[2] == '\0'));
199 if (argv[1][0] != '!' || argv[1][1] != '\0') {
201 t_wp_op && t_wp_op->op_type == BINOP) {
208 if (argv[1][0] == '!' && argv[1][1] == '\0') {
210 t_wp_op && t_wp_op->op_type == BINOP) {
212 exit (!(binop() == 0));
219 res = !oexpr(t_lex(*t_wp));
221 if (*t_wp != NULL && *++t_wp != NULL)
222 syntax(*t_wp, "unknown operand");
233 error_msg_and_die("%s: %s", op, msg);
235 error_msg_and_die("%s", msg);
245 if (t_lex(*++t_wp) == BOR)
246 return oexpr(t_lex(*++t_wp)) || res;
258 if (t_lex(*++t_wp) == BAND)
259 return aexpr(t_lex(*++t_wp)) && res;
266 enum token n; /* token */
269 return !nexpr(t_lex(*++t_wp));
280 syntax(NULL, "argument expected");
282 res = oexpr(t_lex(*++t_wp));
283 if (t_lex(*++t_wp) != RPAREN)
284 syntax(NULL, "closing paren expected");
287 if (t_wp_op && t_wp_op->op_type == UNOP) {
288 /* unary expression */
290 syntax(t_wp_op->op_text, "argument expected");
293 return strlen(*t_wp) == 0;
295 return strlen(*t_wp) != 0;
297 return isatty(getn(*t_wp));
299 return filstat(*t_wp, n);
303 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
307 return strlen(*t_wp) > 0;
313 const char *opnd1, *opnd2;
314 struct t_op const *op;
317 (void) t_lex(*++t_wp);
320 if ((opnd2 = *++t_wp) == (char *)0)
321 syntax(op->op_text, "argument expected");
323 switch (op->op_num) {
325 return strcmp(opnd1, opnd2) == 0;
327 return strcmp(opnd1, opnd2) != 0;
329 return strcmp(opnd1, opnd2) < 0;
331 return strcmp(opnd1, opnd2) > 0;
333 return getn(opnd1) == getn(opnd2);
335 return getn(opnd1) != getn(opnd2);
337 return getn(opnd1) >= getn(opnd2);
339 return getn(opnd1) > getn(opnd2);
341 return getn(opnd1) <= getn(opnd2);
343 return getn(opnd1) < getn(opnd2);
345 return newerf (opnd1, opnd2);
347 return olderf (opnd1, opnd2);
349 return equalf (opnd1, opnd2);
363 if (mode == FILSYM) {
365 if (lstat(nm, &s) == 0) {
373 if (stat(nm, &s) != 0)
378 return test_eaccess(nm, R_OK) == 0;
380 return test_eaccess(nm, W_OK) == 0;
382 return test_eaccess(nm, X_OK) == 0;
421 return s.st_size > 0L;
423 return s.st_uid == geteuid();
425 return s.st_gid == getegid();
431 return ((s.st_mode & S_IFMT) == i);
434 return ((s.st_mode & i) != 0);
441 struct t_op const *op = ops;
444 t_wp_op = (struct t_op *)0;
447 while (op->op_text) {
448 if (strcmp(s, op->op_text) == 0) {
454 t_wp_op = (struct t_op *)0;
458 /* atoi with error detection */
467 r = strtol(s, &p, 10);
470 error_msg_and_die("%s: out of range", s);
476 error_msg_and_die("%s: bad number", s);
487 return (stat (f1, &b1) == 0 &&
488 stat (f2, &b2) == 0 &&
489 b1.st_mtime > b2.st_mtime);
498 return (stat (f1, &b1) == 0 &&
499 stat (f2, &b2) == 0 &&
500 b1.st_mtime < b2.st_mtime);
509 return (stat (f1, &b1) == 0 &&
510 stat (f2, &b2) == 0 &&
511 b1.st_dev == b2.st_dev &&
512 b1.st_ino == b2.st_ino);
515 /* Do the same thing access(2) does, but use the effective uid and gid,
516 and don't make the mistake of telling root that any file is
519 test_eaccess (path, mode)
524 unsigned int euid = geteuid();
526 if (stat (path, &st) < 0)
530 /* Root can read or write any file. */
534 /* Root can execute any file that has any one of the execute
536 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
540 if (st.st_uid == euid) /* owner */
542 else if (is_a_group_member (st.st_gid))
545 if (st.st_mode & mode)
552 initialize_group_array ()
554 ngroups = getgroups(0, NULL);
555 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
556 getgroups(ngroups, group_array);
559 /* Return non-zero if GID is one that we have in our groups list. */
561 is_a_group_member (gid)
566 /* Short-circuit if possible, maybe saving a call to getgroups(). */
567 if (gid == getgid() || gid == getegid())
571 initialize_group_array ();
573 /* Search through the list looking for GID. */
574 for (i = 0; i < ngroups; i++)
575 if (gid == group_array[i])