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."
29 /* test(1) accepts the following grammar:
30 oexpr ::= aexpr | aexpr "-o" oexpr ;
31 aexpr ::= nexpr | nexpr "-a" aexpr ;
32 nexpr ::= primary | "!" primary
33 primary ::= unary-operator operand
34 | operand binary-operator operand
38 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
39 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
41 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
43 operand ::= <any legal UNIX file name>
97 static const struct t_op {
99 short op_num, op_type;
102 "-r", FILRD, UNOP}, {
103 "-w", FILWR, UNOP}, {
104 "-x", FILEX, UNOP}, {
105 "-e", FILEXIST, UNOP}, {
106 "-f", FILREG, UNOP}, {
107 "-d", FILDIR, UNOP}, {
108 "-c", FILCDEV, UNOP}, {
109 "-b", FILBDEV, UNOP}, {
110 "-p", FILFIFO, UNOP}, {
111 "-u", FILSUID, UNOP}, {
112 "-g", FILSGID, UNOP}, {
113 "-k", FILSTCK, UNOP}, {
114 "-s", FILGZ, UNOP}, {
115 "-t", FILTT, UNOP}, {
116 "-z", STREZ, UNOP}, {
117 "-n", STRNZ, UNOP}, {
118 "-h", FILSYM, UNOP}, /* for backwards compat */
120 "-O", FILUID, UNOP}, {
121 "-G", FILGID, UNOP}, {
122 "-L", FILSYM, UNOP}, {
123 "-S", FILSOCK, UNOP}, {
124 "=", STREQ, BINOP}, {
125 "==", STREQ, BINOP}, {
126 "!=", STRNE, BINOP}, {
127 "<", STRLT, BINOP}, {
128 ">", STRGT, BINOP}, {
129 "-eq", INTEQ, BINOP}, {
130 "-ne", INTNE, BINOP}, {
131 "-ge", INTGE, BINOP}, {
132 "-gt", INTGT, BINOP}, {
133 "-le", INTLE, BINOP}, {
134 "-lt", INTLT, BINOP}, {
135 "-nt", FILNT, BINOP}, {
136 "-ot", FILOT, BINOP}, {
137 "-ef", FILEQ, BINOP}, {
139 "-a", BAND, BBINOP}, {
140 "-o", BOR, BBINOP}, {
141 "(", LPAREN, PAREN}, {
142 ")", RPAREN, PAREN}, {
146 #ifdef CONFIG_FEATURE_TEST_64
147 typedef int64_t arith_t;
153 static struct t_op const *t_wp_op;
154 static gid_t *group_array;
157 static enum token t_lex(char *s);
158 static arith_t oexpr(enum token n);
159 static arith_t aexpr(enum token n);
160 static arith_t nexpr(enum token n);
161 static int binop(void);
162 static arith_t primary(enum token n);
163 static int filstat(char *nm, enum token mode);
164 static arith_t getn(const char *s);
165 static int newerf(const char *f1, const char *f2);
166 static int olderf(const char *f1, const char *f2);
167 static int equalf(const char *f1, const char *f2);
168 static int test_eaccess(char *path, int mode);
169 static int is_a_group_member(gid_t gid);
170 static void initialize_group_array(void);
172 static jmp_buf leaving;
174 int bb_test(int argc, char **argv)
178 if (LONE_CHAR(argv[0], '[')) {
180 if (NOT_LONE_CHAR(argv[argc], ']')) {
181 bb_error_msg("missing ]");
185 } 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 if (*(skip_whitespace(p)))
486 syntax(s, "bad number");
491 static int newerf(const char *f1, const char *f2)
495 return (stat(f1, &b1) == 0 &&
496 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
499 static int olderf(const char *f1, const char *f2)
503 return (stat(f1, &b1) == 0 &&
504 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
507 static int equalf(const char *f1, const char *f2)
511 return (stat(f1, &b1) == 0 &&
512 stat(f2, &b2) == 0 &&
513 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
516 /* Do the same thing access(2) does, but use the effective uid and gid,
517 and don't make the mistake of telling root that any file is
519 static int test_eaccess(char *path, int mode)
522 unsigned int euid = geteuid();
524 if (stat(path, &st) < 0)
528 /* Root can read or write any file. */
532 /* Root can execute any file that has any one of the execute
534 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
538 if (st.st_uid == euid) /* owner */
540 else if (is_a_group_member(st.st_gid))
543 if (st.st_mode & mode)
549 static void initialize_group_array(void)
551 ngroups = getgroups(0, NULL);
553 group_array = xmalloc(ngroups * sizeof(gid_t));
554 getgroups(ngroups, group_array);
558 /* Return non-zero if GID is one that we have in our groups list. */
559 static int is_a_group_member(gid_t gid)
563 /* Short-circuit if possible, maybe saving a call to getgroups(). */
564 if (gid == getgid() || gid == getegid())
568 initialize_group_array();
570 /* Search through the list looking for GID. */
571 for (i = 0; i < ngroups; i++)
572 if (gid == group_array[i])
579 /* applet entry point */
581 int test_main(int argc, char **argv);
582 int test_main(int argc, char **argv)
584 return bb_test(argc, argv);