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
16 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
18 * Original copyright notice states:
19 * "This program is in the Public Domain."
22 #include <sys/types.h>
31 /* test(1) accepts the following grammar:
32 oexpr ::= aexpr | aexpr "-o" oexpr ;
33 aexpr ::= nexpr | nexpr "-a" aexpr ;
34 nexpr ::= primary | "!" primary
35 primary ::= unary-operator operand
36 | operand binary-operator operand
40 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
41 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
43 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
45 operand ::= <any legal UNIX file name>
99 static const struct t_op {
101 short op_num, op_type;
104 "-r", FILRD, UNOP}, {
105 "-w", FILWR, UNOP}, {
106 "-x", FILEX, UNOP}, {
107 "-e", FILEXIST, UNOP}, {
108 "-f", FILREG, UNOP}, {
109 "-d", FILDIR, UNOP}, {
110 "-c", FILCDEV, UNOP}, {
111 "-b", FILBDEV, UNOP}, {
112 "-p", FILFIFO, UNOP}, {
113 "-u", FILSUID, UNOP}, {
114 "-g", FILSGID, UNOP}, {
115 "-k", FILSTCK, UNOP}, {
116 "-s", FILGZ, UNOP}, {
117 "-t", FILTT, UNOP}, {
118 "-z", STREZ, UNOP}, {
119 "-n", STRNZ, UNOP}, {
120 "-h", FILSYM, UNOP}, /* for backwards compat */
122 "-O", FILUID, UNOP}, {
123 "-G", FILGID, UNOP}, {
124 "-L", FILSYM, UNOP}, {
125 "-S", FILSOCK, UNOP}, {
126 "=", STREQ, BINOP}, {
127 "==", STREQ, BINOP}, {
128 "!=", STRNE, BINOP}, {
129 "<", STRLT, BINOP}, {
130 ">", STRGT, BINOP}, {
131 "-eq", INTEQ, BINOP}, {
132 "-ne", INTNE, BINOP}, {
133 "-ge", INTGE, BINOP}, {
134 "-gt", INTGT, BINOP}, {
135 "-le", INTLE, BINOP}, {
136 "-lt", INTLT, BINOP}, {
137 "-nt", FILNT, BINOP}, {
138 "-ot", FILOT, BINOP}, {
139 "-ef", FILEQ, BINOP}, {
141 "-a", BAND, BBINOP}, {
142 "-o", BOR, BBINOP}, {
143 "(", LPAREN, PAREN}, {
144 ")", RPAREN, PAREN}, {
148 #ifdef CONFIG_FEATURE_TEST_64
149 typedef int64_t arith_t;
155 static struct t_op const *t_wp_op;
156 static gid_t *group_array = NULL;
159 static enum token t_lex(char *s);
160 static arith_t oexpr(enum token n);
161 static arith_t aexpr(enum token n);
162 static arith_t nexpr(enum token n);
163 static int binop(void);
164 static arith_t primary(enum token n);
165 static int filstat(char *nm, enum token mode);
166 static arith_t getn(const char *s);
167 static int newerf(const char *f1, const char *f2);
168 static int olderf(const char *f1, const char *f2);
169 static int equalf(const char *f1, const char *f2);
170 static int test_eaccess(char *path, int mode);
171 static int is_a_group_member(gid_t gid);
172 static void initialize_group_array(void);
174 static jmp_buf leaving;
176 int bb_test(int argc, char **argv)
180 if (strcmp(argv[0], "[") == 0) {
181 if (strcmp(argv[--argc], "]")) {
182 bb_error_msg("missing ]");
186 } else if (strcmp(argv[0], "[[") == 0) {
187 if (strcmp(argv[--argc], "]]")) {
188 bb_error_msg("missing ]]");
194 res = setjmp(leaving);
198 /* resetting ngroups is probably unnecessary. it will
199 * force a new call to getgroups(), which prevents using
200 * group data fetched during a previous call. but the
201 * only way the group data could be stale is if there's
202 * been an intervening call to setgroups(), and this
203 * isn't likely in the case of a shell. paranoia
208 /* Implement special cases from POSIX.2, section 4.62.4 */
213 return *argv[1] == '\0';
215 if (argv[1][0] == '!' && argv[1][1] == '\0') {
216 return *argv[2] != '\0';
220 if (argv[1][0] != '!' || argv[1][1] != '\0') {
221 if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) {
228 if (argv[1][0] == '!' && argv[1][1] == '\0') {
229 if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) {
238 res = !oexpr(t_lex(*t_wp));
240 if (*t_wp != NULL && *++t_wp != NULL) {
241 bb_error_msg("%s: unknown operand", *t_wp);
247 static void syntax(const char *op, const char *msg)
250 bb_error_msg("%s: %s", op, msg);
252 bb_error_msg("%s", msg);
257 static arith_t oexpr(enum token n)
262 if (t_lex(*++t_wp) == BOR) {
263 return oexpr(t_lex(*++t_wp)) || res;
269 static arith_t aexpr(enum token n)
274 if (t_lex(*++t_wp) == BAND)
275 return aexpr(t_lex(*++t_wp)) && res;
280 static arith_t nexpr(enum token n)
283 return !nexpr(t_lex(*++t_wp));
287 static arith_t primary(enum token n)
292 syntax(NULL, "argument expected");
295 res = oexpr(t_lex(*++t_wp));
296 if (t_lex(*++t_wp) != RPAREN)
297 syntax(NULL, "closing paren expected");
300 if (t_wp_op && t_wp_op->op_type == UNOP) {
301 /* unary expression */
303 syntax(t_wp_op->op_text, "argument expected");
306 return strlen(*t_wp) == 0;
308 return strlen(*t_wp) != 0;
310 return isatty(getn(*t_wp));
312 return filstat(*t_wp, n);
316 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
320 return strlen(*t_wp) > 0;
323 static int binop(void)
325 const char *opnd1, *opnd2;
326 struct t_op const *op;
329 (void) t_lex(*++t_wp);
332 if ((opnd2 = *++t_wp) == (char *) 0)
333 syntax(op->op_text, "argument expected");
335 switch (op->op_num) {
337 return strcmp(opnd1, opnd2) == 0;
339 return strcmp(opnd1, opnd2) != 0;
341 return strcmp(opnd1, opnd2) < 0;
343 return strcmp(opnd1, opnd2) > 0;
345 return getn(opnd1) == getn(opnd2);
347 return getn(opnd1) != getn(opnd2);
349 return getn(opnd1) >= getn(opnd2);
351 return getn(opnd1) > getn(opnd2);
353 return getn(opnd1) <= getn(opnd2);
355 return getn(opnd1) < getn(opnd2);
357 return newerf(opnd1, opnd2);
359 return olderf(opnd1, opnd2);
361 return equalf(opnd1, opnd2);
367 static int filstat(char *nm, enum token mode)
372 if (mode == FILSYM) {
374 if (lstat(nm, &s) == 0) {
382 if (stat(nm, &s) != 0)
387 return test_eaccess(nm, R_OK) == 0;
389 return test_eaccess(nm, W_OK) == 0;
391 return test_eaccess(nm, X_OK) == 0;
430 return s.st_size > 0L;
432 return s.st_uid == geteuid();
434 return s.st_gid == getegid();
440 return ((s.st_mode & S_IFMT) == i);
443 return ((s.st_mode & i) != 0);
446 static enum token t_lex(char *s)
448 struct t_op const *op = ops;
451 t_wp_op = (struct t_op *) 0;
454 while (op->op_text) {
455 if (strcmp(s, op->op_text) == 0) {
461 t_wp_op = (struct t_op *) 0;
465 /* atoi with error detection */
466 static arith_t getn(const char *s)
469 #ifdef CONFIG_FEATURE_TEST_64
476 #ifdef CONFIG_FEATURE_TEST_64
477 r = strtoll(s, &p, 10);
479 r = strtol(s, &p, 10);
483 syntax(s, "out of range");
485 /* p = bb_skip_whitespace(p); avoid const warning */
486 if (*(bb_skip_whitespace(p)))
487 syntax(s, "bad number");
492 static int newerf(const char *f1, const char *f2)
496 return (stat(f1, &b1) == 0 &&
497 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
500 static int olderf(const char *f1, const char *f2)
504 return (stat(f1, &b1) == 0 &&
505 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
508 static int equalf(const char *f1, const char *f2)
512 return (stat(f1, &b1) == 0 &&
513 stat(f2, &b2) == 0 &&
514 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
517 /* Do the same thing access(2) does, but use the effective uid and gid,
518 and don't make the mistake of telling root that any file is
520 static int test_eaccess(char *path, int mode)
523 unsigned int euid = geteuid();
525 if (stat(path, &st) < 0)
529 /* Root can read or write any file. */
533 /* Root can execute any file that has any one of the execute
535 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
539 if (st.st_uid == euid) /* owner */
541 else if (is_a_group_member(st.st_gid))
544 if (st.st_mode & mode)
550 static void initialize_group_array(void)
552 ngroups = getgroups(0, NULL);
553 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
554 getgroups(ngroups, group_array);
557 /* Return non-zero if GID is one that we have in our groups list. */
558 static int is_a_group_member(gid_t gid)
562 /* Short-circuit if possible, maybe saving a call to getgroups(). */
563 if (gid == getgid() || gid == getegid())
567 initialize_group_array();
569 /* Search through the list looking for GID. */
570 for (i = 0; i < ngroups; i++)
571 if (gid == group_array[i])
578 /* applet entry point */
580 int test_main(int argc, char **argv)
582 exit(bb_test(argc, argv));