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(applet_name, "[") == 0) {
187 if (strcmp(argv[--argc], "]"))
188 fatalError("missing ]\n");
191 if (strcmp(argv[1], dash_dash_help) == 0)
194 /* Implement special cases from POSIX.2, section 4.62.4 */
199 exit (*argv[1] == '\0');
201 if (argv[1][0] == '!' && argv[1][1] == '\0') {
202 exit (!(*argv[2] == '\0'));
206 if (argv[1][0] != '!' || argv[1][1] != '\0') {
208 t_wp_op && t_wp_op->op_type == BINOP) {
215 if (argv[1][0] == '!' && argv[1][1] == '\0') {
217 t_wp_op && t_wp_op->op_type == BINOP) {
219 exit (!(binop() == 0));
226 res = !oexpr(t_lex(*t_wp));
228 if (*t_wp != NULL && *++t_wp != NULL)
229 syntax(*t_wp, "unknown operand");
240 fatalError("%s: %s\n", op, msg);
242 fatalError("%s\n", msg);
252 if (t_lex(*++t_wp) == BOR)
253 return oexpr(t_lex(*++t_wp)) || res;
265 if (t_lex(*++t_wp) == BAND)
266 return aexpr(t_lex(*++t_wp)) && res;
273 enum token n; /* token */
276 return !nexpr(t_lex(*++t_wp));
287 syntax(NULL, "argument expected");
289 res = oexpr(t_lex(*++t_wp));
290 if (t_lex(*++t_wp) != RPAREN)
291 syntax(NULL, "closing paren expected");
294 if (t_wp_op && t_wp_op->op_type == UNOP) {
295 /* unary expression */
297 syntax(t_wp_op->op_text, "argument expected");
300 return strlen(*t_wp) == 0;
302 return strlen(*t_wp) != 0;
304 return isatty(getn(*t_wp));
306 return filstat(*t_wp, n);
310 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
314 return strlen(*t_wp) > 0;
320 const char *opnd1, *opnd2;
321 struct t_op const *op;
324 (void) t_lex(*++t_wp);
327 if ((opnd2 = *++t_wp) == (char *)0)
328 syntax(op->op_text, "argument expected");
330 switch (op->op_num) {
332 return strcmp(opnd1, opnd2) == 0;
334 return strcmp(opnd1, opnd2) != 0;
336 return strcmp(opnd1, opnd2) < 0;
338 return strcmp(opnd1, opnd2) > 0;
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 getn(opnd1) <= getn(opnd2);
350 return getn(opnd1) < getn(opnd2);
352 return newerf (opnd1, opnd2);
354 return olderf (opnd1, opnd2);
356 return equalf (opnd1, opnd2);
370 if (mode == FILSYM) {
372 if (lstat(nm, &s) == 0) {
380 if (stat(nm, &s) != 0)
385 return test_eaccess(nm, R_OK) == 0;
387 return test_eaccess(nm, W_OK) == 0;
389 return test_eaccess(nm, X_OK) == 0;
428 return s.st_size > 0L;
430 return s.st_uid == geteuid();
432 return s.st_gid == getegid();
438 return ((s.st_mode & S_IFMT) == i);
441 return ((s.st_mode & i) != 0);
448 struct t_op const *op = ops;
451 t_wp_op = (struct t_op *)0;
454 while (op->op_text) {
455 if (strcmp(s, op->op_text) == 0) {
461 t_wp_op = (struct t_op *)0;
465 /* atoi with error detection */
474 r = strtol(s, &p, 10);
477 fatalError("%s: out of range\n", s);
483 fatalError("%s: bad number\n", s);
494 return (stat (f1, &b1) == 0 &&
495 stat (f2, &b2) == 0 &&
496 b1.st_mtime > b2.st_mtime);
505 return (stat (f1, &b1) == 0 &&
506 stat (f2, &b2) == 0 &&
507 b1.st_mtime < b2.st_mtime);
516 return (stat (f1, &b1) == 0 &&
517 stat (f2, &b2) == 0 &&
518 b1.st_dev == b2.st_dev &&
519 b1.st_ino == b2.st_ino);
522 /* Do the same thing access(2) does, but use the effective uid and gid,
523 and don't make the mistake of telling root that any file is
526 test_eaccess (path, mode)
531 unsigned int euid = geteuid();
533 if (stat (path, &st) < 0)
537 /* Root can read or write any file. */
541 /* Root can execute any file that has any one of the execute
543 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
547 if (st.st_uid == euid) /* owner */
549 else if (is_a_group_member (st.st_gid))
552 if (st.st_mode & mode)
559 initialize_group_array ()
561 ngroups = getgroups(0, NULL);
562 if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
563 fatalError("Out of space\n");
565 getgroups(ngroups, group_array);
568 /* Return non-zero if GID is one that we have in our groups list. */
570 is_a_group_member (gid)
575 /* Short-circuit if possible, maybe saving a call to getgroups(). */
576 if (gid == getgid() || gid == getegid())
580 initialize_group_array ();
582 /* Search through the list looking for GID. */
583 for (i = 0; i < ngroups; i++)
584 if (gid == group_array[i])