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
15 * modified by Bernhard Fischer to be useable (i.e. a bit less bloaty).
17 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
19 * Original copyright notice states:
20 * "This program is in the Public Domain."
26 /* This is a NOEXEC applet. Be very careful! */
29 /* test(1) accepts the following grammar:
30 oexpr ::= aexpr | aexpr "-o" oexpr ;
31 aexpr ::= nexpr | nexpr "-a" aexpr ;
32 nexpr ::= primary | "!" primary
33 primary ::= unary-operator operand
34 | operand binary-operator operand
38 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
39 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
41 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
43 operand ::= <any legal UNIX file name>
88 #define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
89 #define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
90 #define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
91 #define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
92 #define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
93 #define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
102 static const struct t_op {
104 unsigned char op_num, op_type;
106 { "-r", FILRD , UNOP },
107 { "-w", FILWR , UNOP },
108 { "-x", FILEX , UNOP },
109 { "-e", FILEXIST, UNOP },
110 { "-f", FILREG , UNOP },
111 { "-d", FILDIR , UNOP },
112 { "-c", FILCDEV , UNOP },
113 { "-b", FILBDEV , UNOP },
114 { "-p", FILFIFO , UNOP },
115 { "-u", FILSUID , UNOP },
116 { "-g", FILSGID , UNOP },
117 { "-k", FILSTCK , UNOP },
118 { "-s", FILGZ , UNOP },
119 { "-t", FILTT , UNOP },
120 { "-z", STREZ , UNOP },
121 { "-n", STRNZ , UNOP },
122 { "-h", FILSYM , UNOP }, /* for backwards compat */
124 { "-O" , FILUID , UNOP },
125 { "-G" , FILGID , UNOP },
126 { "-L" , FILSYM , UNOP },
127 { "-S" , FILSOCK, UNOP },
128 { "=" , STREQ , BINOP },
129 { "==" , STREQ , BINOP },
130 { "!=" , STRNE , BINOP },
131 { "<" , STRLT , BINOP },
132 { ">" , STRGT , BINOP },
133 { "-eq", INTEQ , BINOP },
134 { "-ne", INTNE , BINOP },
135 { "-ge", INTGE , BINOP },
136 { "-gt", INTGT , BINOP },
137 { "-le", INTLE , BINOP },
138 { "-lt", INTLT , BINOP },
139 { "-nt", FILNT , BINOP },
140 { "-ot", FILOT , BINOP },
141 { "-ef", FILEQ , BINOP },
142 { "!" , UNOT , BUNOP },
143 { "-a" , BAND , BBINOP },
144 { "-o" , BOR , BBINOP },
145 { "(" , LPAREN , PAREN },
146 { ")" , RPAREN , PAREN },
149 enum { NUM_OPS = sizeof(ops) / sizeof(ops[0]) };
151 #if ENABLE_FEATURE_TEST_64
152 typedef int64_t arith_t;
157 /* Cannot eliminate these static data (do the G trick)
158 * because of bb_test usage from other applets */
160 static struct t_op const *t_wp_op;
161 static gid_t *group_array;
163 static jmp_buf leaving;
165 static enum token t_lex(char *s);
166 static arith_t oexpr(enum token n);
167 static arith_t aexpr(enum token n);
168 static arith_t nexpr(enum token n);
169 static int binop(void);
170 static arith_t primary(enum token n);
171 static int filstat(char *nm, enum token mode);
172 static arith_t getn(const char *s);
174 static int newerf(const char *f1, const char *f2);
175 static int olderf(const char *f1, const char *f2);
176 static int equalf(const char *f1, const char *f2);
178 static int test_eaccess(char *path, int mode);
179 static int is_a_group_member(gid_t gid);
180 static void initialize_group_array(void);
182 int bb_test(int argc, char **argv)
188 arg0 = strrchr(argv[0], '/');
189 if (!arg0++) arg0 = argv[0];
190 if (arg0[0] == '[') {
191 if (!arg0[1]) { /* "[" ? */
193 if (NOT_LONE_CHAR(argv[argc], ']')) {
194 bb_error_msg("missing ]");
198 } else if (LONE_CHAR(arg0+1, '[') == 0) { /* "[[" ? */
200 if (strcmp(argv[argc], "]]") != 0) {
201 bb_error_msg("missing ]]");
208 res = setjmp(leaving);
212 /* resetting ngroups is probably unnecessary. it will
213 * force a new call to getgroups(), which prevents using
214 * group data fetched during a previous call. but the
215 * only way the group data could be stale is if there's
216 * been an intervening call to setgroups(), and this
217 * isn't likely in the case of a shell. paranoia
222 /* Implement special cases from POSIX.2, section 4.62.4 */
226 return *argv[1] == '\0';
228 /* remember if we saw argc==4 which wants *no* '!' test */
231 (LONE_CHAR(argv[1], '!'))
232 : (argv[1][0] != '!' || argv[1][1] != '\0'))
235 return *argv[2] != '\0';
237 t_lex(argv[2 + _off]);
238 if (t_wp_op && t_wp_op->op_type == BINOP) {
239 t_wp = &argv[1 + _off];
240 return binop() == _off;
244 res = !oexpr(t_lex(*t_wp));
246 if (*t_wp != NULL && *++t_wp != NULL) {
247 bb_error_msg("%s: unknown operand", *t_wp);
253 static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
254 static void syntax(const char *op, const char *msg)
257 bb_error_msg("%s: %s", op, msg);
259 bb_error_msg("%s: %s"+4, msg);
264 static arith_t oexpr(enum token n)
269 if (t_lex(*++t_wp) == BOR) {
270 return oexpr(t_lex(*++t_wp)) || res;
276 static arith_t aexpr(enum token n)
281 if (t_lex(*++t_wp) == BAND)
282 return aexpr(t_lex(*++t_wp)) && res;
287 static arith_t nexpr(enum token n)
290 return !nexpr(t_lex(*++t_wp));
294 static arith_t primary(enum token n)
299 syntax(NULL, "argument expected");
302 res = oexpr(t_lex(*++t_wp));
303 if (t_lex(*++t_wp) != RPAREN)
304 syntax(NULL, "closing paren expected");
307 if (t_wp_op && t_wp_op->op_type == UNOP) {
308 /* unary expression */
310 syntax(t_wp_op->op_text, "argument expected");
312 return t_wp[0][0] == '\0';
314 return t_wp[0][0] != '\0';
316 return isatty(getn(*t_wp));
317 return filstat(*t_wp, n);
321 if (t_wp_op && t_wp_op->op_type == BINOP) {
325 return t_wp[0][0] != '\0';
328 static int binop(void)
330 const char *opnd1, *opnd2;
331 struct t_op const *op;
335 (void) t_lex(*++t_wp);
340 syntax(op->op_text, "argument expected");
342 if (is_int_op(op->op_num)) {
345 if (op->op_num == INTEQ)
347 if (op->op_num == INTNE)
349 if (op->op_num == INTGE)
351 if (op->op_num == INTGT)
353 if (op->op_num == INTLE)
355 if (op->op_num == INTLT)
358 if (is_str_op(op->op_num)) {
359 val1 = strcmp(opnd1, opnd2);
360 if (op->op_num == STREQ)
362 if (op->op_num == STRNE)
364 if (op->op_num == STRLT)
366 if (op->op_num == STRGT)
369 /* We are sure that these three are by now the only binops we didn't check
370 * yet, so we do not check if the class is correct:
372 /* if (is_file_op(op->op_num)) */
376 if (stat(opnd1, &b1) || stat(opnd2, &b2))
377 return 0; /* false, since at least one stat failed */
378 if (op->op_num == FILNT)
379 return b1.st_mtime > b2.st_mtime;
380 if (op->op_num == FILOT)
381 return b1.st_mtime < b2.st_mtime;
382 if (op->op_num == FILEQ)
383 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
385 return 1; /* NOTREACHED */
388 static int filstat(char *nm, enum token mode)
393 if (mode == FILSYM) {
395 if (lstat(nm, &s) == 0) {
403 if (stat(nm, &s) != 0)
405 if (mode == FILEXIST)
407 if (is_file_access(mode)) {
414 return test_eaccess(nm, i) == 0;
416 if (is_file_type(mode)) {
425 if (mode == FILFIFO) {
432 if (mode == FILSOCK) {
440 return ((s.st_mode & S_IFMT) == i);
442 if (is_file_bit(mode)) {
449 return ((s.st_mode & i) != 0);
452 return s.st_size > 0L;
454 return s.st_uid == geteuid();
456 return s.st_gid == getegid();
457 return 1; /* NOTREACHED */
460 static enum token t_lex(char *s)
462 const struct t_op *op;
471 if (strcmp(s, op->op_text) == 0) {
476 } while (op < ops + NUM_OPS);
481 /* atoi with error detection */
482 //XXX: FIXME: duplicate of existing libbb function?
483 static arith_t getn(const char *s)
486 #if ENABLE_FEATURE_TEST_64
493 #if ENABLE_FEATURE_TEST_64
494 r = strtoll(s, &p, 10);
496 r = strtol(s, &p, 10);
500 syntax(s, "out of range");
502 if (*(skip_whitespace(p)))
503 syntax(s, "bad number");
509 static int newerf(const char *f1, const char *f2)
513 return (stat(f1, &b1) == 0 &&
514 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
517 static int olderf(const char *f1, const char *f2)
521 return (stat(f1, &b1) == 0 &&
522 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
525 static int equalf(const char *f1, const char *f2)
529 return (stat(f1, &b1) == 0 &&
530 stat(f2, &b2) == 0 &&
531 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
535 /* Do the same thing access(2) does, but use the effective uid and gid,
536 and don't make the mistake of telling root that any file is
538 static int test_eaccess(char *path, int mode)
541 unsigned int euid = geteuid();
543 if (stat(path, &st) < 0)
547 /* Root can read or write any file. */
551 /* Root can execute any file that has any one of the execute
553 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
557 if (st.st_uid == euid) /* owner */
559 else if (is_a_group_member(st.st_gid))
562 if (st.st_mode & mode)
568 static void initialize_group_array(void)
570 ngroups = getgroups(0, NULL);
572 /* FIXME: ash tries so hard to not die on OOM,
573 * and we spoil it with just one xrealloc here */
574 /* We realloc, because bb_test can be entered repeatedly by shell.
575 * Testcase (ash): 'while true; do test -x some_file; done'
576 * and watch top. (some_file must have owner != you) */
577 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
578 getgroups(ngroups, group_array);
582 /* Return non-zero if GID is one that we have in our groups list. */
583 //XXX: FIXME: duplicate of existing libbb function?
584 // see toplevel TODO file:
585 // possible code duplication ingroup() and is_a_group_member()
586 static int is_a_group_member(gid_t gid)
590 /* Short-circuit if possible, maybe saving a call to getgroups(). */
591 if (gid == getgid() || gid == getegid())
595 initialize_group_array();
597 /* Search through the list looking for GID. */
598 for (i = 0; i < ngroups; i++)
599 if (gid == group_array[i])
606 /* applet entry point */
608 int test_main(int argc, char **argv);
609 int test_main(int argc, char **argv)
611 return bb_test(argc, argv);