Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 3 | * test implementation for busybox |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (c) by a whole pile of folks: |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 6 | * |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 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. |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 15 | * modified by Bernhard Reutner-Fischer to be useable (i.e. a bit less bloaty). |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 16 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 17 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 18 | * |
| 19 | * Original copyright notice states: |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 20 | * "This program is in the Public Domain." |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 21 | */ |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 22 | |
| 23 | //kbuild:lib-$(CONFIG_TEST) += test.o test_ptr_hack.o |
| 24 | //kbuild:lib-$(CONFIG_ASH) += test.o test_ptr_hack.o |
| 25 | //kbuild:lib-$(CONFIG_HUSH) += test.o test_ptr_hack.o |
| 26 | |
| 27 | //config:config TEST |
| 28 | //config: bool "test" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 29 | //config: default y |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 30 | //config: help |
| 31 | //config: test is used to check file types and compare values, |
| 32 | //config: returning an appropriate exit code. The bash shell |
| 33 | //config: has test built in, ash can build it in optionally. |
| 34 | //config: |
| 35 | //config:config FEATURE_TEST_64 |
| 36 | //config: bool "Extend test to 64 bit" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 37 | //config: default y |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 38 | //config: depends on TEST || ASH_BUILTIN_TEST || HUSH |
| 39 | //config: help |
| 40 | //config: Enable 64-bit support in test. |
| 41 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 42 | /* "test --help" does not print help (POSIX compat), only "[ --help" does. |
| 43 | * We display "<applet> EXPRESSION ]" here (not "<applet> EXPRESSION") |
| 44 | * Unfortunately, it screws up generated BusyBox.html. TODO. */ |
| 45 | //usage:#define test_trivial_usage |
| 46 | //usage: "EXPRESSION ]" |
| 47 | //usage:#define test_full_usage "\n\n" |
| 48 | //usage: "Check file types, compare values etc. Return a 0/1 exit code\n" |
| 49 | //usage: "depending on logical value of EXPRESSION" |
| 50 | //usage: |
| 51 | //usage:#define test_example_usage |
| 52 | //usage: "$ test 1 -eq 2\n" |
| 53 | //usage: "$ echo $?\n" |
| 54 | //usage: "1\n" |
| 55 | //usage: "$ test 1 -eq 1\n" |
| 56 | //usage: "$ echo $?\n" |
| 57 | //usage: "0\n" |
| 58 | //usage: "$ [ -d /etc ]\n" |
| 59 | //usage: "$ echo $?\n" |
| 60 | //usage: "0\n" |
| 61 | //usage: "$ [ -d /junk ]\n" |
| 62 | //usage: "$ echo $?\n" |
| 63 | //usage: "1\n" |
| 64 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 65 | #include "libbb.h" |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 66 | #include <setjmp.h> |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 68 | /* This is a NOFORK applet. Be very careful! */ |
| 69 | |
| 70 | /* test_main() is called from shells, and we need to be extra careful here. |
Denys Vlasenko | 86cf036 | 2011-03-08 12:44:02 +0100 | [diff] [blame] | 71 | * This is true regardless of PREFER_APPLETS and SH_STANDALONE |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 72 | * state. */ |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 73 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 74 | /* test(1) accepts the following grammar: |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 75 | oexpr ::= aexpr | aexpr "-o" oexpr ; |
| 76 | aexpr ::= nexpr | nexpr "-a" aexpr ; |
| 77 | nexpr ::= primary | "!" primary |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 78 | primary ::= unary-operator operand |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 79 | | operand binary-operator operand |
| 80 | | operand |
| 81 | | "(" oexpr ")" |
| 82 | ; |
| 83 | unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"| |
| 84 | "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S"; |
| 85 | |
Mike Frysinger | 75ac42b | 2005-04-14 02:49:22 +0000 | [diff] [blame] | 86 | binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 87 | "-nt"|"-ot"|"-ef"; |
| 88 | operand ::= <any legal UNIX file name> |
| 89 | */ |
| 90 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 91 | /* TODO: handle [[ expr ]] bashism bash-compatibly. |
| 92 | * [[ ]] is meant to be a "better [ ]", with less weird syntax |
| 93 | * and without the risk of variables and quoted strings misinterpreted |
| 94 | * as operators. |
| 95 | * This will require support from shells - we need to know quote status |
| 96 | * of each parameter (see below). |
| 97 | * |
| 98 | * Word splitting and pathname expansion should NOT be performed: |
| 99 | * # a="a b"; [[ $a = "a b" ]] && echo YES |
| 100 | * YES |
| 101 | * # [[ /bin/m* ]] && echo YES |
| 102 | * YES |
| 103 | * |
| 104 | * =~ should do regexp match |
| 105 | * = and == should do pattern match against right side: |
| 106 | * # [[ *a* == bab ]] && echo YES |
| 107 | * # [[ bab == *a* ]] && echo YES |
| 108 | * YES |
| 109 | * != does the negated == (i.e., also with pattern matching). |
| 110 | * Pattern matching is quotation-sensitive: |
| 111 | * # [[ bab == "b"a* ]] && echo YES |
| 112 | * YES |
| 113 | * # [[ bab == b"a*" ]] && echo YES |
| 114 | * |
| 115 | * Conditional operators such as -f must be unquoted literals to be recognized: |
| 116 | * # [[ -e /bin ]] && echo YES |
| 117 | * YES |
| 118 | * # [[ '-e' /bin ]] && echo YES |
| 119 | * bash: conditional binary operator expected... |
| 120 | * # A='-e'; [[ $A /bin ]] && echo YES |
| 121 | * bash: conditional binary operator expected... |
| 122 | * |
| 123 | * || and && should work as -o and -a work in [ ] |
| 124 | * -a and -o aren't recognized (&& and || are to be used instead) |
| 125 | * ( and ) do not need to be quoted unlike in [ ]: |
| 126 | * # [[ ( abc ) && '' ]] && echo YES |
| 127 | * # [[ ( abc ) || '' ]] && echo YES |
| 128 | * YES |
| 129 | * # [[ ( abc ) -o '' ]] && echo YES |
| 130 | * bash: syntax error in conditional expression... |
| 131 | * |
| 132 | * Apart from the above, [[ expr ]] should work as [ expr ] |
| 133 | */ |
| 134 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 135 | #define TEST_DEBUG 0 |
| 136 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 137 | enum token { |
| 138 | EOI, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 139 | |
| 140 | FILRD, /* file access */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 141 | FILWR, |
| 142 | FILEX, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 143 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 144 | FILEXIST, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 145 | |
| 146 | FILREG, /* file type */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 147 | FILDIR, |
| 148 | FILCDEV, |
| 149 | FILBDEV, |
| 150 | FILFIFO, |
| 151 | FILSOCK, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 152 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 153 | FILSYM, |
| 154 | FILGZ, |
| 155 | FILTT, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 156 | |
| 157 | FILSUID, /* file bit */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 158 | FILSGID, |
| 159 | FILSTCK, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 160 | |
| 161 | FILNT, /* file ops */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 162 | FILOT, |
| 163 | FILEQ, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 164 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 165 | FILUID, |
| 166 | FILGID, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 167 | |
| 168 | STREZ, /* str ops */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 169 | STRNZ, |
| 170 | STREQ, |
| 171 | STRNE, |
| 172 | STRLT, |
| 173 | STRGT, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 174 | |
| 175 | INTEQ, /* int ops */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 176 | INTNE, |
| 177 | INTGE, |
| 178 | INTGT, |
| 179 | INTLE, |
| 180 | INTLT, |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 181 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 182 | UNOT, |
| 183 | BAND, |
| 184 | BOR, |
| 185 | LPAREN, |
| 186 | RPAREN, |
| 187 | OPERAND |
| 188 | }; |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 189 | #define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5) |
| 190 | #define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5) |
| 191 | #define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2) |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 192 | #define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2) |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 193 | #define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5) |
| 194 | #define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2) |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 195 | |
| 196 | #if TEST_DEBUG |
| 197 | int depth; |
| 198 | #define nest_msg(...) do { \ |
| 199 | depth++; \ |
| 200 | fprintf(stderr, "%*s", depth*2, ""); \ |
| 201 | fprintf(stderr, __VA_ARGS__); \ |
| 202 | } while (0) |
| 203 | #define unnest_msg(...) do { \ |
| 204 | fprintf(stderr, "%*s", depth*2, ""); \ |
| 205 | fprintf(stderr, __VA_ARGS__); \ |
| 206 | depth--; \ |
| 207 | } while (0) |
| 208 | #define dbg_msg(...) do { \ |
| 209 | fprintf(stderr, "%*s", depth*2, ""); \ |
| 210 | fprintf(stderr, __VA_ARGS__); \ |
| 211 | } while (0) |
| 212 | #define unnest_msg_and_return(expr, ...) do { \ |
| 213 | number_t __res = (expr); \ |
| 214 | fprintf(stderr, "%*s", depth*2, ""); \ |
| 215 | fprintf(stderr, __VA_ARGS__, res); \ |
| 216 | depth--; \ |
| 217 | return __res; \ |
| 218 | } while (0) |
| 219 | static const char *const TOKSTR[] = { |
| 220 | "EOI", |
| 221 | "FILRD", |
| 222 | "FILWR", |
| 223 | "FILEX", |
| 224 | "FILEXIST", |
| 225 | "FILREG", |
| 226 | "FILDIR", |
| 227 | "FILCDEV", |
| 228 | "FILBDEV", |
| 229 | "FILFIFO", |
| 230 | "FILSOCK", |
| 231 | "FILSYM", |
| 232 | "FILGZ", |
| 233 | "FILTT", |
| 234 | "FILSUID", |
| 235 | "FILSGID", |
| 236 | "FILSTCK", |
| 237 | "FILNT", |
| 238 | "FILOT", |
| 239 | "FILEQ", |
| 240 | "FILUID", |
| 241 | "FILGID", |
| 242 | "STREZ", |
| 243 | "STRNZ", |
| 244 | "STREQ", |
| 245 | "STRNE", |
| 246 | "STRLT", |
| 247 | "STRGT", |
| 248 | "INTEQ", |
| 249 | "INTNE", |
| 250 | "INTGE", |
| 251 | "INTGT", |
| 252 | "INTLE", |
| 253 | "INTLT", |
| 254 | "UNOT", |
| 255 | "BAND", |
| 256 | "BOR", |
| 257 | "LPAREN", |
| 258 | "RPAREN", |
| 259 | "OPERAND" |
| 260 | }; |
| 261 | #else |
| 262 | #define nest_msg(...) ((void)0) |
| 263 | #define unnest_msg(...) ((void)0) |
| 264 | #define dbg_msg(...) ((void)0) |
| 265 | #define unnest_msg_and_return(expr, ...) return expr |
| 266 | #endif |
| 267 | |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 268 | enum { |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 269 | UNOP, |
| 270 | BINOP, |
| 271 | BUNOP, |
| 272 | BBINOP, |
| 273 | PAREN |
| 274 | }; |
| 275 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 276 | struct operator_t { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 277 | unsigned char op_num, op_type; |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 280 | static const struct operator_t ops_table[] = { |
| 281 | { /* "-r" */ FILRD , UNOP }, |
| 282 | { /* "-w" */ FILWR , UNOP }, |
| 283 | { /* "-x" */ FILEX , UNOP }, |
| 284 | { /* "-e" */ FILEXIST, UNOP }, |
| 285 | { /* "-f" */ FILREG , UNOP }, |
| 286 | { /* "-d" */ FILDIR , UNOP }, |
| 287 | { /* "-c" */ FILCDEV , UNOP }, |
| 288 | { /* "-b" */ FILBDEV , UNOP }, |
| 289 | { /* "-p" */ FILFIFO , UNOP }, |
| 290 | { /* "-u" */ FILSUID , UNOP }, |
| 291 | { /* "-g" */ FILSGID , UNOP }, |
| 292 | { /* "-k" */ FILSTCK , UNOP }, |
| 293 | { /* "-s" */ FILGZ , UNOP }, |
| 294 | { /* "-t" */ FILTT , UNOP }, |
| 295 | { /* "-z" */ STREZ , UNOP }, |
| 296 | { /* "-n" */ STRNZ , UNOP }, |
| 297 | { /* "-h" */ FILSYM , UNOP }, /* for backwards compat */ |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 298 | |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 299 | { /* "-O" */ FILUID , UNOP }, |
| 300 | { /* "-G" */ FILGID , UNOP }, |
| 301 | { /* "-L" */ FILSYM , UNOP }, |
| 302 | { /* "-S" */ FILSOCK , UNOP }, |
| 303 | { /* "=" */ STREQ , BINOP }, |
| 304 | { /* "==" */ STREQ , BINOP }, |
| 305 | { /* "!=" */ STRNE , BINOP }, |
| 306 | { /* "<" */ STRLT , BINOP }, |
| 307 | { /* ">" */ STRGT , BINOP }, |
| 308 | { /* "-eq"*/ INTEQ , BINOP }, |
| 309 | { /* "-ne"*/ INTNE , BINOP }, |
| 310 | { /* "-ge"*/ INTGE , BINOP }, |
| 311 | { /* "-gt"*/ INTGT , BINOP }, |
| 312 | { /* "-le"*/ INTLE , BINOP }, |
| 313 | { /* "-lt"*/ INTLT , BINOP }, |
| 314 | { /* "-nt"*/ FILNT , BINOP }, |
| 315 | { /* "-ot"*/ FILOT , BINOP }, |
| 316 | { /* "-ef"*/ FILEQ , BINOP }, |
| 317 | { /* "!" */ UNOT , BUNOP }, |
| 318 | { /* "-a" */ BAND , BBINOP }, |
| 319 | { /* "-o" */ BOR , BBINOP }, |
| 320 | { /* "(" */ LPAREN , PAREN }, |
| 321 | { /* ")" */ RPAREN , PAREN }, |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 322 | }; |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 323 | /* Please keep these two tables in sync */ |
| 324 | static const char ops_texts[] ALIGN1 = |
| 325 | "-r" "\0" |
| 326 | "-w" "\0" |
| 327 | "-x" "\0" |
| 328 | "-e" "\0" |
| 329 | "-f" "\0" |
| 330 | "-d" "\0" |
| 331 | "-c" "\0" |
| 332 | "-b" "\0" |
| 333 | "-p" "\0" |
| 334 | "-u" "\0" |
| 335 | "-g" "\0" |
| 336 | "-k" "\0" |
| 337 | "-s" "\0" |
| 338 | "-t" "\0" |
| 339 | "-z" "\0" |
| 340 | "-n" "\0" |
| 341 | "-h" "\0" |
| 342 | |
| 343 | "-O" "\0" |
| 344 | "-G" "\0" |
| 345 | "-L" "\0" |
| 346 | "-S" "\0" |
| 347 | "=" "\0" |
| 348 | "==" "\0" |
| 349 | "!=" "\0" |
| 350 | "<" "\0" |
| 351 | ">" "\0" |
| 352 | "-eq" "\0" |
| 353 | "-ne" "\0" |
| 354 | "-ge" "\0" |
| 355 | "-gt" "\0" |
| 356 | "-le" "\0" |
| 357 | "-lt" "\0" |
| 358 | "-nt" "\0" |
| 359 | "-ot" "\0" |
| 360 | "-ef" "\0" |
| 361 | "!" "\0" |
| 362 | "-a" "\0" |
| 363 | "-o" "\0" |
| 364 | "(" "\0" |
| 365 | ")" "\0" |
| 366 | ; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 367 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 368 | |
| 369 | #if ENABLE_FEATURE_TEST_64 |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 370 | typedef int64_t number_t; |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 371 | #else |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 372 | typedef int number_t; |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 373 | #endif |
| 374 | |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 375 | |
| 376 | /* We try to minimize both static and stack usage. */ |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 377 | struct test_statics { |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 378 | char **args; |
| 379 | /* set only by check_operator(), either to bogus struct |
| 380 | * or points to matching operator_t struct. Never NULL. */ |
| 381 | const struct operator_t *last_operator; |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 382 | gid_t *group_array; |
| 383 | int ngroups; |
| 384 | jmp_buf leaving; |
| 385 | }; |
| 386 | |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 387 | /* See test_ptr_hack.c */ |
| 388 | extern struct test_statics *const test_ptr_to_statics; |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 389 | |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 390 | #define S (*test_ptr_to_statics) |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 391 | #define args (S.args ) |
| 392 | #define last_operator (S.last_operator) |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 393 | #define group_array (S.group_array ) |
| 394 | #define ngroups (S.ngroups ) |
| 395 | #define leaving (S.leaving ) |
| 396 | |
| 397 | #define INIT_S() do { \ |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 398 | (*(struct test_statics**)&test_ptr_to_statics) = xzalloc(sizeof(S)); \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 399 | barrier(); \ |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 400 | } while (0) |
| 401 | #define DEINIT_S() do { \ |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 402 | free(test_ptr_to_statics); \ |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 403 | } while (0) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 404 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 405 | static number_t primary(enum token n); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 406 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 407 | static void syntax(const char *op, const char *msg) NORETURN; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 408 | static void syntax(const char *op, const char *msg) |
| 409 | { |
| 410 | if (op && *op) { |
| 411 | bb_error_msg("%s: %s", op, msg); |
| 412 | } else { |
Denis Vlasenko | fe3e177 | 2007-05-27 03:39:50 +0000 | [diff] [blame] | 413 | bb_error_msg("%s: %s"+4, msg); |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 414 | } |
| 415 | longjmp(leaving, 2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 418 | /* atoi with error detection */ |
| 419 | //XXX: FIXME: duplicate of existing libbb function? |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 420 | static number_t getn(const char *s) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 421 | { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 422 | char *p; |
| 423 | #if ENABLE_FEATURE_TEST_64 |
| 424 | long long r; |
| 425 | #else |
| 426 | long r; |
| 427 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 428 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 429 | errno = 0; |
| 430 | #if ENABLE_FEATURE_TEST_64 |
| 431 | r = strtoll(s, &p, 10); |
| 432 | #else |
| 433 | r = strtol(s, &p, 10); |
| 434 | #endif |
| 435 | |
| 436 | if (errno != 0) |
| 437 | syntax(s, "out of range"); |
| 438 | |
Denys Vlasenko | 3e47cfe | 2010-06-03 01:47:04 +0200 | [diff] [blame] | 439 | if (p == s || *(skip_whitespace(p)) != '\0') |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 440 | syntax(s, "bad number"); |
| 441 | |
| 442 | return r; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 445 | /* UNUSED |
| 446 | static int newerf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 447 | { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 448 | struct stat b1, b2; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 449 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 450 | return (stat(f1, &b1) == 0 && |
| 451 | stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 454 | static int olderf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 455 | { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 456 | struct stat b1, b2; |
| 457 | |
| 458 | return (stat(f1, &b1) == 0 && |
| 459 | stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 462 | static int equalf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 463 | { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 464 | struct stat b1, b2; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 465 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 466 | return (stat(f1, &b1) == 0 && |
| 467 | stat(f2, &b2) == 0 && |
| 468 | b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 469 | } |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 470 | */ |
| 471 | |
| 472 | |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 473 | static enum token check_operator(const char *s) |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 474 | { |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 475 | static const struct operator_t no_op = { |
| 476 | .op_num = -1, |
| 477 | .op_type = -1 |
| 478 | }; |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 479 | int n; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 480 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 481 | last_operator = &no_op; |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 482 | if (s == NULL) |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 483 | return EOI; |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 484 | n = index_in_strings(ops_texts, s); |
| 485 | if (n < 0) |
| 486 | return OPERAND; |
| 487 | last_operator = &ops_table[n]; |
| 488 | return ops_table[n].op_num; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 491 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 492 | static int binop(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 493 | { |
| 494 | const char *opnd1, *opnd2; |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 495 | const struct operator_t *op; |
| 496 | number_t val1, val2; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 497 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 498 | opnd1 = *args; |
| 499 | check_operator(*++args); |
| 500 | op = last_operator; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 501 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 502 | opnd2 = *++args; |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 503 | if (opnd2 == NULL) |
Denys Vlasenko | c541a89 | 2009-09-12 22:41:57 +0200 | [diff] [blame] | 504 | syntax(args[-1], "argument expected"); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 505 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 506 | if (is_int_op(op->op_num)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 507 | val1 = getn(opnd1); |
| 508 | val2 = getn(opnd2); |
| 509 | if (op->op_num == INTEQ) |
| 510 | return val1 == val2; |
| 511 | if (op->op_num == INTNE) |
| 512 | return val1 != val2; |
| 513 | if (op->op_num == INTGE) |
| 514 | return val1 >= val2; |
| 515 | if (op->op_num == INTGT) |
| 516 | return val1 > val2; |
| 517 | if (op->op_num == INTLE) |
| 518 | return val1 <= val2; |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 519 | /*if (op->op_num == INTLT)*/ |
| 520 | return val1 < val2; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 521 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 522 | if (is_str_op(op->op_num)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 523 | val1 = strcmp(opnd1, opnd2); |
| 524 | if (op->op_num == STREQ) |
| 525 | return val1 == 0; |
| 526 | if (op->op_num == STRNE) |
| 527 | return val1 != 0; |
| 528 | if (op->op_num == STRLT) |
| 529 | return val1 < 0; |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 530 | /*if (op->op_num == STRGT)*/ |
| 531 | return val1 > 0; |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 532 | } |
| 533 | /* We are sure that these three are by now the only binops we didn't check |
| 534 | * yet, so we do not check if the class is correct: |
| 535 | */ |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 536 | /* if (is_file_op(op->op_num)) */ |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 537 | { |
| 538 | struct stat b1, b2; |
| 539 | |
Denis Vlasenko | fe3e177 | 2007-05-27 03:39:50 +0000 | [diff] [blame] | 540 | if (stat(opnd1, &b1) || stat(opnd2, &b2)) |
| 541 | return 0; /* false, since at least one stat failed */ |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 542 | if (op->op_num == FILNT) |
| 543 | return b1.st_mtime > b2.st_mtime; |
| 544 | if (op->op_num == FILOT) |
| 545 | return b1.st_mtime < b2.st_mtime; |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 546 | /*if (op->op_num == FILEQ)*/ |
| 547 | return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino; |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 548 | } |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 549 | /*return 1; - NOTREACHED */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 552 | |
| 553 | static void initialize_group_array(void) |
| 554 | { |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 555 | int n; |
| 556 | |
| 557 | /* getgroups may be expensive, try to use it only once */ |
| 558 | ngroups = 32; |
| 559 | do { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 560 | /* FIXME: ash tries so hard to not die on OOM, |
| 561 | * and we spoil it with just one xrealloc here */ |
| 562 | /* We realloc, because test_main can be entered repeatedly by shell. |
| 563 | * Testcase (ash): 'while true; do test -x some_file; done' |
| 564 | * and watch top. (some_file must have owner != you) */ |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 565 | n = ngroups; |
| 566 | group_array = xrealloc(group_array, n * sizeof(gid_t)); |
| 567 | ngroups = getgroups(n, group_array); |
| 568 | } while (ngroups > n); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
| 572 | /* Return non-zero if GID is one that we have in our groups list. */ |
| 573 | //XXX: FIXME: duplicate of existing libbb function? |
| 574 | // see toplevel TODO file: |
| 575 | // possible code duplication ingroup() and is_a_group_member() |
| 576 | static int is_a_group_member(gid_t gid) |
| 577 | { |
| 578 | int i; |
| 579 | |
| 580 | /* Short-circuit if possible, maybe saving a call to getgroups(). */ |
| 581 | if (gid == getgid() || gid == getegid()) |
| 582 | return 1; |
| 583 | |
| 584 | if (ngroups == 0) |
| 585 | initialize_group_array(); |
| 586 | |
| 587 | /* Search through the list looking for GID. */ |
| 588 | for (i = 0; i < ngroups; i++) |
| 589 | if (gid == group_array[i]) |
| 590 | return 1; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | /* Do the same thing access(2) does, but use the effective uid and gid, |
| 597 | and don't make the mistake of telling root that any file is |
| 598 | executable. */ |
| 599 | static int test_eaccess(char *path, int mode) |
| 600 | { |
| 601 | struct stat st; |
| 602 | unsigned int euid = geteuid(); |
| 603 | |
| 604 | if (stat(path, &st) < 0) |
| 605 | return -1; |
| 606 | |
| 607 | if (euid == 0) { |
| 608 | /* Root can read or write any file. */ |
| 609 | if (mode != X_OK) |
| 610 | return 0; |
| 611 | |
| 612 | /* Root can execute any file that has any one of the execute |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 613 | * bits set. */ |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 614 | if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | if (st.st_uid == euid) /* owner */ |
| 619 | mode <<= 6; |
| 620 | else if (is_a_group_member(st.st_gid)) |
| 621 | mode <<= 3; |
| 622 | |
| 623 | if (st.st_mode & mode) |
| 624 | return 0; |
| 625 | |
| 626 | return -1; |
| 627 | } |
| 628 | |
| 629 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 630 | static int filstat(char *nm, enum token mode) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 631 | { |
| 632 | struct stat s; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 633 | unsigned i = i; /* gcc 3.x thinks it can be used uninitialized */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 634 | |
| 635 | if (mode == FILSYM) { |
| 636 | #ifdef S_IFLNK |
| 637 | if (lstat(nm, &s) == 0) { |
| 638 | i = S_IFLNK; |
| 639 | goto filetype; |
| 640 | } |
| 641 | #endif |
| 642 | return 0; |
| 643 | } |
| 644 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 645 | if (stat(nm, &s) != 0) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 646 | return 0; |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 647 | if (mode == FILEXIST) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 648 | return 1; |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 649 | if (is_file_access(mode)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 650 | if (mode == FILRD) |
| 651 | i = R_OK; |
| 652 | if (mode == FILWR) |
| 653 | i = W_OK; |
| 654 | if (mode == FILEX) |
| 655 | i = X_OK; |
| 656 | return test_eaccess(nm, i) == 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 657 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 658 | if (is_file_type(mode)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 659 | if (mode == FILREG) |
| 660 | i = S_IFREG; |
| 661 | if (mode == FILDIR) |
| 662 | i = S_IFDIR; |
| 663 | if (mode == FILCDEV) |
| 664 | i = S_IFCHR; |
| 665 | if (mode == FILBDEV) |
| 666 | i = S_IFBLK; |
| 667 | if (mode == FILFIFO) { |
| 668 | #ifdef S_IFIFO |
| 669 | i = S_IFIFO; |
| 670 | #else |
| 671 | return 0; |
| 672 | #endif |
| 673 | } |
| 674 | if (mode == FILSOCK) { |
| 675 | #ifdef S_IFSOCK |
| 676 | i = S_IFSOCK; |
| 677 | #else |
| 678 | return 0; |
| 679 | #endif |
| 680 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 681 | filetype: |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 682 | return ((s.st_mode & S_IFMT) == i); |
| 683 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 684 | if (is_file_bit(mode)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 685 | if (mode == FILSUID) |
| 686 | i = S_ISUID; |
| 687 | if (mode == FILSGID) |
| 688 | i = S_ISGID; |
| 689 | if (mode == FILSTCK) |
| 690 | i = S_ISVTX; |
| 691 | return ((s.st_mode & i) != 0); |
| 692 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 693 | if (mode == FILGZ) |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 694 | return s.st_size > 0L; |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 695 | if (mode == FILUID) |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 696 | return s.st_uid == geteuid(); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 697 | if (mode == FILGID) |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 698 | return s.st_gid == getegid(); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 699 | return 1; /* NOTREACHED */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 702 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 703 | static number_t nexpr(enum token n) |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 704 | { |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 705 | number_t res; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 706 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 707 | nest_msg(">nexpr(%s)\n", TOKSTR[n]); |
| 708 | if (n == UNOT) { |
Denys Vlasenko | d23f64e | 2009-07-17 00:59:26 +0200 | [diff] [blame] | 709 | n = check_operator(*++args); |
| 710 | if (n == EOI) { |
| 711 | /* special case: [ ! ], [ a -a ! ] are valid */ |
| 712 | /* IOW, "! ARG" may miss ARG */ |
Denys Vlasenko | 07fcaab | 2012-03-08 03:50:01 +0100 | [diff] [blame] | 713 | args--; |
| 714 | unnest_msg("<nexpr:1 (!EOI), args:%s(%p)\n", args[0], &args[0]); |
Denys Vlasenko | d23f64e | 2009-07-17 00:59:26 +0200 | [diff] [blame] | 715 | return 1; |
| 716 | } |
| 717 | res = !nexpr(n); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 718 | unnest_msg("<nexpr:%lld\n", res); |
| 719 | return res; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 720 | } |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 721 | res = primary(n); |
| 722 | unnest_msg("<nexpr:%lld\n", res); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 723 | return res; |
| 724 | } |
| 725 | |
| 726 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 727 | static number_t aexpr(enum token n) |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 728 | { |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 729 | number_t res; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 730 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 731 | nest_msg(">aexpr(%s)\n", TOKSTR[n]); |
| 732 | res = nexpr(n); |
Denys Vlasenko | 07fcaab | 2012-03-08 03:50:01 +0100 | [diff] [blame] | 733 | dbg_msg("aexpr: nexpr:%lld, next args:%s(%p)\n", res, args[1], &args[1]); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 734 | if (check_operator(*++args) == BAND) { |
Denys Vlasenko | 07fcaab | 2012-03-08 03:50:01 +0100 | [diff] [blame] | 735 | dbg_msg("aexpr: arg is AND, next args:%s(%p)\n", args[1], &args[1]); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 736 | res = aexpr(check_operator(*++args)) && res; |
| 737 | unnest_msg("<aexpr:%lld\n", res); |
| 738 | return res; |
| 739 | } |
| 740 | args--; |
Denys Vlasenko | 07fcaab | 2012-03-08 03:50:01 +0100 | [diff] [blame] | 741 | unnest_msg("<aexpr:%lld, args:%s(%p)\n", res, args[0], &args[0]); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 742 | return res; |
| 743 | } |
| 744 | |
| 745 | |
| 746 | static number_t oexpr(enum token n) |
| 747 | { |
| 748 | number_t res; |
| 749 | |
| 750 | nest_msg(">oexpr(%s)\n", TOKSTR[n]); |
| 751 | res = aexpr(n); |
Denys Vlasenko | 07fcaab | 2012-03-08 03:50:01 +0100 | [diff] [blame] | 752 | dbg_msg("oexpr: aexpr:%lld, next args:%s(%p)\n", res, args[1], &args[1]); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 753 | if (check_operator(*++args) == BOR) { |
Denys Vlasenko | 07fcaab | 2012-03-08 03:50:01 +0100 | [diff] [blame] | 754 | dbg_msg("oexpr: next arg is OR, next args:%s(%p)\n", args[1], &args[1]); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 755 | res = oexpr(check_operator(*++args)) || res; |
| 756 | unnest_msg("<oexpr:%lld\n", res); |
| 757 | return res; |
| 758 | } |
| 759 | args--; |
Denys Vlasenko | 07fcaab | 2012-03-08 03:50:01 +0100 | [diff] [blame] | 760 | unnest_msg("<oexpr:%lld, args:%s(%p)\n", res, args[0], &args[0]); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 761 | return res; |
| 762 | } |
| 763 | |
| 764 | |
| 765 | static number_t primary(enum token n) |
| 766 | { |
| 767 | #if TEST_DEBUG |
| 768 | number_t res = res; /* for compiler */ |
| 769 | #else |
| 770 | number_t res; |
| 771 | #endif |
| 772 | const struct operator_t *args0_op; |
| 773 | |
| 774 | nest_msg(">primary(%s)\n", TOKSTR[n]); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 775 | if (n == EOI) { |
| 776 | syntax(NULL, "argument expected"); |
| 777 | } |
| 778 | if (n == LPAREN) { |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 779 | res = oexpr(check_operator(*++args)); |
| 780 | if (check_operator(*++args) != RPAREN) |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 781 | syntax(NULL, "closing paren expected"); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 782 | unnest_msg("<primary:%lld\n", res); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 783 | return res; |
| 784 | } |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 785 | |
| 786 | /* coreutils 6.9 checks "is args[1] binop and args[2] exist?" first, |
| 787 | * do the same */ |
| 788 | args0_op = last_operator; |
| 789 | /* last_operator = operator at args[1] */ |
| 790 | if (check_operator(args[1]) != EOI) { /* if args[1] != NULL */ |
| 791 | if (args[2]) { |
| 792 | // coreutils also does this: |
| 793 | // if (args[3] && args[0]="-l" && args[2] is BINOP) |
| 794 | // return binop(1 /* prepended by -l */); |
| 795 | if (last_operator->op_type == BINOP) |
| 796 | unnest_msg_and_return(binop(), "<primary: binop:%lld\n"); |
| 797 | } |
| 798 | } |
| 799 | /* check "is args[0] unop?" second */ |
| 800 | if (args0_op->op_type == UNOP) { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 801 | /* unary expression */ |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 802 | if (args[1] == NULL) |
| 803 | // syntax(args0_op->op_text, "argument expected"); |
| 804 | goto check_emptiness; |
| 805 | args++; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 806 | if (n == STREZ) |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 807 | unnest_msg_and_return(args[0][0] == '\0', "<primary:%lld\n"); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 808 | if (n == STRNZ) |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 809 | unnest_msg_and_return(args[0][0] != '\0', "<primary:%lld\n"); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 810 | if (n == FILTT) |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 811 | unnest_msg_and_return(isatty(getn(*args)), "<primary: isatty(%s)%lld\n", *args); |
| 812 | unnest_msg_and_return(filstat(*args, n), "<primary: filstat(%s):%lld\n", *args); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 813 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 814 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 815 | /*check_operator(args[1]); - already done */ |
| 816 | if (last_operator->op_type == BINOP) { |
| 817 | /* args[2] is known to be NULL, isn't it bound to fail? */ |
| 818 | unnest_msg_and_return(binop(), "<primary:%lld\n"); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 819 | } |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 820 | check_emptiness: |
| 821 | unnest_msg_and_return(args[0][0] != '\0', "<primary:%lld\n"); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | |
| 825 | int test_main(int argc, char **argv) |
| 826 | { |
| 827 | int res; |
| 828 | const char *arg0; |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 829 | // bool negate = 0; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 830 | |
| 831 | arg0 = bb_basename(argv[0]); |
| 832 | if (arg0[0] == '[') { |
| 833 | --argc; |
| 834 | if (!arg0[1]) { /* "[" ? */ |
| 835 | if (NOT_LONE_CHAR(argv[argc], ']')) { |
| 836 | bb_error_msg("missing ]"); |
| 837 | return 2; |
| 838 | } |
| 839 | } else { /* assuming "[[" */ |
| 840 | if (strcmp(argv[argc], "]]") != 0) { |
| 841 | bb_error_msg("missing ]]"); |
| 842 | return 2; |
| 843 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 844 | } |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 845 | argv[argc] = NULL; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 848 | /* We must do DEINIT_S() prior to returning */ |
| 849 | INIT_S(); |
| 850 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 851 | res = setjmp(leaving); |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 852 | if (res) |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 853 | goto ret; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 854 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 855 | /* resetting ngroups is probably unnecessary. it will |
| 856 | * force a new call to getgroups(), which prevents using |
| 857 | * group data fetched during a previous call. but the |
| 858 | * only way the group data could be stale is if there's |
| 859 | * been an intervening call to setgroups(), and this |
| 860 | * isn't likely in the case of a shell. paranoia |
| 861 | * prevails... |
| 862 | */ |
Denys Vlasenko | 16635cc | 2009-06-13 22:49:08 +0200 | [diff] [blame] | 863 | /*ngroups = 0; - done by INIT_S() */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 864 | |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 865 | //argc--; |
| 866 | argv++; |
| 867 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 868 | /* Implement special cases from POSIX.2, section 4.62.4 */ |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 869 | if (!argv[0]) { /* "test" */ |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 870 | res = 1; |
| 871 | goto ret; |
| 872 | } |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 873 | #if 0 |
| 874 | // Now it's fixed in the parser and should not be needed |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 875 | if (LONE_CHAR(argv[0], '!') && argv[1]) { |
| 876 | negate = 1; |
| 877 | //argc--; |
| 878 | argv++; |
| 879 | } |
| 880 | if (!argv[1]) { /* "test [!] arg" */ |
| 881 | res = (*argv[0] == '\0'); |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 882 | goto ret; |
| 883 | } |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 884 | if (argv[2] && !argv[3]) { |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 885 | check_operator(argv[1]); |
| 886 | if (last_operator->op_type == BINOP) { |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 887 | /* "test [!] arg1 <binary_op> arg2" */ |
Denys Vlasenko | d23f64e | 2009-07-17 00:59:26 +0200 | [diff] [blame] | 888 | args = argv; |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 889 | res = (binop() == 0); |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 890 | goto ret; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 893 | |
| 894 | /* Some complex expression. Undo '!' removal */ |
| 895 | if (negate) { |
| 896 | negate = 0; |
| 897 | //argc++; |
| 898 | argv--; |
| 899 | } |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 900 | #endif |
Denys Vlasenko | d23f64e | 2009-07-17 00:59:26 +0200 | [diff] [blame] | 901 | args = argv; |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 902 | res = !oexpr(check_operator(*args)); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 903 | |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 904 | if (*args != NULL && *++args != NULL) { |
Denys Vlasenko | 27c6c00 | 2010-12-20 03:43:20 +0100 | [diff] [blame] | 905 | /* Examples: |
| 906 | * test 3 -lt 5 6 |
| 907 | * test -t 1 2 |
| 908 | */ |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 909 | bb_error_msg("%s: unknown operand", *args); |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 910 | res = 2; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 911 | } |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 912 | ret: |
| 913 | DEINIT_S(); |
Denis Vlasenko | bd28f6b | 2008-07-19 08:15:13 +0000 | [diff] [blame] | 914 | // return negate ? !res : res; |
| 915 | return res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 916 | } |