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