blob: 70b392f3870cfd5ed1a6e9cd4512f5880e0b0a0b [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
Rob Landleyea224be2006-06-18 20:20:07 +000023#include "busybox.h"
Paul Fox6ab03782006-06-08 21:37:26 +000024#include <setjmp.h>
Erik Andersen13456d12000-03-16 08:09:57 +000025
Denis Vlasenko99912ca2007-04-10 15:43:37 +000026/* This is a NOEXEC applet. Be very careful! */
27
28
Erik Andersen13456d12000-03-16 08:09:57 +000029/* test(1) accepts the following grammar:
30 oexpr ::= aexpr | aexpr "-o" oexpr ;
31 aexpr ::= nexpr | nexpr "-a" aexpr ;
32 nexpr ::= primary | "!" primary
Paul Fox6ab03782006-06-08 21:37:26 +000033 primary ::= unary-operator operand
Erik Andersen13456d12000-03-16 08:09:57 +000034 | operand binary-operator operand
35 | operand
36 | "(" oexpr ")"
37 ;
38 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
39 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
40
Mike Frysinger75ac42b2005-04-14 02:49:22 +000041 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
Erik Andersen13456d12000-03-16 08:09:57 +000042 "-nt"|"-ot"|"-ef";
43 operand ::= <any legal UNIX file name>
44*/
45
46enum token {
47 EOI,
48 FILRD,
49 FILWR,
50 FILEX,
51 FILEXIST,
52 FILREG,
53 FILDIR,
54 FILCDEV,
55 FILBDEV,
56 FILFIFO,
57 FILSOCK,
58 FILSYM,
59 FILGZ,
60 FILTT,
61 FILSUID,
62 FILSGID,
63 FILSTCK,
64 FILNT,
65 FILOT,
66 FILEQ,
67 FILUID,
68 FILGID,
69 STREZ,
70 STRNZ,
71 STREQ,
72 STRNE,
73 STRLT,
74 STRGT,
75 INTEQ,
76 INTNE,
77 INTGE,
78 INTGT,
79 INTLE,
80 INTLT,
81 UNOT,
82 BAND,
83 BOR,
84 LPAREN,
85 RPAREN,
86 OPERAND
87};
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000088#define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
89#define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
90#define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
91#define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
92#define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
93#define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
Erik Andersen13456d12000-03-16 08:09:57 +000094enum token_types {
95 UNOP,
96 BINOP,
97 BUNOP,
98 BBINOP,
99 PAREN
100};
101
Eric Andersen92d23242001-03-19 23:49:41 +0000102static const struct t_op {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000103 char op_text[4];
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000104 unsigned char op_num, op_type;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000105} ops[] = {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000106 { "-r", FILRD , UNOP },
107 { "-w", FILWR , UNOP },
108 { "-x", FILEX , UNOP },
109 { "-e", FILEXIST, UNOP },
110 { "-f", FILREG , UNOP },
111 { "-d", FILDIR , UNOP },
112 { "-c", FILCDEV , UNOP },
113 { "-b", FILBDEV , UNOP },
114 { "-p", FILFIFO , UNOP },
115 { "-u", FILSUID , UNOP },
116 { "-g", FILSGID , UNOP },
117 { "-k", FILSTCK , UNOP },
118 { "-s", FILGZ , UNOP },
119 { "-t", FILTT , UNOP },
120 { "-z", STREZ , UNOP },
121 { "-n", STRNZ , UNOP },
122 { "-h", FILSYM , UNOP }, /* for backwards compat */
123
124 { "-O" , FILUID , UNOP },
125 { "-G" , FILGID , UNOP },
126 { "-L" , FILSYM , UNOP },
127 { "-S" , FILSOCK, UNOP },
128 { "=" , STREQ , BINOP },
129 { "==" , STREQ , BINOP },
130 { "!=" , STRNE , BINOP },
131 { "<" , STRLT , BINOP },
132 { ">" , STRGT , BINOP },
133 { "-eq", INTEQ , BINOP },
134 { "-ne", INTNE , BINOP },
135 { "-ge", INTGE , BINOP },
136 { "-gt", INTGT , BINOP },
137 { "-le", INTLE , BINOP },
138 { "-lt", INTLT , BINOP },
139 { "-nt", FILNT , BINOP },
140 { "-ot", FILOT , BINOP },
141 { "-ef", FILEQ , BINOP },
142 { "!" , UNOT , BUNOP },
143 { "-a" , BAND , BBINOP },
144 { "-o" , BOR , BBINOP },
145 { "(" , LPAREN , PAREN },
146 { ")" , RPAREN , PAREN },
Erik Andersen13456d12000-03-16 08:09:57 +0000147};
148
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000149enum { NUM_OPS = sizeof(ops) / sizeof(ops[0]) };
150
151#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000152typedef int64_t arith_t;
153#else
154typedef int arith_t;
155#endif
156
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000157/* Cannot eliminate these static data (do the G trick)
158 * because of bb_test usage from other applets */
Matt Kraaie6e81832000-12-30 07:46:23 +0000159static char **t_wp;
160static struct t_op const *t_wp_op;
Rob Landleyecb29572006-08-22 23:40:28 +0000161static gid_t *group_array;
Erik Andersen13456d12000-03-16 08:09:57 +0000162static int ngroups;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000163static jmp_buf leaving;
Erik Andersen13456d12000-03-16 08:09:57 +0000164
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000165static enum token t_lex(char *s);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000166static arith_t oexpr(enum token n);
167static arith_t aexpr(enum token n);
168static arith_t nexpr(enum token n);
Eric Andersen74400cc2001-10-18 04:11:39 +0000169static int binop(void);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000170static arith_t primary(enum token n);
Eric Andersen74400cc2001-10-18 04:11:39 +0000171static int filstat(char *nm, enum token mode);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000172static arith_t getn(const char *s);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000173/* UNUSED
Eric Andersen74400cc2001-10-18 04:11:39 +0000174static int newerf(const char *f1, const char *f2);
175static int olderf(const char *f1, const char *f2);
176static int equalf(const char *f1, const char *f2);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000177*/
Eric Andersen74400cc2001-10-18 04:11:39 +0000178static int test_eaccess(char *path, int mode);
179static int is_a_group_member(gid_t gid);
180static void initialize_group_array(void);
Erik Andersen13456d12000-03-16 08:09:57 +0000181
Paul Fox6ab03782006-06-08 21:37:26 +0000182int bb_test(int argc, char **argv)
Erik Andersen13456d12000-03-16 08:09:57 +0000183{
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000184 int res;
Erik Andersen13456d12000-03-16 08:09:57 +0000185
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000186 if (LONE_CHAR(argv[0], '[')) {
187 --argc;
188 if (NOT_LONE_CHAR(argv[argc], ']')) {
Paul Fox6ab03782006-06-08 21:37:26 +0000189 bb_error_msg("missing ]");
190 return 2;
191 }
192 argv[argc] = NULL;
193 } else if (strcmp(argv[0], "[[") == 0) {
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000194 --argc;
195 if (strcmp(argv[argc], "]]")) {
Paul Fox6ab03782006-06-08 21:37:26 +0000196 bb_error_msg("missing ]]");
197 return 2;
198 }
Erik Andersen13456d12000-03-16 08:09:57 +0000199 argv[argc] = NULL;
200 }
Paul Fox6ab03782006-06-08 21:37:26 +0000201
202 res = setjmp(leaving);
203 if (res)
204 return res;
205
206 /* resetting ngroups is probably unnecessary. it will
207 * force a new call to getgroups(), which prevents using
208 * group data fetched during a previous call. but the
209 * only way the group data could be stale is if there's
210 * been an intervening call to setgroups(), and this
211 * isn't likely in the case of a shell. paranoia
212 * prevails...
213 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000214 ngroups = 0;
Paul Fox6ab03782006-06-08 21:37:26 +0000215
Erik Andersen13456d12000-03-16 08:09:57 +0000216 /* Implement special cases from POSIX.2, section 4.62.4 */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000217 if (argc == 1)
Paul Fox6ab03782006-06-08 21:37:26 +0000218 return 1;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000219 if (argc == 2)
Paul Fox6ab03782006-06-08 21:37:26 +0000220 return *argv[1] == '\0';
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000221//assert(argc);
222 if (LONE_CHAR(argv[1], '!')) {
223 bool _off;
224 if (argc == 3)
Paul Fox6ab03782006-06-08 21:37:26 +0000225 return *argv[2] != '\0';
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000226 _off = argc - 4;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000227 t_lex(argv[2 + _off]);
228 if (t_wp_op && t_wp_op->op_type == BINOP) {
229 t_wp = &argv[1 + _off];
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000230 return binop() == 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000231 }
Erik Andersen13456d12000-03-16 08:09:57 +0000232 }
Erik Andersen13456d12000-03-16 08:09:57 +0000233 t_wp = &argv[1];
234 res = !oexpr(t_lex(*t_wp));
235
Paul Fox6ab03782006-06-08 21:37:26 +0000236 if (*t_wp != NULL && *++t_wp != NULL) {
237 bb_error_msg("%s: unknown operand", *t_wp);
238 return 2;
239 }
240 return res;
241}
Erik Andersen13456d12000-03-16 08:09:57 +0000242
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000243static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
Paul Fox6ab03782006-06-08 21:37:26 +0000244static void syntax(const char *op, const char *msg)
245{
246 if (op && *op) {
247 bb_error_msg("%s: %s", op, msg);
248 } else {
249 bb_error_msg("%s", msg);
250 }
251 longjmp(leaving, 2);
Erik Andersen13456d12000-03-16 08:09:57 +0000252}
253
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000254static arith_t oexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000255{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000256 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000257
258 res = aexpr(n);
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000259 if (t_lex(*++t_wp) == BOR) {
Erik Andersen13456d12000-03-16 08:09:57 +0000260 return oexpr(t_lex(*++t_wp)) || res;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000261 }
Erik Andersen13456d12000-03-16 08:09:57 +0000262 t_wp--;
263 return res;
264}
265
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000266static arith_t aexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000267{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000268 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000269
270 res = nexpr(n);
271 if (t_lex(*++t_wp) == BAND)
272 return aexpr(t_lex(*++t_wp)) && res;
273 t_wp--;
274 return res;
275}
276
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000277static arith_t nexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000278{
279 if (n == UNOT)
280 return !nexpr(t_lex(*++t_wp));
281 return primary(n);
282}
283
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000284static arith_t primary(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000285{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000286 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000287
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000288 if (n == EOI) {
Paul Fox6ab03782006-06-08 21:37:26 +0000289 syntax(NULL, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000290 }
Erik Andersen13456d12000-03-16 08:09:57 +0000291 if (n == LPAREN) {
292 res = oexpr(t_lex(*++t_wp));
293 if (t_lex(*++t_wp) != RPAREN)
Paul Fox6ab03782006-06-08 21:37:26 +0000294 syntax(NULL, "closing paren expected");
Erik Andersen13456d12000-03-16 08:09:57 +0000295 return res;
296 }
297 if (t_wp_op && t_wp_op->op_type == UNOP) {
298 /* unary expression */
299 if (*++t_wp == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000300 syntax(t_wp_op->op_text, "argument expected");
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000301 if (n == STREZ)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000302 return t_wp[0][0] == '\0';
303 if (n == STRNZ)
304 return t_wp[0][0] != '\0';
305 if (n == FILTT)
Erik Andersen13456d12000-03-16 08:09:57 +0000306 return isatty(getn(*t_wp));
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000307 return filstat(*t_wp, n);
Erik Andersen13456d12000-03-16 08:09:57 +0000308 }
309
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000310 t_lex(t_wp[1]);
311 if (t_wp_op && t_wp_op->op_type == BINOP) {
Erik Andersen13456d12000-03-16 08:09:57 +0000312 return binop();
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000313 }
Erik Andersen13456d12000-03-16 08:09:57 +0000314
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000315 return t_wp[0][0] != '\0';
Erik Andersen13456d12000-03-16 08:09:57 +0000316}
317
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000318static int binop(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000319{
320 const char *opnd1, *opnd2;
321 struct t_op const *op;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000322 smallint val1, val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000323
324 opnd1 = *t_wp;
325 (void) t_lex(*++t_wp);
326 op = t_wp_op;
327
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000328 opnd2 = *++t_wp;
329 if (opnd2 == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000330 syntax(op->op_text, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000331
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000332 if (is_int_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000333 val1 = getn(opnd1);
334 val2 = getn(opnd2);
335 if (op->op_num == INTEQ)
336 return val1 == val2;
337 if (op->op_num == INTNE)
338 return val1 != val2;
339 if (op->op_num == INTGE)
340 return val1 >= val2;
341 if (op->op_num == INTGT)
342 return val1 > val2;
343 if (op->op_num == INTLE)
344 return val1 <= val2;
345 if (op->op_num == INTLT)
346 return val1 < val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000347 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000348 if (is_str_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000349 val1 = strcmp(opnd1, opnd2);
350 if (op->op_num == STREQ)
351 return val1 == 0;
352 if (op->op_num == STRNE)
353 return val1 != 0;
354 if (op->op_num == STRLT)
355 return val1 < 0;
356 if (op->op_num == STRGT)
357 return val1 > 0;
358 }
359 /* We are sure that these three are by now the only binops we didn't check
360 * yet, so we do not check if the class is correct:
361 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000362/* if (is_file_op(op->op_num)) */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000363 {
364 struct stat b1, b2;
365
366 if (!(!stat(opnd1, &b1) && !stat(opnd2, &b2)))
367 return 0; /* false, since stat failed */
368 if (op->op_num == FILNT)
369 return b1.st_mtime > b2.st_mtime;
370 if (op->op_num == FILOT)
371 return b1.st_mtime < b2.st_mtime;
372 if (op->op_num == FILEQ)
373 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
374 }
375 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000376}
377
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000378static int filstat(char *nm, enum token mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000379{
380 struct stat s;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000381 int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000382
383 if (mode == FILSYM) {
384#ifdef S_IFLNK
385 if (lstat(nm, &s) == 0) {
386 i = S_IFLNK;
387 goto filetype;
388 }
389#endif
390 return 0;
391 }
392
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000393 if (stat(nm, &s) != 0)
Erik Andersen13456d12000-03-16 08:09:57 +0000394 return 0;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000395 if (mode == FILEXIST)
Erik Andersen13456d12000-03-16 08:09:57 +0000396 return 1;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000397 if (is_file_access(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000398 if (mode == FILRD)
399 i = R_OK;
400 if (mode == FILWR)
401 i = W_OK;
402 if (mode == FILEX)
403 i = X_OK;
404 return test_eaccess(nm, i) == 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000405 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000406 if (is_file_type(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000407 if (mode == FILREG)
408 i = S_IFREG;
409 if (mode == FILDIR)
410 i = S_IFDIR;
411 if (mode == FILCDEV)
412 i = S_IFCHR;
413 if (mode == FILBDEV)
414 i = S_IFBLK;
415 if (mode == FILFIFO) {
416#ifdef S_IFIFO
417 i = S_IFIFO;
418#else
419 return 0;
420#endif
421 }
422 if (mode == FILSOCK) {
423#ifdef S_IFSOCK
424 i = S_IFSOCK;
425#else
426 return 0;
427#endif
428 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000429 filetype:
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000430 return ((s.st_mode & S_IFMT) == i);
431 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000432 if (is_file_bit(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000433 if (mode == FILSUID)
434 i = S_ISUID;
435 if (mode == FILSGID)
436 i = S_ISGID;
437 if (mode == FILSTCK)
438 i = S_ISVTX;
439 return ((s.st_mode & i) != 0);
440 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000441 if (mode == FILGZ)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000442 return s.st_size > 0L;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000443 if (mode == FILUID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000444 return s.st_uid == geteuid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000445 if (mode == FILGID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000446 return s.st_gid == getegid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000447 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000448}
449
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000450static enum token t_lex(char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000451{
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000452 const struct t_op *op;
Erik Andersen13456d12000-03-16 08:09:57 +0000453
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000454 t_wp_op = NULL;
455 if (s == NULL) {
Erik Andersen13456d12000-03-16 08:09:57 +0000456 return EOI;
457 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000458
459 op = ops;
460 do {
Erik Andersen13456d12000-03-16 08:09:57 +0000461 if (strcmp(s, op->op_text) == 0) {
462 t_wp_op = op;
463 return op->op_num;
464 }
465 op++;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000466 } while (op < ops + NUM_OPS);
467
Erik Andersen13456d12000-03-16 08:09:57 +0000468 return OPERAND;
469}
470
471/* atoi with error detection */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000472//XXX: FIXME: duplicate of existing libbb function?
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000473static arith_t getn(const char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000474{
475 char *p;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000476#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000477 long long r;
478#else
Erik Andersen13456d12000-03-16 08:09:57 +0000479 long r;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000480#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000481
482 errno = 0;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000483#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000484 r = strtoll(s, &p, 10);
485#else
Erik Andersen13456d12000-03-16 08:09:57 +0000486 r = strtol(s, &p, 10);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000487#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000488
489 if (errno != 0)
Paul Fox6ab03782006-06-08 21:37:26 +0000490 syntax(s, "out of range");
Erik Andersen13456d12000-03-16 08:09:57 +0000491
Rob Landleyea224be2006-06-18 20:20:07 +0000492 if (*(skip_whitespace(p)))
Paul Fox6ab03782006-06-08 21:37:26 +0000493 syntax(s, "bad number");
Erik Andersen13456d12000-03-16 08:09:57 +0000494
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000495 return r;
Erik Andersen13456d12000-03-16 08:09:57 +0000496}
497
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000498/* UNUSED
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000499static int newerf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000500{
501 struct stat b1, b2;
502
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000503 return (stat(f1, &b1) == 0 &&
504 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000505}
506
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000507static int olderf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000508{
509 struct stat b1, b2;
510
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000511 return (stat(f1, &b1) == 0 &&
512 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000513}
514
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000515static int equalf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000516{
517 struct stat b1, b2;
518
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000519 return (stat(f1, &b1) == 0 &&
520 stat(f2, &b2) == 0 &&
521 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
Erik Andersen13456d12000-03-16 08:09:57 +0000522}
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000523*/
Erik Andersen13456d12000-03-16 08:09:57 +0000524
525/* Do the same thing access(2) does, but use the effective uid and gid,
526 and don't make the mistake of telling root that any file is
527 executable. */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000528static int test_eaccess(char *path, int mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000529{
530 struct stat st;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000531 unsigned int euid = geteuid();
Erik Andersen13456d12000-03-16 08:09:57 +0000532
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000533 if (stat(path, &st) < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000534 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000535
536 if (euid == 0) {
537 /* Root can read or write any file. */
538 if (mode != X_OK)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000539 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000540
541 /* Root can execute any file that has any one of the execute
542 bits set. */
543 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000544 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000545 }
546
Paul Fox6ab03782006-06-08 21:37:26 +0000547 if (st.st_uid == euid) /* owner */
Erik Andersen13456d12000-03-16 08:09:57 +0000548 mode <<= 6;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000549 else if (is_a_group_member(st.st_gid))
Erik Andersen13456d12000-03-16 08:09:57 +0000550 mode <<= 3;
551
552 if (st.st_mode & mode)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000553 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000554
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000555 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000556}
557
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000558static void initialize_group_array(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000559{
560 ngroups = getgroups(0, NULL);
Rob Landleyecb29572006-08-22 23:40:28 +0000561 if (ngroups > 0) {
562 group_array = xmalloc(ngroups * sizeof(gid_t));
563 getgroups(ngroups, group_array);
564 }
Erik Andersen13456d12000-03-16 08:09:57 +0000565}
566
567/* Return non-zero if GID is one that we have in our groups list. */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000568//XXX: FIXME: duplicate of existing libbb function?
569// see toplevel TODO file:
570// possible code duplication ingroup() and is_a_group_member()
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000571static int is_a_group_member(gid_t gid)
Erik Andersen13456d12000-03-16 08:09:57 +0000572{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000573 int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000574
575 /* Short-circuit if possible, maybe saving a call to getgroups(). */
576 if (gid == getgid() || gid == getegid())
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000577 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000578
579 if (ngroups == 0)
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000580 initialize_group_array();
Erik Andersen13456d12000-03-16 08:09:57 +0000581
582 /* Search through the list looking for GID. */
583 for (i = 0; i < ngroups; i++)
584 if (gid == group_array[i])
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000585 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000586
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000587 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000588}
Paul Fox6ab03782006-06-08 21:37:26 +0000589
590
591/* applet entry point */
592
Denis Vlasenko06af2162007-02-03 17:28:39 +0000593int test_main(int argc, char **argv);
Paul Fox6ab03782006-06-08 21:37:26 +0000594int test_main(int argc, char **argv)
595{
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000596 return bb_test(argc, argv);
Paul Fox6ab03782006-06-08 21:37:26 +0000597}