test.c 21 KB

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