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 (strcmp(argv[0], "[") == 0) {
179 if (strcmp(argv[--argc], "]")) {
180 bb_error_msg("missing ]");
184 } else if (strcmp(argv[0], "[[") == 0) {
185 if (strcmp(argv[--argc], "]]")) {
186 bb_error_msg("missing ]]");
192 res = setjmp(leaving);
196 /* resetting ngroups is probably unnecessary. it will
197 * force a new call to getgroups(), which prevents using
198 * group data fetched during a previous call. but the
199 * only way the group data could be stale is if there's
200 * been an intervening call to setgroups(), and this
201 * isn't likely in the case of a shell. paranoia
206 /* Implement special cases from POSIX.2, section 4.62.4 */
211 return *argv[1] == '\0';
213 if (argv[1][0] == '!' && argv[1][1] == '\0') {
214 return *argv[2] != '\0';
218 if (argv[1][0] != '!' || argv[1][1] != '\0') {
219 if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) {
226 if (argv[1][0] == '!' && argv[1][1] == '\0') {
227 if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) {
236 res = !oexpr(t_lex(*t_wp));
238 if (*t_wp != NULL && *++t_wp != NULL) {
239 bb_error_msg("%s: unknown operand", *t_wp);
245 static void syntax(const char *op, const char *msg)
248 bb_error_msg("%s: %s", op, msg);
250 bb_error_msg("%s", msg);
255 static arith_t oexpr(enum token n)
260 if (t_lex(*++t_wp) == BOR) {
261 return oexpr(t_lex(*++t_wp)) || res;
267 static arith_t aexpr(enum token n)
272 if (t_lex(*++t_wp) == BAND)
273 return aexpr(t_lex(*++t_wp)) && res;
278 static arith_t nexpr(enum token n)
281 return !nexpr(t_lex(*++t_wp));
285 static arith_t primary(enum token n)
290 syntax(NULL, "argument expected");
293 res = oexpr(t_lex(*++t_wp));
294 if (t_lex(*++t_wp) != RPAREN)
295 syntax(NULL, "closing paren expected");
298 if (t_wp_op && t_wp_op->op_type == UNOP) {
299 /* unary expression */
301 syntax(t_wp_op->op_text, "argument expected");
304 return strlen(*t_wp) == 0;
306 return strlen(*t_wp) != 0;
308 return isatty(getn(*t_wp));
310 return filstat(*t_wp, n);
314 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
318 return strlen(*t_wp) > 0;
321 static int binop(void)
323 const char *opnd1, *opnd2;
324 struct t_op const *op;
327 (void) t_lex(*++t_wp);
330 if ((opnd2 = *++t_wp) == (char *) 0)
331 syntax(op->op_text, "argument expected");
333 switch (op->op_num) {
335 return strcmp(opnd1, opnd2) == 0;
337 return strcmp(opnd1, opnd2) != 0;
339 return strcmp(opnd1, opnd2) < 0;
341 return strcmp(opnd1, opnd2) > 0;
343 return getn(opnd1) == getn(opnd2);
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 newerf(opnd1, opnd2);
357 return olderf(opnd1, opnd2);
359 return equalf(opnd1, opnd2);
365 static int filstat(char *nm, enum token mode)
370 if (mode == FILSYM) {
372 if (lstat(nm, &s) == 0) {
380 if (stat(nm, &s) != 0)
385 return test_eaccess(nm, R_OK) == 0;
387 return test_eaccess(nm, W_OK) == 0;
389 return test_eaccess(nm, X_OK) == 0;
428 return s.st_size > 0L;
430 return s.st_uid == geteuid();
432 return s.st_gid == getegid();
438 return ((s.st_mode & S_IFMT) == i);
441 return ((s.st_mode & i) != 0);
444 static enum token t_lex(char *s)
446 struct t_op const *op = ops;
449 t_wp_op = (struct t_op *) 0;
452 while (op->op_text) {
453 if (strcmp(s, op->op_text) == 0) {
459 t_wp_op = (struct t_op *) 0;
463 /* atoi with error detection */
464 static arith_t getn(const char *s)
467 #ifdef CONFIG_FEATURE_TEST_64
474 #ifdef CONFIG_FEATURE_TEST_64
475 r = strtoll(s, &p, 10);
477 r = strtol(s, &p, 10);
481 syntax(s, "out of range");
483 if (*(skip_whitespace(p)))
484 syntax(s, "bad number");
489 static int newerf(const char *f1, const char *f2)
493 return (stat(f1, &b1) == 0 &&
494 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
497 static int olderf(const char *f1, const char *f2)
501 return (stat(f1, &b1) == 0 &&
502 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
505 static int equalf(const char *f1, const char *f2)
509 return (stat(f1, &b1) == 0 &&
510 stat(f2, &b2) == 0 &&
511 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
514 /* Do the same thing access(2) does, but use the effective uid and gid,
515 and don't make the mistake of telling root that any file is
517 static int test_eaccess(char *path, int mode)
520 unsigned int euid = geteuid();
522 if (stat(path, &st) < 0)
526 /* Root can read or write any file. */
530 /* Root can execute any file that has any one of the execute
532 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
536 if (st.st_uid == euid) /* owner */
538 else if (is_a_group_member(st.st_gid))
541 if (st.st_mode & mode)
547 static void initialize_group_array(void)
549 ngroups = getgroups(0, NULL);
551 group_array = xmalloc(ngroups * sizeof(gid_t));
552 getgroups(ngroups, group_array);
556 /* Return non-zero if GID is one that we have in our groups list. */
557 static int is_a_group_member(gid_t gid)
561 /* Short-circuit if possible, maybe saving a call to getgroups(). */
562 if (gid == getgid() || gid == getegid())
566 initialize_group_array();
568 /* Search through the list looking for GID. */
569 for (i = 0; i < ngroups; i++)
570 if (gid == group_array[i])
577 /* applet entry point */
579 int test_main(int argc, char **argv)
581 exit(bb_test(argc, argv));