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 <andersen@codepoet.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;
115 "-r", FILRD, UNOP}, {
116 "-w", FILWR, UNOP}, {
117 "-x", FILEX, UNOP}, {
118 "-e", FILEXIST, UNOP}, {
119 "-f", FILREG, UNOP}, {
120 "-d", FILDIR, UNOP}, {
121 "-c", FILCDEV, UNOP}, {
122 "-b", FILBDEV, UNOP}, {
123 "-p", FILFIFO, UNOP}, {
124 "-u", FILSUID, UNOP}, {
125 "-g", FILSGID, UNOP}, {
126 "-k", FILSTCK, UNOP}, {
127 "-s", FILGZ, UNOP}, {
128 "-t", FILTT, UNOP}, {
129 "-z", STREZ, UNOP}, {
130 "-n", STRNZ, UNOP}, {
131 "-h", FILSYM, UNOP}, /* for backwards compat */
133 "-O", FILUID, UNOP}, {
134 "-G", FILGID, UNOP}, {
135 "-L", FILSYM, UNOP}, {
136 "-S", FILSOCK, UNOP}, {
137 "=", STREQ, BINOP}, {
138 "==", STREQ, BINOP}, {
139 "!=", STRNE, BINOP}, {
140 "<", STRLT, BINOP}, {
141 ">", STRGT, 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}, {
153 "-o", BOR, BBINOP}, {
154 "(", LPAREN, PAREN}, {
155 ")", RPAREN, PAREN}, {
159 #ifdef CONFIG_FEATURE_TEST_64
160 typedef int64_t arith_t;
166 static struct t_op const *t_wp_op;
167 static gid_t *group_array = NULL;
170 static enum token t_lex(char *s);
171 static arith_t oexpr(enum token n);
172 static arith_t aexpr(enum token n);
173 static arith_t nexpr(enum token n);
174 static int binop(void);
175 static arith_t primary(enum token n);
176 static int filstat(char *nm, enum token mode);
177 static arith_t getn(const char *s);
178 static int newerf(const char *f1, const char *f2);
179 static int olderf(const char *f1, const char *f2);
180 static int equalf(const char *f1, const char *f2);
181 static void syntax(const char *op, const char *msg);
182 static int test_eaccess(char *path, int mode);
183 static int is_a_group_member(gid_t gid);
184 static void initialize_group_array(void);
186 int test_main(int argc, char **argv)
190 if (strcmp(bb_applet_name, "[") == 0) {
191 if (strcmp(argv[--argc], "]"))
192 bb_error_msg_and_die("missing ]");
195 if (strcmp(bb_applet_name, "[[") == 0) {
196 if (strcmp(argv[--argc], "]]"))
197 bb_error_msg_and_die("missing ]]");
200 /* Implement special cases from POSIX.2, section 4.62.4 */
205 exit(*argv[1] == '\0');
207 if (argv[1][0] == '!' && argv[1][1] == '\0') {
208 exit(!(*argv[2] == '\0'));
212 if (argv[1][0] != '!' || argv[1][1] != '\0') {
213 if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) {
220 if (argv[1][0] == '!' && argv[1][1] == '\0') {
221 if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) {
223 exit(!(binop() == 0));
230 res = !oexpr(t_lex(*t_wp));
232 if (*t_wp != NULL && *++t_wp != NULL)
233 syntax(*t_wp, "unknown operand");
238 static void syntax(const char *op, const char *msg)
241 bb_error_msg_and_die("%s: %s", op, msg);
243 bb_error_msg_and_die("%s", msg);
247 static arith_t oexpr(enum token n)
252 if (t_lex(*++t_wp) == BOR) {
253 return oexpr(t_lex(*++t_wp)) || res;
259 static arith_t aexpr(enum token n)
264 if (t_lex(*++t_wp) == BAND)
265 return aexpr(t_lex(*++t_wp)) && res;
270 static arith_t nexpr(enum token n)
273 return !nexpr(t_lex(*++t_wp));
277 static arith_t primary(enum token n)
282 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;
313 static int binop(void)
315 const char *opnd1, *opnd2;
316 struct t_op const *op;
319 (void) t_lex(*++t_wp);
322 if ((opnd2 = *++t_wp) == (char *) 0)
323 syntax(op->op_text, "argument expected");
325 switch (op->op_num) {
327 return strcmp(opnd1, opnd2) == 0;
329 return strcmp(opnd1, opnd2) != 0;
331 return strcmp(opnd1, opnd2) < 0;
333 return strcmp(opnd1, opnd2) > 0;
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 getn(opnd1) < getn(opnd2);
347 return newerf(opnd1, opnd2);
349 return olderf(opnd1, opnd2);
351 return equalf(opnd1, opnd2);
357 static int filstat(char *nm, enum token mode)
362 if (mode == FILSYM) {
364 if (lstat(nm, &s) == 0) {
372 if (stat(nm, &s) != 0)
377 return test_eaccess(nm, R_OK) == 0;
379 return test_eaccess(nm, W_OK) == 0;
381 return test_eaccess(nm, X_OK) == 0;
420 return s.st_size > 0L;
422 return s.st_uid == geteuid();
424 return s.st_gid == getegid();
430 return ((s.st_mode & S_IFMT) == i);
433 return ((s.st_mode & i) != 0);
436 static enum token t_lex(char *s)
438 struct t_op const *op = ops;
441 t_wp_op = (struct t_op *) 0;
444 while (op->op_text) {
445 if (strcmp(s, op->op_text) == 0) {
451 t_wp_op = (struct t_op *) 0;
455 /* atoi with error detection */
456 static arith_t getn(const char *s)
459 #ifdef CONFIG_FEATURE_TEST_64
466 #ifdef CONFIG_FEATURE_TEST_64
467 r = strtoll(s, &p, 10);
469 r = strtol(s, &p, 10);
473 bb_error_msg_and_die("%s: out of range", s);
475 /* p = bb_skip_whitespace(p); avoid const warning */
476 if (*(bb_skip_whitespace(p)))
477 bb_error_msg_and_die("%s: bad number", s);
482 static int newerf(const char *f1, const char *f2)
486 return (stat(f1, &b1) == 0 &&
487 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
490 static int olderf(const char *f1, const char *f2)
494 return (stat(f1, &b1) == 0 &&
495 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
498 static int equalf(const char *f1, const char *f2)
502 return (stat(f1, &b1) == 0 &&
503 stat(f2, &b2) == 0 &&
504 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
507 /* Do the same thing access(2) does, but use the effective uid and gid,
508 and don't make the mistake of telling root that any file is
510 static int test_eaccess(char *path, int mode)
513 unsigned int euid = geteuid();
515 if (stat(path, &st) < 0)
519 /* Root can read or write any file. */
523 /* Root can execute any file that has any one of the execute
525 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
529 if (st.st_uid == euid) /* owner */
531 else if (is_a_group_member(st.st_gid))
534 if (st.st_mode & mode)
540 static void initialize_group_array(void)
542 ngroups = getgroups(0, NULL);
543 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
544 getgroups(ngroups, group_array);
547 /* Return non-zero if GID is one that we have in our groups list. */
548 static int is_a_group_member(gid_t gid)
552 /* Short-circuit if possible, maybe saving a call to getgroups(). */
553 if (gid == getgid() || gid == getegid())
557 initialize_group_array();
559 /* Search through the list looking for GID. */
560 for (i = 0; i < ngroups; i++)
561 if (gid == group_array[i])