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. */
164 const struct t_op *t_wp_op;
170 /* Make it reside in writable memory, yet make compiler understand
171 * that it is not going to change. */
172 static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
174 #define S (*ptr_to_statics)
175 #define t_wp (S.t_wp )
176 #define t_wp_op (S.t_wp_op )
177 #define group_array (S.group_array )
178 #define ngroups (S.ngroups )
179 #define leaving (S.leaving )
181 #define INIT_S() do { \
182 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \
185 #define DEINIT_S() do { \
186 free(ptr_to_statics); \
189 static arith_t primary(enum token n);
191 static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
192 static void syntax(const char *op, const char *msg)
195 bb_error_msg("%s: %s", op, msg);
197 bb_error_msg("%s: %s"+4, msg);
202 /* atoi with error detection */
203 //XXX: FIXME: duplicate of existing libbb function?
204 static arith_t getn(const char *s)
207 #if ENABLE_FEATURE_TEST_64
214 #if ENABLE_FEATURE_TEST_64
215 r = strtoll(s, &p, 10);
217 r = strtol(s, &p, 10);
221 syntax(s, "out of range");
223 if (*(skip_whitespace(p)))
224 syntax(s, "bad number");
230 static int newerf(const char *f1, const char *f2)
234 return (stat(f1, &b1) == 0 &&
235 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
238 static int olderf(const char *f1, const char *f2)
242 return (stat(f1, &b1) == 0 &&
243 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
246 static int equalf(const char *f1, const char *f2)
250 return (stat(f1, &b1) == 0 &&
251 stat(f2, &b2) == 0 &&
252 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
257 static enum token t_lex(char *s)
259 const struct t_op *op;
268 if (strcmp(s, op->op_text) == 0) {
273 } while (op < ops + ARRAY_SIZE(ops));
279 static int binop(void)
281 const char *opnd1, *opnd2;
282 struct t_op const *op;
286 (void) t_lex(*++t_wp);
291 syntax(op->op_text, "argument expected");
293 if (is_int_op(op->op_num)) {
296 if (op->op_num == INTEQ)
298 if (op->op_num == INTNE)
300 if (op->op_num == INTGE)
302 if (op->op_num == INTGT)
304 if (op->op_num == INTLE)
306 if (op->op_num == INTLT)
309 if (is_str_op(op->op_num)) {
310 val1 = strcmp(opnd1, opnd2);
311 if (op->op_num == STREQ)
313 if (op->op_num == STRNE)
315 if (op->op_num == STRLT)
317 if (op->op_num == STRGT)
320 /* We are sure that these three are by now the only binops we didn't check
321 * yet, so we do not check if the class is correct:
323 /* if (is_file_op(op->op_num)) */
327 if (stat(opnd1, &b1) || stat(opnd2, &b2))
328 return 0; /* false, since at least one stat failed */
329 if (op->op_num == FILNT)
330 return b1.st_mtime > b2.st_mtime;
331 if (op->op_num == FILOT)
332 return b1.st_mtime < b2.st_mtime;
333 if (op->op_num == FILEQ)
334 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
336 return 1; /* NOTREACHED */
340 static void initialize_group_array(void)
342 ngroups = getgroups(0, NULL);
344 /* FIXME: ash tries so hard to not die on OOM,
345 * and we spoil it with just one xrealloc here */
346 /* We realloc, because test_main can be entered repeatedly by shell.
347 * Testcase (ash): 'while true; do test -x some_file; done'
348 * and watch top. (some_file must have owner != you) */
349 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
350 getgroups(ngroups, group_array);
355 /* Return non-zero if GID is one that we have in our groups list. */
356 //XXX: FIXME: duplicate of existing libbb function?
357 // see toplevel TODO file:
358 // possible code duplication ingroup() and is_a_group_member()
359 static int is_a_group_member(gid_t gid)
363 /* Short-circuit if possible, maybe saving a call to getgroups(). */
364 if (gid == getgid() || gid == getegid())
368 initialize_group_array();
370 /* Search through the list looking for GID. */
371 for (i = 0; i < ngroups; i++)
372 if (gid == group_array[i])
379 /* Do the same thing access(2) does, but use the effective uid and gid,
380 and don't make the mistake of telling root that any file is
382 static int test_eaccess(char *path, int mode)
385 unsigned int euid = geteuid();
387 if (stat(path, &st) < 0)
391 /* Root can read or write any file. */
395 /* Root can execute any file that has any one of the execute
397 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
401 if (st.st_uid == euid) /* owner */
403 else if (is_a_group_member(st.st_gid))
406 if (st.st_mode & mode)
413 static int filstat(char *nm, enum token mode)
416 int i = i; /* gcc 3.x thinks it can be used uninitialized */
418 if (mode == FILSYM) {
420 if (lstat(nm, &s) == 0) {
428 if (stat(nm, &s) != 0)
430 if (mode == FILEXIST)
432 if (is_file_access(mode)) {
439 return test_eaccess(nm, i) == 0;
441 if (is_file_type(mode)) {
450 if (mode == FILFIFO) {
457 if (mode == FILSOCK) {
465 return ((s.st_mode & S_IFMT) == i);
467 if (is_file_bit(mode)) {
474 return ((s.st_mode & i) != 0);
477 return s.st_size > 0L;
479 return s.st_uid == geteuid();
481 return s.st_gid == getegid();
482 return 1; /* NOTREACHED */
486 static arith_t nexpr(enum token n)
489 return !nexpr(t_lex(*++t_wp));
494 static arith_t aexpr(enum token n)
499 if (t_lex(*++t_wp) == BAND)
500 return aexpr(t_lex(*++t_wp)) && res;
506 static arith_t oexpr(enum token n)
511 if (t_lex(*++t_wp) == BOR) {
512 return oexpr(t_lex(*++t_wp)) || res;
520 static arith_t primary(enum token n)
525 syntax(NULL, "argument expected");
528 res = oexpr(t_lex(*++t_wp));
529 if (t_lex(*++t_wp) != RPAREN)
530 syntax(NULL, "closing paren expected");
533 if (t_wp_op && t_wp_op->op_type == UNOP) {
534 /* unary expression */
536 syntax(t_wp_op->op_text, "argument expected");
538 return t_wp[0][0] == '\0';
540 return t_wp[0][0] != '\0';
542 return isatty(getn(*t_wp));
543 return filstat(*t_wp, n);
547 if (t_wp_op && t_wp_op->op_type == BINOP) {
551 return t_wp[0][0] != '\0';
555 int test_main(int argc, char **argv)
561 arg0 = bb_basename(argv[0]);
562 if (arg0[0] == '[') {
564 if (!arg0[1]) { /* "[" ? */
565 if (NOT_LONE_CHAR(argv[argc], ']')) {
566 bb_error_msg("missing ]");
569 } else { /* assuming "[[" */
570 if (strcmp(argv[argc], "]]") != 0) {
571 bb_error_msg("missing ]]");
578 /* We must do DEINIT_S() prior to returning */
581 res = setjmp(leaving);
585 /* resetting ngroups is probably unnecessary. it will
586 * force a new call to getgroups(), which prevents using
587 * group data fetched during a previous call. but the
588 * only way the group data could be stale is if there's
589 * been an intervening call to setgroups(), and this
590 * isn't likely in the case of a shell. paranoia
598 /* Implement special cases from POSIX.2, section 4.62.4 */
599 if (!argv[0]) { /* "test" */
603 if (LONE_CHAR(argv[0], '!') && argv[1]) {
608 if (!argv[1]) { /* "test [!] arg" */
609 res = (*argv[0] == '\0');
612 if (argv[2] && !argv[3]) {
614 if (t_wp_op && t_wp_op->op_type == BINOP) {
615 /* "test [!] arg1 <binary_op> arg2" */
617 res = (binop() == 0);
622 /* Some complex expression. Undo '!' removal */
629 res = !oexpr(t_lex(*t_wp));
631 if (*t_wp != NULL && *++t_wp != NULL) {
632 bb_error_msg("%s: unknown operand", *t_wp);
637 return negate ? !res : res;