blob: 22dadac0e84ff2065f22a82fba0f2d307011efb4 [file] [log] [blame]
Erik Andersen13456d12000-03-16 08:09:57 +00001/* vi: set sw=4 ts=4: */
2/*
Erik Andersen5e1189e2000-04-15 16:34:54 +00003 * test implementation for busybox
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (c) by a whole pile of folks:
Erik Andersen13456d12000-03-16 08:09:57 +00006 *
Paul Fox6ab03782006-06-08 21:37:26 +00007 * 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
14 * in busybox.
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +000015 * modified by Bernhard Fischer to be useable (i.e. a bit less bloaty).
Erik Andersen13456d12000-03-16 08:09:57 +000016 *
Paul Fox6ab03782006-06-08 21:37:26 +000017 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen13456d12000-03-16 08:09:57 +000018 *
19 * Original copyright notice states:
Paul Fox6ab03782006-06-08 21:37:26 +000020 * "This program is in the Public Domain."
Erik Andersen13456d12000-03-16 08:09:57 +000021 */
22
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000023#include "libbb.h"
Paul Fox6ab03782006-06-08 21:37:26 +000024#include <setjmp.h>
Erik Andersen13456d12000-03-16 08:09:57 +000025
Denis Vlasenko6672c8e2007-11-30 07:29:05 +000026/* This is a NOFORK applet. Be very careful! */
27
28/* test_main() is called from shells, and we need to be extra careful here.
29 * This is true regardless of PREFER_APPLETS and STANDALONE_SHELL
30 * state. */
Denis Vlasenko99912ca2007-04-10 15:43:37 +000031
32
Erik Andersen13456d12000-03-16 08:09:57 +000033/* test(1) accepts the following grammar:
34 oexpr ::= aexpr | aexpr "-o" oexpr ;
35 aexpr ::= nexpr | nexpr "-a" aexpr ;
36 nexpr ::= primary | "!" primary
Paul Fox6ab03782006-06-08 21:37:26 +000037 primary ::= unary-operator operand
Erik Andersen13456d12000-03-16 08:09:57 +000038 | operand binary-operator operand
39 | operand
40 | "(" oexpr ")"
41 ;
42 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
43 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
44
Mike Frysinger75ac42b2005-04-14 02:49:22 +000045 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
Erik Andersen13456d12000-03-16 08:09:57 +000046 "-nt"|"-ot"|"-ef";
47 operand ::= <any legal UNIX file name>
48*/
49
50enum token {
51 EOI,
52 FILRD,
53 FILWR,
54 FILEX,
55 FILEXIST,
56 FILREG,
57 FILDIR,
58 FILCDEV,
59 FILBDEV,
60 FILFIFO,
61 FILSOCK,
62 FILSYM,
63 FILGZ,
64 FILTT,
65 FILSUID,
66 FILSGID,
67 FILSTCK,
68 FILNT,
69 FILOT,
70 FILEQ,
71 FILUID,
72 FILGID,
73 STREZ,
74 STRNZ,
75 STREQ,
76 STRNE,
77 STRLT,
78 STRGT,
79 INTEQ,
80 INTNE,
81 INTGE,
82 INTGT,
83 INTLE,
84 INTLT,
85 UNOT,
86 BAND,
87 BOR,
88 LPAREN,
89 RPAREN,
90 OPERAND
91};
Denis Vlasenko6672c8e2007-11-30 07:29:05 +000092#define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
93#define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
94#define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000095#define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
Denis Vlasenko6672c8e2007-11-30 07:29:05 +000096#define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
97#define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
Erik Andersen13456d12000-03-16 08:09:57 +000098enum token_types {
99 UNOP,
100 BINOP,
101 BUNOP,
102 BBINOP,
103 PAREN
104};
105
Eric Andersen92d23242001-03-19 23:49:41 +0000106static const struct t_op {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000107 char op_text[4];
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000108 unsigned char op_num, op_type;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000109} ops[] = {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000110 { "-r", FILRD , UNOP },
111 { "-w", FILWR , UNOP },
112 { "-x", FILEX , UNOP },
113 { "-e", FILEXIST, UNOP },
114 { "-f", FILREG , UNOP },
115 { "-d", FILDIR , UNOP },
116 { "-c", FILCDEV , UNOP },
117 { "-b", FILBDEV , UNOP },
118 { "-p", FILFIFO , UNOP },
119 { "-u", FILSUID , UNOP },
120 { "-g", FILSGID , UNOP },
121 { "-k", FILSTCK , UNOP },
122 { "-s", FILGZ , UNOP },
123 { "-t", FILTT , UNOP },
124 { "-z", STREZ , UNOP },
125 { "-n", STRNZ , UNOP },
126 { "-h", FILSYM , UNOP }, /* for backwards compat */
127
128 { "-O" , FILUID , UNOP },
129 { "-G" , FILGID , UNOP },
130 { "-L" , FILSYM , UNOP },
131 { "-S" , FILSOCK, UNOP },
132 { "=" , STREQ , BINOP },
133 { "==" , STREQ , BINOP },
134 { "!=" , STRNE , BINOP },
135 { "<" , STRLT , BINOP },
136 { ">" , STRGT , BINOP },
137 { "-eq", INTEQ , BINOP },
138 { "-ne", INTNE , BINOP },
139 { "-ge", INTGE , BINOP },
140 { "-gt", INTGT , BINOP },
141 { "-le", INTLE , BINOP },
142 { "-lt", INTLT , BINOP },
143 { "-nt", FILNT , BINOP },
144 { "-ot", FILOT , BINOP },
145 { "-ef", FILEQ , BINOP },
146 { "!" , UNOT , BUNOP },
147 { "-a" , BAND , BBINOP },
148 { "-o" , BOR , BBINOP },
149 { "(" , LPAREN , PAREN },
150 { ")" , RPAREN , PAREN },
Erik Andersen13456d12000-03-16 08:09:57 +0000151};
152
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000153
154#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000155typedef int64_t arith_t;
156#else
157typedef int arith_t;
158#endif
159
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000160
161/* We try to minimize both static and stack usage. */
162struct statics {
163 char **t_wp;
164 const struct t_op *t_wp_op;
165 gid_t *group_array;
166 int ngroups;
167 jmp_buf leaving;
168};
169
170/* Make it reside in writable memory, yet make compiler understand
171 * that it is not going to change. */
172static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
173
174#define S (*ptr_to_statics)
175#define t_wp (S.t_wp )
176#define t_wp_op (S.t_wp_op )
177#define group_array (S.group_array )
178#define ngroups (S.ngroups )
179#define leaving (S.leaving )
180
181#define INIT_S() do { \
182 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \
183} while (0)
184#define DEINIT_S() do { \
185 free(ptr_to_statics); \
186} while (0)
Erik Andersen13456d12000-03-16 08:09:57 +0000187
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000188static arith_t primary(enum token n);
Erik Andersen13456d12000-03-16 08:09:57 +0000189
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000190static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
Paul Fox6ab03782006-06-08 21:37:26 +0000191static void syntax(const char *op, const char *msg)
192{
193 if (op && *op) {
194 bb_error_msg("%s: %s", op, msg);
195 } else {
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000196 bb_error_msg("%s: %s"+4, msg);
Paul Fox6ab03782006-06-08 21:37:26 +0000197 }
198 longjmp(leaving, 2);
Erik Andersen13456d12000-03-16 08:09:57 +0000199}
200
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000201/* atoi with error detection */
202//XXX: FIXME: duplicate of existing libbb function?
203static arith_t getn(const char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000204{
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000205 char *p;
206#if ENABLE_FEATURE_TEST_64
207 long long r;
208#else
209 long r;
210#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000211
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000212 errno = 0;
213#if ENABLE_FEATURE_TEST_64
214 r = strtoll(s, &p, 10);
215#else
216 r = strtol(s, &p, 10);
217#endif
218
219 if (errno != 0)
220 syntax(s, "out of range");
221
222 if (*(skip_whitespace(p)))
223 syntax(s, "bad number");
224
225 return r;
Erik Andersen13456d12000-03-16 08:09:57 +0000226}
227
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000228/* UNUSED
229static int newerf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000230{
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000231 struct stat b1, b2;
Erik Andersen13456d12000-03-16 08:09:57 +0000232
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000233 return (stat(f1, &b1) == 0 &&
234 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000235}
236
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000237static int olderf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000238{
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000239 struct stat b1, b2;
240
241 return (stat(f1, &b1) == 0 &&
242 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000243}
244
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000245static int equalf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000246{
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000247 struct stat b1, b2;
Erik Andersen13456d12000-03-16 08:09:57 +0000248
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000249 return (stat(f1, &b1) == 0 &&
250 stat(f2, &b2) == 0 &&
251 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
Erik Andersen13456d12000-03-16 08:09:57 +0000252}
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000253*/
254
255
256static enum token t_lex(char *s)
257{
258 const struct t_op *op;
259
260 t_wp_op = NULL;
261 if (s == NULL) {
262 return EOI;
263 }
264
265 op = ops;
266 do {
267 if (strcmp(s, op->op_text) == 0) {
268 t_wp_op = op;
269 return op->op_num;
270 }
271 op++;
272 } while (op < ops + ARRAY_SIZE(ops));
273
274 return OPERAND;
275}
276
Erik Andersen13456d12000-03-16 08:09:57 +0000277
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000278static int binop(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000279{
280 const char *opnd1, *opnd2;
281 struct t_op const *op;
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000282 arith_t val1, val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000283
284 opnd1 = *t_wp;
285 (void) t_lex(*++t_wp);
286 op = t_wp_op;
287
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000288 opnd2 = *++t_wp;
289 if (opnd2 == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000290 syntax(op->op_text, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000291
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000292 if (is_int_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000293 val1 = getn(opnd1);
294 val2 = getn(opnd2);
295 if (op->op_num == INTEQ)
296 return val1 == val2;
297 if (op->op_num == INTNE)
298 return val1 != val2;
299 if (op->op_num == INTGE)
300 return val1 >= val2;
301 if (op->op_num == INTGT)
302 return val1 > val2;
303 if (op->op_num == INTLE)
304 return val1 <= val2;
305 if (op->op_num == INTLT)
306 return val1 < val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000307 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000308 if (is_str_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000309 val1 = strcmp(opnd1, opnd2);
310 if (op->op_num == STREQ)
311 return val1 == 0;
312 if (op->op_num == STRNE)
313 return val1 != 0;
314 if (op->op_num == STRLT)
315 return val1 < 0;
316 if (op->op_num == STRGT)
317 return val1 > 0;
318 }
319 /* We are sure that these three are by now the only binops we didn't check
320 * yet, so we do not check if the class is correct:
321 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000322/* if (is_file_op(op->op_num)) */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000323 {
324 struct stat b1, b2;
325
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000326 if (stat(opnd1, &b1) || stat(opnd2, &b2))
327 return 0; /* false, since at least one stat failed */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000328 if (op->op_num == FILNT)
329 return b1.st_mtime > b2.st_mtime;
330 if (op->op_num == FILOT)
331 return b1.st_mtime < b2.st_mtime;
332 if (op->op_num == FILEQ)
333 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
334 }
335 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000336}
337
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000338
339static void initialize_group_array(void)
340{
341 ngroups = getgroups(0, NULL);
342 if (ngroups > 0) {
343 /* FIXME: ash tries so hard to not die on OOM,
344 * and we spoil it with just one xrealloc here */
345 /* We realloc, because test_main can be entered repeatedly by shell.
346 * Testcase (ash): 'while true; do test -x some_file; done'
347 * and watch top. (some_file must have owner != you) */
348 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
349 getgroups(ngroups, group_array);
350 }
351}
352
353
354/* Return non-zero if GID is one that we have in our groups list. */
355//XXX: FIXME: duplicate of existing libbb function?
356// see toplevel TODO file:
357// possible code duplication ingroup() and is_a_group_member()
358static int is_a_group_member(gid_t gid)
359{
360 int i;
361
362 /* Short-circuit if possible, maybe saving a call to getgroups(). */
363 if (gid == getgid() || gid == getegid())
364 return 1;
365
366 if (ngroups == 0)
367 initialize_group_array();
368
369 /* Search through the list looking for GID. */
370 for (i = 0; i < ngroups; i++)
371 if (gid == group_array[i])
372 return 1;
373
374 return 0;
375}
376
377
378/* Do the same thing access(2) does, but use the effective uid and gid,
379 and don't make the mistake of telling root that any file is
380 executable. */
381static int test_eaccess(char *path, int mode)
382{
383 struct stat st;
384 unsigned int euid = geteuid();
385
386 if (stat(path, &st) < 0)
387 return -1;
388
389 if (euid == 0) {
390 /* Root can read or write any file. */
391 if (mode != X_OK)
392 return 0;
393
394 /* Root can execute any file that has any one of the execute
395 bits set. */
396 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
397 return 0;
398 }
399
400 if (st.st_uid == euid) /* owner */
401 mode <<= 6;
402 else if (is_a_group_member(st.st_gid))
403 mode <<= 3;
404
405 if (st.st_mode & mode)
406 return 0;
407
408 return -1;
409}
410
411
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000412static int filstat(char *nm, enum token mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000413{
414 struct stat s;
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000415 int i = i; /* gcc 3.x thinks it can be used uninitialized */
Erik Andersen13456d12000-03-16 08:09:57 +0000416
417 if (mode == FILSYM) {
418#ifdef S_IFLNK
419 if (lstat(nm, &s) == 0) {
420 i = S_IFLNK;
421 goto filetype;
422 }
423#endif
424 return 0;
425 }
426
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000427 if (stat(nm, &s) != 0)
Erik Andersen13456d12000-03-16 08:09:57 +0000428 return 0;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000429 if (mode == FILEXIST)
Erik Andersen13456d12000-03-16 08:09:57 +0000430 return 1;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000431 if (is_file_access(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000432 if (mode == FILRD)
433 i = R_OK;
434 if (mode == FILWR)
435 i = W_OK;
436 if (mode == FILEX)
437 i = X_OK;
438 return test_eaccess(nm, i) == 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000439 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000440 if (is_file_type(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000441 if (mode == FILREG)
442 i = S_IFREG;
443 if (mode == FILDIR)
444 i = S_IFDIR;
445 if (mode == FILCDEV)
446 i = S_IFCHR;
447 if (mode == FILBDEV)
448 i = S_IFBLK;
449 if (mode == FILFIFO) {
450#ifdef S_IFIFO
451 i = S_IFIFO;
452#else
453 return 0;
454#endif
455 }
456 if (mode == FILSOCK) {
457#ifdef S_IFSOCK
458 i = S_IFSOCK;
459#else
460 return 0;
461#endif
462 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000463 filetype:
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000464 return ((s.st_mode & S_IFMT) == i);
465 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000466 if (is_file_bit(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000467 if (mode == FILSUID)
468 i = S_ISUID;
469 if (mode == FILSGID)
470 i = S_ISGID;
471 if (mode == FILSTCK)
472 i = S_ISVTX;
473 return ((s.st_mode & i) != 0);
474 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000475 if (mode == FILGZ)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000476 return s.st_size > 0L;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000477 if (mode == FILUID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000478 return s.st_uid == geteuid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000479 if (mode == FILGID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000480 return s.st_gid == getegid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000481 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000482}
483
Erik Andersen13456d12000-03-16 08:09:57 +0000484
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000485static arith_t nexpr(enum token n)
486{
487 if (n == UNOT)
488 return !nexpr(t_lex(*++t_wp));
489 return primary(n);
490}
491
492
493static arith_t aexpr(enum token n)
494{
495 arith_t res;
496
497 res = nexpr(n);
498 if (t_lex(*++t_wp) == BAND)
499 return aexpr(t_lex(*++t_wp)) && res;
500 t_wp--;
501 return res;
502}
503
504
505static arith_t oexpr(enum token n)
506{
507 arith_t res;
508
509 res = aexpr(n);
510 if (t_lex(*++t_wp) == BOR) {
511 return oexpr(t_lex(*++t_wp)) || res;
512 }
513 t_wp--;
514 return res;
515}
516
517
518
519static arith_t primary(enum token n)
520{
521 arith_t res;
522
523 if (n == EOI) {
524 syntax(NULL, "argument expected");
525 }
526 if (n == LPAREN) {
527 res = oexpr(t_lex(*++t_wp));
528 if (t_lex(*++t_wp) != RPAREN)
529 syntax(NULL, "closing paren expected");
530 return res;
531 }
532 if (t_wp_op && t_wp_op->op_type == UNOP) {
533 /* unary expression */
534 if (*++t_wp == NULL)
535 syntax(t_wp_op->op_text, "argument expected");
536 if (n == STREZ)
537 return t_wp[0][0] == '\0';
538 if (n == STRNZ)
539 return t_wp[0][0] != '\0';
540 if (n == FILTT)
541 return isatty(getn(*t_wp));
542 return filstat(*t_wp, n);
Erik Andersen13456d12000-03-16 08:09:57 +0000543 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000544
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000545 t_lex(t_wp[1]);
546 if (t_wp_op && t_wp_op->op_type == BINOP) {
547 return binop();
548 }
549
550 return t_wp[0][0] != '\0';
551}
552
553
554int test_main(int argc, char **argv)
555{
556 int res;
557 const char *arg0;
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000558 bool negate = 0;
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000559
560 arg0 = bb_basename(argv[0]);
561 if (arg0[0] == '[') {
562 --argc;
563 if (!arg0[1]) { /* "[" ? */
564 if (NOT_LONE_CHAR(argv[argc], ']')) {
565 bb_error_msg("missing ]");
566 return 2;
567 }
568 } else { /* assuming "[[" */
569 if (strcmp(argv[argc], "]]") != 0) {
570 bb_error_msg("missing ]]");
571 return 2;
572 }
Erik Andersen13456d12000-03-16 08:09:57 +0000573 }
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000574 argv[argc] = NULL;
Erik Andersen13456d12000-03-16 08:09:57 +0000575 }
576
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000577 /* We must do DEINIT_S() prior to returning */
578 INIT_S();
579
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000580 res = setjmp(leaving);
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000581 if (res)
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000582 goto ret;
Erik Andersen13456d12000-03-16 08:09:57 +0000583
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000584 /* resetting ngroups is probably unnecessary. it will
585 * force a new call to getgroups(), which prevents using
586 * group data fetched during a previous call. but the
587 * only way the group data could be stale is if there's
588 * been an intervening call to setgroups(), and this
589 * isn't likely in the case of a shell. paranoia
590 * prevails...
591 */
592 ngroups = 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000593
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000594 //argc--;
595 argv++;
596
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000597 /* Implement special cases from POSIX.2, section 4.62.4 */
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000598 if (!argv[0]) { /* "test" */
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000599 res = 1;
600 goto ret;
601 }
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000602 if (LONE_CHAR(argv[0], '!') && argv[1]) {
603 negate = 1;
604 //argc--;
605 argv++;
606 }
607 if (!argv[1]) { /* "test [!] arg" */
608 res = (*argv[0] == '\0');
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000609 goto ret;
610 }
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000611 if (argv[2] && !argv[3]) {
612 t_lex(argv[1]);
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000613 if (t_wp_op && t_wp_op->op_type == BINOP) {
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000614 /* "test [!] arg1 <binary_op> arg2" */
615 t_wp = &argv[0];
616 res = (binop() == 0);
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000617 goto ret;
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000618 }
619 }
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000620
621 /* Some complex expression. Undo '!' removal */
622 if (negate) {
623 negate = 0;
624 //argc++;
625 argv--;
626 }
627 t_wp = &argv[0];
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000628 res = !oexpr(t_lex(*t_wp));
Erik Andersen13456d12000-03-16 08:09:57 +0000629
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000630 if (*t_wp != NULL && *++t_wp != NULL) {
631 bb_error_msg("%s: unknown operand", *t_wp);
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000632 res = 2;
Bernhard Reutner-Fischera7024572007-11-16 12:20:30 +0000633 }
Denis Vlasenko6672c8e2007-11-30 07:29:05 +0000634 ret:
635 DEINIT_S();
Denis Vlasenko1e2a7e42008-02-09 05:48:42 +0000636 return negate ? !res : res;
Erik Andersen13456d12000-03-16 08:09:57 +0000637}