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