Reimplement rename()
[git/pclouds.git] / box / coreutils / test.c
blob8ae5dc05d2d9b0144c18a3d446858d5a4bf6eeb5
1 /* vi: set sw=4 ts=4: */
2 /*
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
14 * in busybox.
15 * modified by Bernhard Fischer to be useable (i.e. a bit less bloaty).
17 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
19 * Original copyright notice states:
20 * "This program is in the Public Domain."
23 #include "libbb.h"
24 #include <setjmp.h>
26 /* This is a NOEXEC applet. Be very careful! */
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
35 | operand
36 | "(" oexpr ")"
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"|
42 "-nt"|"-ot"|"-ef";
43 operand ::= <any legal UNIX file name>
46 enum 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
88 #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)
94 enum token_types {
95 UNOP,
96 BINOP,
97 BUNOP,
98 BBINOP,
99 PAREN
102 static const struct t_op {
103 char op_text[4];
104 unsigned char op_num, op_type;
105 } ops[] = {
106 { "-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 */
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 },
149 enum { NUM_OPS = sizeof(ops) / sizeof(ops[0]) };
151 #if ENABLE_FEATURE_TEST_64
152 typedef int64_t arith_t;
153 #else
154 typedef int arith_t;
155 #endif
157 /* Cannot eliminate these static data (do the G trick)
158 * because of bb_test usage from other applets */
159 static char **t_wp;
160 static struct t_op const *t_wp_op;
161 static gid_t *group_array;
162 static int ngroups;
163 static jmp_buf leaving;
165 static enum token t_lex(char *s);
166 static arith_t oexpr(enum token n);
167 static arith_t aexpr(enum token n);
168 static arith_t nexpr(enum token n);
169 static int binop(void);
170 static arith_t primary(enum token n);
171 static int filstat(char *nm, enum token mode);
172 static arith_t getn(const char *s);
173 /* UNUSED
174 static int newerf(const char *f1, const char *f2);
175 static int olderf(const char *f1, const char *f2);
176 static int equalf(const char *f1, const char *f2);
178 static int test_eaccess(char *path, int mode);
179 static int is_a_group_member(gid_t gid);
180 static void initialize_group_array(void);
182 int bb_test(int argc, char **argv)
184 int res;
185 char *arg0;
186 bool _off;
188 arg0 = strrchr(argv[0], '/');
189 if (!arg0++) arg0 = argv[0];
190 if (arg0[0] == '[') {
191 --argc;
192 if (!arg0[1]) { /* "[" ? */
193 if (NOT_LONE_CHAR(argv[argc], ']')) {
194 bb_error_msg("missing ]");
195 return 2;
197 } else { /* assuming "[[" */
198 if (strcmp(argv[argc], "]]") != 0) {
199 bb_error_msg("missing ]]");
200 return 2;
203 argv[argc] = NULL;
206 res = setjmp(leaving);
207 if (res)
208 return res;
210 /* resetting ngroups is probably unnecessary. it will
211 * force a new call to getgroups(), which prevents using
212 * group data fetched during a previous call. but the
213 * only way the group data could be stale is if there's
214 * been an intervening call to setgroups(), and this
215 * isn't likely in the case of a shell. paranoia
216 * prevails...
218 ngroups = 0;
220 /* Implement special cases from POSIX.2, section 4.62.4 */
221 if (argc == 1)
222 return 1;
223 if (argc == 2)
224 return *argv[1] == '\0';
225 //assert(argc);
226 /* remember if we saw argc==4 which wants *no* '!' test */
227 _off = argc - 4;
228 if (_off ?
229 (LONE_CHAR(argv[1], '!'))
230 : (argv[1][0] != '!' || argv[1][1] != '\0'))
232 if (argc == 3)
233 return *argv[2] != '\0';
235 t_lex(argv[2 + _off]);
236 if (t_wp_op && t_wp_op->op_type == BINOP) {
237 t_wp = &argv[1 + _off];
238 return binop() == _off;
241 t_wp = &argv[1];
242 res = !oexpr(t_lex(*t_wp));
244 if (*t_wp != NULL && *++t_wp != NULL) {
245 bb_error_msg("%s: unknown operand", *t_wp);
246 return 2;
248 return res;
251 static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
252 static void syntax(const char *op, const char *msg)
254 if (op && *op) {
255 bb_error_msg("%s: %s", op, msg);
256 } else {
257 bb_error_msg("%s: %s"+4, msg);
259 longjmp(leaving, 2);
262 static arith_t oexpr(enum token n)
264 arith_t res;
266 res = aexpr(n);
267 if (t_lex(*++t_wp) == BOR) {
268 return oexpr(t_lex(*++t_wp)) || res;
270 t_wp--;
271 return res;
274 static arith_t aexpr(enum token n)
276 arith_t res;
278 res = nexpr(n);
279 if (t_lex(*++t_wp) == BAND)
280 return aexpr(t_lex(*++t_wp)) && res;
281 t_wp--;
282 return res;
285 static arith_t nexpr(enum token n)
287 if (n == UNOT)
288 return !nexpr(t_lex(*++t_wp));
289 return primary(n);
292 static arith_t primary(enum token n)
294 arith_t res;
296 if (n == EOI) {
297 syntax(NULL, "argument expected");
299 if (n == LPAREN) {
300 res = oexpr(t_lex(*++t_wp));
301 if (t_lex(*++t_wp) != RPAREN)
302 syntax(NULL, "closing paren expected");
303 return res;
305 if (t_wp_op && t_wp_op->op_type == UNOP) {
306 /* unary expression */
307 if (*++t_wp == NULL)
308 syntax(t_wp_op->op_text, "argument expected");
309 if (n == STREZ)
310 return t_wp[0][0] == '\0';
311 if (n == STRNZ)
312 return t_wp[0][0] != '\0';
313 if (n == FILTT)
314 return isatty(getn(*t_wp));
315 return filstat(*t_wp, n);
318 t_lex(t_wp[1]);
319 if (t_wp_op && t_wp_op->op_type == BINOP) {
320 return binop();
323 return t_wp[0][0] != '\0';
326 static int binop(void)
328 const char *opnd1, *opnd2;
329 struct t_op const *op;
330 arith_t val1, val2;
332 opnd1 = *t_wp;
333 (void) t_lex(*++t_wp);
334 op = t_wp_op;
336 opnd2 = *++t_wp;
337 if (opnd2 == NULL)
338 syntax(op->op_text, "argument expected");
340 if (is_int_op(op->op_num)) {
341 val1 = getn(opnd1);
342 val2 = getn(opnd2);
343 if (op->op_num == INTEQ)
344 return val1 == val2;
345 if (op->op_num == INTNE)
346 return val1 != val2;
347 if (op->op_num == INTGE)
348 return val1 >= val2;
349 if (op->op_num == INTGT)
350 return val1 > val2;
351 if (op->op_num == INTLE)
352 return val1 <= val2;
353 if (op->op_num == INTLT)
354 return val1 < val2;
356 if (is_str_op(op->op_num)) {
357 val1 = strcmp(opnd1, opnd2);
358 if (op->op_num == STREQ)
359 return val1 == 0;
360 if (op->op_num == STRNE)
361 return val1 != 0;
362 if (op->op_num == STRLT)
363 return val1 < 0;
364 if (op->op_num == STRGT)
365 return val1 > 0;
367 /* We are sure that these three are by now the only binops we didn't check
368 * yet, so we do not check if the class is correct:
370 /* if (is_file_op(op->op_num)) */
372 struct stat b1, b2;
374 if (stat(opnd1, &b1) || stat(opnd2, &b2))
375 return 0; /* false, since at least one stat failed */
376 if (op->op_num == FILNT)
377 return b1.st_mtime > b2.st_mtime;
378 if (op->op_num == FILOT)
379 return b1.st_mtime < b2.st_mtime;
380 if (op->op_num == FILEQ)
381 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
383 return 1; /* NOTREACHED */
386 static int filstat(char *nm, enum token mode)
388 struct stat s;
389 int i = i; /* gcc 3.x thinks it can be used uninitialized */
391 if (mode == FILSYM) {
392 #ifdef S_IFLNK
393 if (lstat(nm, &s) == 0) {
394 i = S_IFLNK;
395 goto filetype;
397 #endif
398 return 0;
401 #ifdef __MINGW32__
402 if (mode == FILEX) {
403 int len = strlen(nm), ret;
404 char *exepath;
405 if (len >= 4 && !strcmp(nm+len-4,".exe"))
406 exepath = nm;
407 else {
408 exepath = malloc(len+5);
409 memcpy(exepath, nm, len);
410 memcpy(exepath+len, ".exe", 5);
412 ret = stat(exepath, &s);
413 if (exepath != nm)
414 free(exepath);
415 return ret >= 0;
417 #endif
419 if (stat(nm, &s) != 0)
420 return 0;
421 if (mode == FILEXIST)
422 return 1;
423 if (is_file_access(mode)) {
424 if (mode == FILRD)
425 i = R_OK;
426 if (mode == FILWR)
427 i = W_OK;
428 if (mode == FILEX)
429 i = X_OK;
430 return test_eaccess(nm, i) == 0;
432 if (is_file_type(mode)) {
433 if (mode == FILREG)
434 i = S_IFREG;
435 if (mode == FILDIR)
436 i = S_IFDIR;
437 if (mode == FILCDEV)
438 i = S_IFCHR;
439 if (mode == FILBDEV)
440 i = S_IFBLK;
441 if (mode == FILFIFO) {
442 #ifdef S_IFIFO
443 i = S_IFIFO;
444 #else
445 return 0;
446 #endif
448 if (mode == FILSOCK) {
449 #ifdef S_IFSOCK
450 i = S_IFSOCK;
451 #else
452 return 0;
453 #endif
455 filetype:
456 return ((s.st_mode & S_IFMT) == i);
458 if (is_file_bit(mode)) {
459 if (mode == FILSUID)
460 i = S_ISUID;
461 if (mode == FILSGID)
462 i = S_ISGID;
463 if (mode == FILSTCK)
464 i = S_ISVTX;
465 return ((s.st_mode & i) != 0);
467 if (mode == FILGZ)
468 return s.st_size > 0L;
469 #ifndef __MINGW32__
470 if (mode == FILUID)
471 return s.st_uid == geteuid();
472 if (mode == FILGID)
473 return s.st_gid == getegid();
474 #endif
475 return 1; /* NOTREACHED */
478 static enum token t_lex(char *s)
480 const struct t_op *op;
482 t_wp_op = NULL;
483 if (s == NULL) {
484 return EOI;
487 op = ops;
488 do {
489 if (strcmp(s, op->op_text) == 0) {
490 t_wp_op = op;
491 return op->op_num;
493 op++;
494 } while (op < ops + NUM_OPS);
496 return OPERAND;
499 /* atoi with error detection */
500 //XXX: FIXME: duplicate of existing libbb function?
501 static arith_t getn(const char *s)
503 char *p;
504 #if ENABLE_FEATURE_TEST_64
505 long long r;
506 #else
507 long r;
508 #endif
510 errno = 0;
511 #if ENABLE_FEATURE_TEST_64
512 r = strtoll(s, &p, 10);
513 #else
514 r = strtol(s, &p, 10);
515 #endif
517 if (errno != 0)
518 syntax(s, "out of range");
520 if (*(skip_whitespace(p)))
521 syntax(s, "bad number");
523 return r;
526 /* UNUSED
527 static int newerf(const char *f1, const char *f2)
529 struct stat b1, b2;
531 return (stat(f1, &b1) == 0 &&
532 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
535 static int olderf(const char *f1, const char *f2)
537 struct stat b1, b2;
539 return (stat(f1, &b1) == 0 &&
540 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
543 static int equalf(const char *f1, const char *f2)
545 struct stat b1, b2;
547 return (stat(f1, &b1) == 0 &&
548 stat(f2, &b2) == 0 &&
549 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
553 /* Do the same thing access(2) does, but use the effective uid and gid,
554 and don't make the mistake of telling root that any file is
555 executable. */
556 static int test_eaccess(char *path, int mode)
558 struct stat st;
559 #ifndef __MINGW32__
560 unsigned int euid = geteuid();
561 #endif
563 if (stat(path, &st) < 0)
564 return -1;
566 #ifndef __MINGW32__
567 if (euid == 0) {
568 /* Root can read or write any file. */
569 if (mode != X_OK)
570 return 0;
572 /* Root can execute any file that has any one of the execute
573 bits set. */
574 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
575 return 0;
578 if (st.st_uid == euid) /* owner */
579 mode <<= 6;
580 else if (is_a_group_member(st.st_gid))
581 mode <<= 3;
582 #endif
584 if (st.st_mode & mode)
585 return 0;
587 return -1;
590 #ifndef __MINGW32__
591 static void initialize_group_array(void)
593 ngroups = getgroups(0, NULL);
594 if (ngroups > 0) {
595 /* FIXME: ash tries so hard to not die on OOM,
596 * and we spoil it with just one xrealloc here */
597 /* We realloc, because bb_test can be entered repeatedly by shell.
598 * Testcase (ash): 'while true; do test -x some_file; done'
599 * and watch top. (some_file must have owner != you) */
600 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
601 getgroups(ngroups, group_array);
605 /* Return non-zero if GID is one that we have in our groups list. */
606 //XXX: FIXME: duplicate of existing libbb function?
607 // see toplevel TODO file:
608 // possible code duplication ingroup() and is_a_group_member()
609 static int is_a_group_member(gid_t gid)
611 int i;
613 /* Short-circuit if possible, maybe saving a call to getgroups(). */
614 if (gid == getgid() || gid == getegid())
615 return 1;
617 if (ngroups == 0)
618 initialize_group_array();
620 /* Search through the list looking for GID. */
621 for (i = 0; i < ngroups; i++)
622 if (gid == group_array[i])
623 return 1;
625 return 0;
627 #endif
630 /* applet entry point */
632 int test_main(int argc, char **argv);
633 int test_main(int argc, char **argv)
635 return bb_test(argc, argv);