test.c 20 KB

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