1 /* vi: set sw=4 ts=4: */
3 * echo 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 <andersee@debian.org> to be used
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Original copyright notice states:
31 * "This program is in the Public Domain."
35 #include <sys/types.h>
43 /* test(1) accepts the following grammar:
44 oexpr ::= aexpr | aexpr "-o" oexpr ;
45 aexpr ::= nexpr | nexpr "-a" aexpr ;
46 nexpr ::= primary | "!" primary
47 primary ::= unary-operator operand
48 | operand binary-operator operand
52 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
53 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
55 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
57 operand ::= <any legal UNIX file name>
113 short op_num, op_type;
118 {"-e", FILEXIST,UNOP},
119 {"-f", FILREG, UNOP},
120 {"-d", FILDIR, UNOP},
121 {"-c", FILCDEV,UNOP},
122 {"-b", FILBDEV,UNOP},
123 {"-p", FILFIFO,UNOP},
124 {"-u", FILSUID,UNOP},
125 {"-g", FILSGID,UNOP},
126 {"-k", FILSTCK,UNOP},
131 {"-h", FILSYM, UNOP}, /* for backwards compat */
132 {"-O", FILUID, UNOP},
133 {"-G", FILGID, UNOP},
134 {"-L", FILSYM, UNOP},
135 {"-S", FILSOCK,UNOP},
137 {"!=", STRNE, BINOP},
140 {"-eq", INTEQ, BINOP},
141 {"-ne", INTNE, BINOP},
142 {"-ge", INTGE, BINOP},
143 {"-gt", INTGT, BINOP},
144 {"-le", INTLE, BINOP},
145 {"-lt", INTLT, BINOP},
146 {"-nt", FILNT, BINOP},
147 {"-ot", FILOT, BINOP},
148 {"-ef", FILEQ, BINOP},
150 {"-a", BAND, BBINOP},
152 {"(", LPAREN, PAREN},
153 {")", RPAREN, PAREN},
158 struct t_op const *t_wp_op;
159 static gid_t *group_array = NULL;
162 static enum token t_lex();
167 static int primary();
168 static int filstat();
173 static void syntax();
174 static int test_eaccess();
175 static int is_a_group_member();
176 static void initialize_group_array();
179 test_main(int argc, char** argv)
183 if (strcmp(argv[0], "[") == 0) {
184 if (strcmp(argv[--argc], "]"))
185 fatalError("missing ]");
189 /* Implement special cases from POSIX.2, section 4.62.4 */
194 exit (*argv[1] == '\0');
196 if (argv[1][0] == '!' && argv[1][1] == '\0') {
197 exit (!(*argv[2] == '\0'));
201 if (argv[1][0] != '!' || argv[1][1] != '\0') {
203 t_wp_op && t_wp_op->op_type == BINOP) {
210 if (argv[1][0] == '!' && argv[1][1] == '\0') {
212 t_wp_op && t_wp_op->op_type == BINOP) {
214 exit (!(binop() == 0));
221 res = !oexpr(t_lex(*t_wp));
223 if (*t_wp != NULL && *++t_wp != NULL)
224 syntax(*t_wp, "unknown operand");
235 fatalError("%s: %s", op, msg);
237 fatalError("%s", msg);
247 if (t_lex(*++t_wp) == BOR)
248 return oexpr(t_lex(*++t_wp)) || res;
260 if (t_lex(*++t_wp) == BAND)
261 return aexpr(t_lex(*++t_wp)) && res;
268 enum token n; /* token */
271 return !nexpr(t_lex(*++t_wp));
282 syntax(NULL, "argument expected");
284 res = oexpr(t_lex(*++t_wp));
285 if (t_lex(*++t_wp) != RPAREN)
286 syntax(NULL, "closing paren expected");
289 if (t_wp_op && t_wp_op->op_type == UNOP) {
290 /* unary expression */
292 syntax(t_wp_op->op_text, "argument expected");
295 return strlen(*t_wp) == 0;
297 return strlen(*t_wp) != 0;
299 return isatty(getn(*t_wp));
301 return filstat(*t_wp, n);
305 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
309 return strlen(*t_wp) > 0;
315 const char *opnd1, *opnd2;
316 struct t_op const *op;
319 (void) t_lex(*++t_wp);
322 if ((opnd2 = *++t_wp) == (char *)0)
323 syntax(op->op_text, "argument expected");
325 switch (op->op_num) {
327 return strcmp(opnd1, opnd2) == 0;
329 return strcmp(opnd1, opnd2) != 0;
331 return strcmp(opnd1, opnd2) < 0;
333 return strcmp(opnd1, opnd2) > 0;
335 return getn(opnd1) == getn(opnd2);
337 return getn(opnd1) != getn(opnd2);
339 return getn(opnd1) >= getn(opnd2);
341 return getn(opnd1) > getn(opnd2);
343 return getn(opnd1) <= getn(opnd2);
345 return getn(opnd1) < getn(opnd2);
347 return newerf (opnd1, opnd2);
349 return olderf (opnd1, opnd2);
351 return equalf (opnd1, opnd2);
365 if (mode == FILSYM) {
367 if (lstat(nm, &s) == 0) {
375 if (stat(nm, &s) != 0)
380 return test_eaccess(nm, R_OK) == 0;
382 return test_eaccess(nm, W_OK) == 0;
384 return test_eaccess(nm, X_OK) == 0;
423 return s.st_size > 0L;
425 return s.st_uid == geteuid();
427 return s.st_gid == getegid();
433 return ((s.st_mode & S_IFMT) == i);
436 return ((s.st_mode & i) != 0);
443 struct t_op const *op = ops;
446 t_wp_op = (struct t_op *)0;
449 while (op->op_text) {
450 if (strcmp(s, op->op_text) == 0) {
456 t_wp_op = (struct t_op *)0;
460 /* atoi with error detection */
469 r = strtol(s, &p, 10);
472 fatalError("%s: out of range", s);
478 fatalError("%s: bad number", s);
489 return (stat (f1, &b1) == 0 &&
490 stat (f2, &b2) == 0 &&
491 b1.st_mtime > b2.st_mtime);
500 return (stat (f1, &b1) == 0 &&
501 stat (f2, &b2) == 0 &&
502 b1.st_mtime < b2.st_mtime);
511 return (stat (f1, &b1) == 0 &&
512 stat (f2, &b2) == 0 &&
513 b1.st_dev == b2.st_dev &&
514 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
521 test_eaccess (path, mode)
526 int euid = geteuid();
528 if (stat (path, &st) < 0)
532 /* Root can read or write any file. */
536 /* Root can execute any file that has any one of the execute
538 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
542 if (st.st_uid == euid) /* owner */
544 else if (is_a_group_member (st.st_gid))
547 if (st.st_mode & mode)
554 initialize_group_array ()
556 ngroups = getgroups(0, NULL);
557 if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
558 fatalError("Out of space");
560 getgroups(ngroups, group_array);
563 /* Return non-zero if GID is one that we have in our groups list. */
565 is_a_group_member (gid)
570 /* Short-circuit if possible, maybe saving a call to getgroups(). */
571 if (gid == getgid() || gid == getegid())
575 initialize_group_array ();
577 /* Search through the list looking for GID. */
578 for (i = 0; i < ngroups; i++)
579 if (gid == group_array[i])