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>
41 #define BB_DECLARE_EXTERN
45 /* test(1) accepts the following grammar:
46 oexpr ::= aexpr | aexpr "-o" oexpr ;
47 aexpr ::= nexpr | nexpr "-a" aexpr ;
48 nexpr ::= primary | "!" primary
49 primary ::= unary-operator operand
50 | operand binary-operator operand
54 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
55 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
57 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
59 operand ::= <any legal UNIX file name>
115 short op_num, op_type;
120 {"-e", FILEXIST,UNOP},
121 {"-f", FILREG, UNOP},
122 {"-d", FILDIR, UNOP},
123 {"-c", FILCDEV,UNOP},
124 {"-b", FILBDEV,UNOP},
125 {"-p", FILFIFO,UNOP},
126 {"-u", FILSUID,UNOP},
127 {"-g", FILSGID,UNOP},
128 {"-k", FILSTCK,UNOP},
133 {"-h", FILSYM, UNOP}, /* for backwards compat */
134 {"-O", FILUID, UNOP},
135 {"-G", FILGID, UNOP},
136 {"-L", FILSYM, UNOP},
137 {"-S", FILSOCK,UNOP},
139 {"!=", STRNE, BINOP},
142 {"-eq", INTEQ, BINOP},
143 {"-ne", INTNE, BINOP},
144 {"-ge", INTGE, BINOP},
145 {"-gt", INTGT, BINOP},
146 {"-le", INTLE, BINOP},
147 {"-lt", INTLT, BINOP},
148 {"-nt", FILNT, BINOP},
149 {"-ot", FILOT, BINOP},
150 {"-ef", FILEQ, BINOP},
152 {"-a", BAND, BBINOP},
154 {"(", LPAREN, PAREN},
155 {")", RPAREN, PAREN},
160 struct t_op const *t_wp_op;
161 static gid_t *group_array = NULL;
164 static enum token t_lex();
169 static int primary();
170 static int filstat();
175 static void syntax();
176 static int test_eaccess();
177 static int is_a_group_member();
178 static void initialize_group_array();
181 test_main(int argc, char** argv)
185 if (strcmp(applet_name, "[") == 0) {
186 if (strcmp(argv[--argc], "]"))
187 error_msg_and_die("missing ]\n");
190 /* Implement special cases from POSIX.2, section 4.62.4 */
195 exit (*argv[1] == '\0');
197 if (argv[1][0] == '!' && argv[1][1] == '\0') {
198 exit (!(*argv[2] == '\0'));
202 if (argv[1][0] != '!' || argv[1][1] != '\0') {
204 t_wp_op && t_wp_op->op_type == BINOP) {
211 if (argv[1][0] == '!' && argv[1][1] == '\0') {
213 t_wp_op && t_wp_op->op_type == BINOP) {
215 exit (!(binop() == 0));
222 res = !oexpr(t_lex(*t_wp));
224 if (*t_wp != NULL && *++t_wp != NULL)
225 syntax(*t_wp, "unknown operand");
236 error_msg_and_die("%s: %s\n", op, msg);
238 error_msg_and_die("%s\n", msg);
248 if (t_lex(*++t_wp) == BOR)
249 return oexpr(t_lex(*++t_wp)) || res;
261 if (t_lex(*++t_wp) == BAND)
262 return aexpr(t_lex(*++t_wp)) && res;
269 enum token n; /* token */
272 return !nexpr(t_lex(*++t_wp));
283 syntax(NULL, "argument expected");
285 res = oexpr(t_lex(*++t_wp));
286 if (t_lex(*++t_wp) != RPAREN)
287 syntax(NULL, "closing paren expected");
290 if (t_wp_op && t_wp_op->op_type == UNOP) {
291 /* unary expression */
293 syntax(t_wp_op->op_text, "argument expected");
296 return strlen(*t_wp) == 0;
298 return strlen(*t_wp) != 0;
300 return isatty(getn(*t_wp));
302 return filstat(*t_wp, n);
306 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
310 return strlen(*t_wp) > 0;
316 const char *opnd1, *opnd2;
317 struct t_op const *op;
320 (void) t_lex(*++t_wp);
323 if ((opnd2 = *++t_wp) == (char *)0)
324 syntax(op->op_text, "argument expected");
326 switch (op->op_num) {
328 return strcmp(opnd1, opnd2) == 0;
330 return strcmp(opnd1, opnd2) != 0;
332 return strcmp(opnd1, opnd2) < 0;
334 return strcmp(opnd1, opnd2) > 0;
336 return getn(opnd1) == getn(opnd2);
338 return getn(opnd1) != getn(opnd2);
340 return getn(opnd1) >= getn(opnd2);
342 return getn(opnd1) > getn(opnd2);
344 return getn(opnd1) <= getn(opnd2);
346 return getn(opnd1) < getn(opnd2);
348 return newerf (opnd1, opnd2);
350 return olderf (opnd1, opnd2);
352 return equalf (opnd1, opnd2);
366 if (mode == FILSYM) {
368 if (lstat(nm, &s) == 0) {
376 if (stat(nm, &s) != 0)
381 return test_eaccess(nm, R_OK) == 0;
383 return test_eaccess(nm, W_OK) == 0;
385 return test_eaccess(nm, X_OK) == 0;
424 return s.st_size > 0L;
426 return s.st_uid == geteuid();
428 return s.st_gid == getegid();
434 return ((s.st_mode & S_IFMT) == i);
437 return ((s.st_mode & i) != 0);
444 struct t_op const *op = ops;
447 t_wp_op = (struct t_op *)0;
450 while (op->op_text) {
451 if (strcmp(s, op->op_text) == 0) {
457 t_wp_op = (struct t_op *)0;
461 /* atoi with error detection */
470 r = strtol(s, &p, 10);
473 error_msg_and_die("%s: out of range\n", s);
479 error_msg_and_die("%s: bad number\n", s);
490 return (stat (f1, &b1) == 0 &&
491 stat (f2, &b2) == 0 &&
492 b1.st_mtime > b2.st_mtime);
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_dev == b2.st_dev &&
515 b1.st_ino == b2.st_ino);
518 /* Do the same thing access(2) does, but use the effective uid and gid,
519 and don't make the mistake of telling root that any file is
522 test_eaccess (path, mode)
527 unsigned int euid = geteuid();
529 if (stat (path, &st) < 0)
533 /* Root can read or write any file. */
537 /* Root can execute any file that has any one of the execute
539 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
543 if (st.st_uid == euid) /* owner */
545 else if (is_a_group_member (st.st_gid))
548 if (st.st_mode & mode)
555 initialize_group_array ()
557 ngroups = getgroups(0, NULL);
558 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
559 getgroups(ngroups, group_array);
562 /* Return non-zero if GID is one that we have in our groups list. */
564 is_a_group_member (gid)
569 /* Short-circuit if possible, maybe saving a call to getgroups(). */
570 if (gid == getgid() || gid == getegid())
574 initialize_group_array ();
576 /* Search through the list looking for GID. */
577 for (i = 0; i < ngroups; i++)
578 if (gid == group_array[i])