test.c 21 KB

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