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 NOFORK applet. Be very careful! */
28 /* test_main() is called from shells, and we need to be extra careful here.
29 * This is true regardless of PREFER_APPLETS and STANDALONE_SHELL
33 /* test(1) accepts the following grammar:
34 oexpr ::= aexpr | aexpr "-o" oexpr ;
35 aexpr ::= nexpr | nexpr "-a" aexpr ;
36 nexpr ::= primary | "!" primary
37 primary ::= unary-operator operand
38 | operand binary-operator operand
42 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
43 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
45 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
47 operand ::= <any legal UNIX file name>
92 #define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
93 #define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
94 #define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
95 #define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
96 #define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
97 #define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
106 static const struct t_op {
108 unsigned char op_num, op_type;
110 { "-r", FILRD , UNOP },
111 { "-w", FILWR , UNOP },
112 { "-x", FILEX , UNOP },
113 { "-e", FILEXIST, UNOP },
114 { "-f", FILREG , UNOP },
115 { "-d", FILDIR , UNOP },
116 { "-c", FILCDEV , UNOP },
117 { "-b", FILBDEV , UNOP },
118 { "-p", FILFIFO , UNOP },
119 { "-u", FILSUID , UNOP },
120 { "-g", FILSGID , UNOP },
121 { "-k", FILSTCK , UNOP },
122 { "-s", FILGZ , UNOP },
123 { "-t", FILTT , UNOP },
124 { "-z", STREZ , UNOP },
125 { "-n", STRNZ , UNOP },
126 { "-h", FILSYM , UNOP }, /* for backwards compat */
128 { "-O" , FILUID , UNOP },
129 { "-G" , FILGID , UNOP },
130 { "-L" , FILSYM , UNOP },
131 { "-S" , FILSOCK, UNOP },
132 { "=" , STREQ , BINOP },
133 { "==" , STREQ , BINOP },
134 { "!=" , STRNE , BINOP },
135 { "<" , STRLT , BINOP },
136 { ">" , STRGT , BINOP },
137 { "-eq", INTEQ , BINOP },
138 { "-ne", INTNE , BINOP },
139 { "-ge", INTGE , BINOP },
140 { "-gt", INTGT , BINOP },
141 { "-le", INTLE , BINOP },
142 { "-lt", INTLT , BINOP },
143 { "-nt", FILNT , BINOP },
144 { "-ot", FILOT , BINOP },
145 { "-ef", FILEQ , BINOP },
146 { "!" , UNOT , BUNOP },
147 { "-a" , BAND , BBINOP },
148 { "-o" , BOR , BBINOP },
149 { "(" , LPAREN , PAREN },
150 { ")" , RPAREN , PAREN },
154 #if ENABLE_FEATURE_TEST_64
155 typedef int64_t arith_t;
161 /* We try to minimize both static and stack usage. */
162 struct test_statics {
164 const struct t_op *t_wp_op;
170 /* See test_ptr_hack.c */
171 extern struct test_statics *const test_ptr_to_statics;
173 #define S (*test_ptr_to_statics)
174 #define t_wp (S.t_wp )
175 #define t_wp_op (S.t_wp_op )
176 #define group_array (S.group_array )
177 #define ngroups (S.ngroups )
178 #define leaving (S.leaving )
180 #define INIT_S() do { \
181 (*(struct test_statics**)&test_ptr_to_statics) = xzalloc(sizeof(S)); \
184 #define DEINIT_S() do { \
185 free(test_ptr_to_statics); \
188 static arith_t primary(enum token n);
190 static void syntax(const char *op, const char *msg) NORETURN;
191 static void syntax(const char *op, const char *msg)
194 bb_error_msg("%s: %s", op, msg);
196 bb_error_msg("%s: %s"+4, msg);
201 /* atoi with error detection */
202 //XXX: FIXME: duplicate of existing libbb function?
203 static arith_t getn(const char *s)
206 #if ENABLE_FEATURE_TEST_64
213 #if ENABLE_FEATURE_TEST_64
214 r = strtoll(s, &p, 10);
216 r = strtol(s, &p, 10);
220 syntax(s, "out of range");
222 if (*(skip_whitespace(p)))
223 syntax(s, "bad number");
229 static int newerf(const char *f1, const char *f2)
233 return (stat(f1, &b1) == 0 &&
234 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
237 static int olderf(const char *f1, const char *f2)
241 return (stat(f1, &b1) == 0 &&
242 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
245 static int equalf(const char *f1, const char *f2)
249 return (stat(f1, &b1) == 0 &&
250 stat(f2, &b2) == 0 &&
251 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
256 static enum token t_lex(char *s)
258 const struct t_op *op;
267 if (strcmp(s, op->op_text) == 0) {
272 } while (op < ops + ARRAY_SIZE(ops));
278 static int binop(void)
280 const char *opnd1, *opnd2;
281 struct t_op const *op;
285 (void) t_lex(*++t_wp);
290 syntax(op->op_text, "argument expected");
292 if (is_int_op(op->op_num)) {
295 if (op->op_num == INTEQ)
297 if (op->op_num == INTNE)
299 if (op->op_num == INTGE)
301 if (op->op_num == INTGT)
303 if (op->op_num == INTLE)
305 if (op->op_num == INTLT)
308 if (is_str_op(op->op_num)) {
309 val1 = strcmp(opnd1, opnd2);
310 if (op->op_num == STREQ)
312 if (op->op_num == STRNE)
314 if (op->op_num == STRLT)
316 if (op->op_num == STRGT)
319 /* We are sure that these three are by now the only binops we didn't check
320 * yet, so we do not check if the class is correct:
322 /* if (is_file_op(op->op_num)) */
326 if (stat(opnd1, &b1) || stat(opnd2, &b2))
327 return 0; /* false, since at least one stat failed */
328 if (op->op_num == FILNT)
329 return b1.st_mtime > b2.st_mtime;
330 if (op->op_num == FILOT)
331 return b1.st_mtime < b2.st_mtime;
332 if (op->op_num == FILEQ)
333 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
335 return 1; /* NOTREACHED */
339 static void initialize_group_array(void)
341 ngroups = getgroups(0, NULL);
343 /* FIXME: ash tries so hard to not die on OOM,
344 * and we spoil it with just one xrealloc here */
345 /* We realloc, because test_main can be entered repeatedly by shell.
346 * Testcase (ash): 'while true; do test -x some_file; done'
347 * and watch top. (some_file must have owner != you) */
348 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
349 getgroups(ngroups, group_array);
354 /* Return non-zero if GID is one that we have in our groups list. */
355 //XXX: FIXME: duplicate of existing libbb function?
356 // see toplevel TODO file:
357 // possible code duplication ingroup() and is_a_group_member()
358 static int is_a_group_member(gid_t gid)
362 /* Short-circuit if possible, maybe saving a call to getgroups(). */
363 if (gid == getgid() || gid == getegid())
367 initialize_group_array();
369 /* Search through the list looking for GID. */
370 for (i = 0; i < ngroups; i++)
371 if (gid == group_array[i])
378 /* Do the same thing access(2) does, but use the effective uid and gid,
379 and don't make the mistake of telling root that any file is
381 static int test_eaccess(char *path, int mode)
384 unsigned int euid = geteuid();
386 if (stat(path, &st) < 0)
390 /* Root can read or write any file. */
394 /* Root can execute any file that has any one of the execute
396 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
400 if (st.st_uid == euid) /* owner */
402 else if (is_a_group_member(st.st_gid))
405 if (st.st_mode & mode)
412 static int filstat(char *nm, enum token mode)
415 unsigned i = i; /* gcc 3.x thinks it can be used uninitialized */
417 if (mode == FILSYM) {
419 if (lstat(nm, &s) == 0) {
427 if (stat(nm, &s) != 0)
429 if (mode == FILEXIST)
431 if (is_file_access(mode)) {
438 return test_eaccess(nm, i) == 0;
440 if (is_file_type(mode)) {
449 if (mode == FILFIFO) {
456 if (mode == FILSOCK) {
464 return ((s.st_mode & S_IFMT) == i);
466 if (is_file_bit(mode)) {
473 return ((s.st_mode & i) != 0);
476 return s.st_size > 0L;
478 return s.st_uid == geteuid();
480 return s.st_gid == getegid();
481 return 1; /* NOTREACHED */
485 static arith_t nexpr(enum token n)
488 return !nexpr(t_lex(*++t_wp));
493 static arith_t aexpr(enum token n)
498 if (t_lex(*++t_wp) == BAND)
499 return aexpr(t_lex(*++t_wp)) && res;
505 static arith_t oexpr(enum token n)
510 if (t_lex(*++t_wp) == BOR) {
511 return oexpr(t_lex(*++t_wp)) || res;
519 static arith_t primary(enum token n)
524 syntax(NULL, "argument expected");
527 res = oexpr(t_lex(*++t_wp));
528 if (t_lex(*++t_wp) != RPAREN)
529 syntax(NULL, "closing paren expected");
532 if (t_wp_op && t_wp_op->op_type == UNOP) {
533 /* unary expression */
535 syntax(t_wp_op->op_text, "argument expected");
537 return t_wp[0][0] == '\0';
539 return t_wp[0][0] != '\0';
541 return isatty(getn(*t_wp));
542 return filstat(*t_wp, n);
546 if (t_wp_op && t_wp_op->op_type == BINOP) {
550 return t_wp[0][0] != '\0';
554 int test_main(int argc, char **argv)
560 arg0 = bb_basename(argv[0]);
561 if (arg0[0] == '[') {
563 if (!arg0[1]) { /* "[" ? */
564 if (NOT_LONE_CHAR(argv[argc], ']')) {
565 bb_error_msg("missing ]");
568 } else { /* assuming "[[" */
569 if (strcmp(argv[argc], "]]") != 0) {
570 bb_error_msg("missing ]]");
577 /* We must do DEINIT_S() prior to returning */
580 res = setjmp(leaving);
584 /* resetting ngroups is probably unnecessary. it will
585 * force a new call to getgroups(), which prevents using
586 * group data fetched during a previous call. but the
587 * only way the group data could be stale is if there's
588 * been an intervening call to setgroups(), and this
589 * isn't likely in the case of a shell. paranoia
597 /* Implement special cases from POSIX.2, section 4.62.4 */
598 if (!argv[0]) { /* "test" */
602 if (LONE_CHAR(argv[0], '!') && argv[1]) {
607 if (!argv[1]) { /* "test [!] arg" */
608 res = (*argv[0] == '\0');
611 if (argv[2] && !argv[3]) {
613 if (t_wp_op && t_wp_op->op_type == BINOP) {
614 /* "test [!] arg1 <binary_op> arg2" */
616 res = (binop() == 0);
621 /* Some complex expression. Undo '!' removal */
628 res = !oexpr(t_lex(*t_wp));
630 if (*t_wp != NULL && *++t_wp != NULL) {
631 bb_error_msg("%s: unknown operand", *t_wp);
636 return negate ? !res : res;