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."
30 /* test(1) accepts the following grammar:
31 oexpr ::= aexpr | aexpr "-o" oexpr ;
32 aexpr ::= nexpr | nexpr "-a" aexpr ;
33 nexpr ::= primary | "!" primary
34 primary ::= unary-operator operand
35 | operand binary-operator operand
39 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
40 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
42 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
44 operand ::= <any legal UNIX file name>
89 #define __is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
90 #define __is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
91 #define __is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
92 #define __is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
93 #define __is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
94 #define __is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
103 static const struct t_op {
104 const char * const op_text;
105 unsigned char op_num, op_type;
108 "-r", FILRD, UNOP}, {
109 "-w", FILWR, UNOP}, {
110 "-x", FILEX, UNOP}, {
111 "-e", FILEXIST, UNOP}, {
112 "-f", FILREG, UNOP}, {
113 "-d", FILDIR, UNOP}, {
114 "-c", FILCDEV, UNOP}, {
115 "-b", FILBDEV, UNOP}, {
116 "-p", FILFIFO, UNOP}, {
117 "-u", FILSUID, UNOP}, {
118 "-g", FILSGID, UNOP}, {
119 "-k", FILSTCK, UNOP}, {
120 "-s", FILGZ, UNOP}, {
121 "-t", FILTT, UNOP}, {
122 "-z", STREZ, UNOP}, {
123 "-n", STRNZ, UNOP}, {
124 "-h", FILSYM, UNOP}, /* for backwards compat */
126 "-O", FILUID, UNOP}, {
127 "-G", FILGID, UNOP}, {
128 "-L", FILSYM, UNOP}, {
129 "-S", FILSOCK, UNOP}, {
130 "=", STREQ, BINOP}, {
131 "==", STREQ, BINOP}, {
132 "!=", STRNE, BINOP}, {
133 "<", STRLT, BINOP}, {
134 ">", STRGT, BINOP}, {
135 "-eq", INTEQ, BINOP}, {
136 "-ne", INTNE, BINOP}, {
137 "-ge", INTGE, BINOP}, {
138 "-gt", INTGT, BINOP}, {
139 "-le", INTLE, BINOP}, {
140 "-lt", INTLT, BINOP}, {
141 "-nt", FILNT, BINOP}, {
142 "-ot", FILOT, BINOP}, {
143 "-ef", FILEQ, BINOP}, {
145 "-a", BAND, BBINOP}, {
146 "-o", BOR, BBINOP}, {
147 "(", LPAREN, PAREN}, {
148 ")", RPAREN, PAREN}, {
152 #ifdef CONFIG_FEATURE_TEST_64
153 typedef int64_t arith_t;
159 static struct t_op const *t_wp_op;
160 static gid_t *group_array;
163 static enum token t_lex(char *s);
164 static arith_t oexpr(enum token n);
165 static arith_t aexpr(enum token n);
166 static arith_t nexpr(enum token n);
167 static int binop(void);
168 static arith_t primary(enum token n);
169 static int filstat(char *nm, enum token mode);
170 static arith_t getn(const char *s);
171 static int newerf(const char *f1, const char *f2);
172 static int olderf(const char *f1, const char *f2);
173 static int equalf(const char *f1, const char *f2);
174 static int test_eaccess(char *path, int mode);
175 static int is_a_group_member(gid_t gid);
176 static void initialize_group_array(void);
178 static jmp_buf leaving;
180 int bb_test(int argc, char **argv)
184 if (LONE_CHAR(argv[0], '[')) {
186 if (NOT_LONE_CHAR(argv[argc], ']')) {
187 bb_error_msg("missing ]");
191 } else if (strcmp(argv[0], "[[") == 0) {
193 if (strcmp(argv[argc], "]]")) {
194 bb_error_msg("missing ]]");
200 res = setjmp(leaving);
204 /* resetting ngroups is probably unnecessary. it will
205 * force a new call to getgroups(), which prevents using
206 * group data fetched during a previous call. but the
207 * only way the group data could be stale is if there's
208 * been an intervening call to setgroups(), and this
209 * isn't likely in the case of a shell. paranoia
214 /* Implement special cases from POSIX.2, section 4.62.4 */
218 return *argv[1] == '\0';
220 if (LONE_CHAR(argv[1], '!')) {
223 return *argv[2] != '\0';
225 if (t_lex(argv[2+_off]), t_wp_op && t_wp_op->op_type == BINOP) {
226 t_wp = &argv[1+_off];
231 res = !oexpr(t_lex(*t_wp));
233 if (*t_wp != NULL && *++t_wp != NULL) {
234 bb_error_msg("%s: unknown operand", *t_wp);
240 static void syntax(const char *op, const char *msg)
243 bb_error_msg("%s: %s", op, msg);
245 bb_error_msg("%s", msg);
250 static arith_t oexpr(enum token n)
255 if (t_lex(*++t_wp) == BOR) {
256 return oexpr(t_lex(*++t_wp)) || res;
262 static arith_t aexpr(enum token n)
267 if (t_lex(*++t_wp) == BAND)
268 return aexpr(t_lex(*++t_wp)) && res;
273 static arith_t nexpr(enum token n)
276 return !nexpr(t_lex(*++t_wp));
280 static arith_t primary(enum token n)
285 syntax(NULL, "argument expected");
288 res = oexpr(t_lex(*++t_wp));
289 if (t_lex(*++t_wp) != RPAREN)
290 syntax(NULL, "closing paren expected");
293 if (t_wp_op && t_wp_op->op_type == UNOP) {
294 /* unary expression */
296 syntax(t_wp_op->op_text, "argument expected");
298 return strlen(*t_wp) == 0;
300 return strlen(*t_wp) != 0;
302 return isatty(getn(*t_wp));
304 return filstat(*t_wp, n);
307 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
311 return strlen(*t_wp) > 0;
314 static int binop(void)
316 const char *opnd1, *opnd2;
317 struct t_op const *op;
321 (void) t_lex(*++t_wp);
324 if ((opnd2 = *++t_wp) == (char *) 0)
325 syntax(op->op_text, "argument expected");
327 if (__is_int_op(op->op_num)) {
330 if (op->op_num == INTEQ)
332 if (op->op_num == INTNE)
334 if (op->op_num == INTGE)
336 if (op->op_num == INTGT)
338 if (op->op_num == INTLE)
340 if (op->op_num == INTLT)
343 if (__is_str_op(op->op_num)) {
344 val1 = strcmp(opnd1, opnd2);
345 if (op->op_num == STREQ)
347 if (op->op_num == STRNE)
349 if (op->op_num == STRLT)
351 if (op->op_num == STRGT)
354 /* We are sure that these three are by now the only binops we didn't check
355 * yet, so we do not check if the class is correct:
357 /* if (__is_file_op(op->op_num)) */
361 if (!(!stat(opnd1, &b1) && !stat(opnd2, &b2)))
362 return 0; /* false, since stat failed */
363 if (op->op_num == FILNT)
364 return b1.st_mtime > b2.st_mtime;
365 if (op->op_num == FILOT)
366 return b1.st_mtime < b2.st_mtime;
367 if (op->op_num == FILEQ)
368 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
370 return 1; /* NOTREACHED */
373 static int filstat(char *nm, enum token mode)
378 if (mode == FILSYM) {
380 if (lstat(nm, &s) == 0) {
388 if (stat(nm, &s) != 0)
390 if (mode == FILEXIST)
392 else if (__is_file_access(mode)) {
399 return test_eaccess(nm, i) == 0;
401 else if (__is_file_type(mode)) {
410 if (mode == FILFIFO) {
417 if (mode == FILSOCK) {
425 return ((s.st_mode & S_IFMT) == i);
427 else if (__is_file_bit(mode)) {
434 return ((s.st_mode & i) != 0);
436 else if (mode == FILGZ)
437 return s.st_size > 0L;
438 else if (mode == FILUID)
439 return s.st_uid == geteuid();
440 else if (mode == FILGID)
441 return s.st_gid == getegid();
443 return 1; /* NOTREACHED */
447 static enum token t_lex(char *s)
449 struct t_op const *op = ops;
452 t_wp_op = (struct t_op *) 0;
455 while (op->op_text) {
456 if (strcmp(s, op->op_text) == 0) {
462 t_wp_op = (struct t_op *) 0;
466 /* atoi with error detection */
467 //XXX: FIXME: duplicate of existing libbb function?
468 static arith_t getn(const char *s)
471 #ifdef CONFIG_FEATURE_TEST_64
478 #ifdef CONFIG_FEATURE_TEST_64
479 r = strtoll(s, &p, 10);
481 r = strtol(s, &p, 10);
485 syntax(s, "out of range");
487 if (*(skip_whitespace(p)))
488 syntax(s, "bad number");
493 static int newerf(const char *f1, const char *f2)
497 return (stat(f1, &b1) == 0 &&
498 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
501 static int olderf(const char *f1, const char *f2)
505 return (stat(f1, &b1) == 0 &&
506 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
509 static int equalf(const char *f1, const char *f2)
513 return (stat(f1, &b1) == 0 &&
514 stat(f2, &b2) == 0 &&
515 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
518 /* Do the same thing access(2) does, but use the effective uid and gid,
519 and don't make the mistake of telling root that any file is
521 static int test_eaccess(char *path, int mode)
524 unsigned int euid = geteuid();
526 if (stat(path, &st) < 0)
530 /* Root can read or write any file. */
534 /* Root can execute any file that has any one of the execute
536 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
540 if (st.st_uid == euid) /* owner */
542 else if (is_a_group_member(st.st_gid))
545 if (st.st_mode & mode)
551 static void initialize_group_array(void)
553 ngroups = getgroups(0, NULL);
555 group_array = xmalloc(ngroups * sizeof(gid_t));
556 getgroups(ngroups, group_array);
560 /* Return non-zero if GID is one that we have in our groups list. */
561 //XXX: FIXME: duplicate of existing libbb function?
562 // see toplevel TODO file:
563 // possible code duplication ingroup() and is_a_group_member()
564 static int is_a_group_member(gid_t gid)
568 /* Short-circuit if possible, maybe saving a call to getgroups(). */
569 if (gid == getgid() || gid == getegid())
573 initialize_group_array();
575 /* Search through the list looking for GID. */
576 for (i = 0; i < ngroups; i++)
577 if (gid == group_array[i])
584 /* applet entry point */
586 int test_main(int argc, char **argv);
587 int test_main(int argc, char **argv)
589 return bb_test(argc, argv);