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);
172 static int newerf(const char *f1, const char *f2);
173 static int olderf(const char *f1, const char *f2);
174 static int equalf(const char *f1, const char *f2);
176 static int test_eaccess(char *path, int mode);
177 static int is_a_group_member(gid_t gid);
178 static void initialize_group_array(void);
180 static jmp_buf leaving;
182 int bb_test(int argc, char **argv)
186 if (LONE_CHAR(argv[0], '[')) {
188 if (NOT_LONE_CHAR(argv[argc], ']')) {
189 bb_error_msg("missing ]");
193 } else if (strcmp(argv[0], "[[") == 0) {
195 if (strcmp(argv[argc], "]]")) {
196 bb_error_msg("missing ]]");
202 res = setjmp(leaving);
206 /* resetting ngroups is probably unnecessary. it will
207 * force a new call to getgroups(), which prevents using
208 * group data fetched during a previous call. but the
209 * only way the group data could be stale is if there's
210 * been an intervening call to setgroups(), and this
211 * isn't likely in the case of a shell. paranoia
216 /* Implement special cases from POSIX.2, section 4.62.4 */
220 return *argv[1] == '\0';
222 if (LONE_CHAR(argv[1], '!')) {
225 return *argv[2] != '\0';
227 if (t_lex(argv[2+_off]), t_wp_op && t_wp_op->op_type == BINOP) {
228 t_wp = &argv[1+_off];
233 res = !oexpr(t_lex(*t_wp));
235 if (*t_wp != NULL && *++t_wp != NULL) {
236 bb_error_msg("%s: unknown operand", *t_wp);
242 static void syntax(const char *op, const char *msg)
245 bb_error_msg("%s: %s", op, msg);
247 bb_error_msg("%s", msg);
252 static arith_t oexpr(enum token n)
257 if (t_lex(*++t_wp) == BOR) {
258 return oexpr(t_lex(*++t_wp)) || res;
264 static arith_t aexpr(enum token n)
269 if (t_lex(*++t_wp) == BAND)
270 return aexpr(t_lex(*++t_wp)) && res;
275 static arith_t nexpr(enum token n)
278 return !nexpr(t_lex(*++t_wp));
282 static arith_t primary(enum token n)
287 syntax(NULL, "argument expected");
290 res = oexpr(t_lex(*++t_wp));
291 if (t_lex(*++t_wp) != RPAREN)
292 syntax(NULL, "closing paren expected");
295 if (t_wp_op && t_wp_op->op_type == UNOP) {
296 /* unary expression */
298 syntax(t_wp_op->op_text, "argument expected");
300 return strlen(*t_wp) == 0;
302 return strlen(*t_wp) != 0;
304 return isatty(getn(*t_wp));
306 return filstat(*t_wp, n);
309 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
313 return strlen(*t_wp) > 0;
316 static int binop(void)
318 const char *opnd1, *opnd2;
319 struct t_op const *op;
323 (void) t_lex(*++t_wp);
326 if ((opnd2 = *++t_wp) == (char *) 0)
327 syntax(op->op_text, "argument expected");
329 if (__is_int_op(op->op_num)) {
332 if (op->op_num == INTEQ)
334 if (op->op_num == INTNE)
336 if (op->op_num == INTGE)
338 if (op->op_num == INTGT)
340 if (op->op_num == INTLE)
342 if (op->op_num == INTLT)
345 if (__is_str_op(op->op_num)) {
346 val1 = strcmp(opnd1, opnd2);
347 if (op->op_num == STREQ)
349 if (op->op_num == STRNE)
351 if (op->op_num == STRLT)
353 if (op->op_num == STRGT)
356 /* We are sure that these three are by now the only binops we didn't check
357 * yet, so we do not check if the class is correct:
359 /* if (__is_file_op(op->op_num)) */
363 if (!(!stat(opnd1, &b1) && !stat(opnd2, &b2)))
364 return 0; /* false, since stat failed */
365 if (op->op_num == FILNT)
366 return b1.st_mtime > b2.st_mtime;
367 if (op->op_num == FILOT)
368 return b1.st_mtime < b2.st_mtime;
369 if (op->op_num == FILEQ)
370 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
372 return 1; /* NOTREACHED */
375 static int filstat(char *nm, enum token mode)
380 if (mode == FILSYM) {
382 if (lstat(nm, &s) == 0) {
390 if (stat(nm, &s) != 0)
392 if (mode == FILEXIST)
394 else if (__is_file_access(mode)) {
401 return test_eaccess(nm, i) == 0;
403 else if (__is_file_type(mode)) {
412 if (mode == FILFIFO) {
419 if (mode == FILSOCK) {
427 return ((s.st_mode & S_IFMT) == i);
429 else if (__is_file_bit(mode)) {
436 return ((s.st_mode & i) != 0);
438 else if (mode == FILGZ)
439 return s.st_size > 0L;
440 else if (mode == FILUID)
441 return s.st_uid == geteuid();
442 else if (mode == FILGID)
443 return s.st_gid == getegid();
445 return 1; /* NOTREACHED */
449 static enum token t_lex(char *s)
451 struct t_op const *op = ops;
454 t_wp_op = (struct t_op *) 0;
457 while (op->op_text) {
458 if (strcmp(s, op->op_text) == 0) {
464 t_wp_op = (struct t_op *) 0;
468 /* atoi with error detection */
469 //XXX: FIXME: duplicate of existing libbb function?
470 static arith_t getn(const char *s)
473 #ifdef CONFIG_FEATURE_TEST_64
480 #ifdef CONFIG_FEATURE_TEST_64
481 r = strtoll(s, &p, 10);
483 r = strtol(s, &p, 10);
487 syntax(s, "out of range");
489 if (*(skip_whitespace(p)))
490 syntax(s, "bad number");
496 static int newerf(const char *f1, const char *f2)
500 return (stat(f1, &b1) == 0 &&
501 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
504 static int olderf(const char *f1, const char *f2)
508 return (stat(f1, &b1) == 0 &&
509 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
512 static int equalf(const char *f1, const char *f2)
516 return (stat(f1, &b1) == 0 &&
517 stat(f2, &b2) == 0 &&
518 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
522 /* Do the same thing access(2) does, but use the effective uid and gid,
523 and don't make the mistake of telling root that any file is
525 static int test_eaccess(char *path, int mode)
528 unsigned int euid = geteuid();
530 if (stat(path, &st) < 0)
534 /* Root can read or write any file. */
538 /* Root can execute any file that has any one of the execute
540 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
544 if (st.st_uid == euid) /* owner */
546 else if (is_a_group_member(st.st_gid))
549 if (st.st_mode & mode)
555 static void initialize_group_array(void)
557 ngroups = getgroups(0, NULL);
559 group_array = xmalloc(ngroups * sizeof(gid_t));
560 getgroups(ngroups, group_array);
564 /* Return non-zero if GID is one that we have in our groups list. */
565 //XXX: FIXME: duplicate of existing libbb function?
566 // see toplevel TODO file:
567 // possible code duplication ingroup() and is_a_group_member()
568 static int is_a_group_member(gid_t gid)
572 /* Short-circuit if possible, maybe saving a call to getgroups(). */
573 if (gid == getgid() || gid == getegid())
577 initialize_group_array();
579 /* Search through the list looking for GID. */
580 for (i = 0; i < ngroups; i++)
581 if (gid == group_array[i])
588 /* applet entry point */
590 int test_main(int argc, char **argv);
591 int test_main(int argc, char **argv)
593 return bb_test(argc, argv);