xargs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Mini xargs implementation for busybox
  3. * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
  4. *
  5. * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
  6. *
  7. * Special thanks
  8. * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
  9. * - Mike Rendell <michael@cs.mun.ca>
  10. * and David MacKenzie <djm@gnu.ai.mit.edu>.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * xargs is described in the Single Unix Specification v3 at
  27. * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <getopt.h>
  35. #include <errno.h>
  36. #include <fcntl.h>
  37. #include <sys/types.h>
  38. #include <sys/wait.h>
  39. #include "busybox.h"
  40. /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
  41. We try to make it as large as possible. */
  42. #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
  43. #define ARG_MAX sysconf (_SC_ARG_MAX)
  44. #endif
  45. #ifndef ARG_MAX
  46. #define ARG_MAX 470
  47. #endif
  48. #ifdef TEST
  49. # ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  50. # define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  51. # endif
  52. # ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
  53. # define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
  54. # endif
  55. # ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
  56. # define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
  57. # endif
  58. # ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  59. # define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  60. # endif
  61. #endif
  62. /*
  63. This function have special algorithm.
  64. Don`t use fork and include to main!
  65. */
  66. static int xargs_exec(char *const *args)
  67. {
  68. pid_t p;
  69. volatile int exec_errno = 0; /* shared vfork stack */
  70. if ((p = vfork()) >= 0) {
  71. if (p == 0) {
  72. /* vfork -- child */
  73. execvp(args[0], args);
  74. exec_errno = errno; /* set error to shared stack */
  75. _exit(1);
  76. } else {
  77. /* vfork -- parent */
  78. int status;
  79. while (wait(&status) == (pid_t) - 1)
  80. if (errno != EINTR)
  81. break;
  82. if (exec_errno) {
  83. errno = exec_errno;
  84. bb_perror_msg("%s", args[0]);
  85. return exec_errno == ENOENT ? 127 : 126;
  86. } else {
  87. if (WEXITSTATUS(status) == 255) {
  88. bb_error_msg("%s: exited with status 255; aborting", args[0]);
  89. return 124;
  90. }
  91. if (WIFSTOPPED(status)) {
  92. bb_error_msg("%s: stopped by signal %d",
  93. args[0], WSTOPSIG(status));
  94. return 125;
  95. }
  96. if (WIFSIGNALED(status)) {
  97. bb_error_msg("%s: terminated by signal %d",
  98. args[0], WTERMSIG(status));
  99. return 125;
  100. }
  101. if (WEXITSTATUS(status) != 0)
  102. return 123;
  103. return 0;
  104. }
  105. }
  106. } else {
  107. bb_perror_msg_and_die("vfork");
  108. }
  109. }
  110. typedef struct xlist_s {
  111. char *data;
  112. size_t lenght;
  113. struct xlist_s *link;
  114. } xlist_t;
  115. static int eof_stdin_detected;
  116. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  117. #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
  118. || (c) == '\f' || (c) == '\v')
  119. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
  120. static xlist_t *process_stdin(xlist_t * list_arg,
  121. const char *eof_str, size_t mc, char *buf)
  122. {
  123. #define NORM 0
  124. #define QUOTE 1
  125. #define BACKSLASH 2
  126. #define SPACE 4
  127. char *s = NULL; /* start word */
  128. char *p = NULL; /* pointer to end word */
  129. char q = 0; /* quote char */
  130. char state = NORM;
  131. char eof_str_detected = 0;
  132. size_t line_l = 0; /* size loaded args line */
  133. int c; /* current char */
  134. xlist_t *cur;
  135. xlist_t *prev;
  136. for (prev = cur = list_arg; cur; cur = cur->link) {
  137. line_l += cur->lenght; /* previous allocated */
  138. if (prev != cur)
  139. prev = prev->link;
  140. }
  141. while (!eof_stdin_detected) {
  142. c = getchar();
  143. if (c == EOF) {
  144. eof_stdin_detected++;
  145. if (s)
  146. goto unexpected_eof;
  147. break;
  148. }
  149. if (eof_str_detected)
  150. continue;
  151. if (state == BACKSLASH) {
  152. state = NORM;
  153. goto set;
  154. } else if (state == QUOTE) {
  155. if (c == q) {
  156. q = 0;
  157. state = NORM;
  158. } else {
  159. goto set;
  160. }
  161. } else { /* if(state == NORM) */
  162. if (ISSPACE(c)) {
  163. if (s) {
  164. unexpected_eof:
  165. state = SPACE;
  166. c = 0;
  167. goto set;
  168. }
  169. } else {
  170. if (s == NULL)
  171. s = p = buf;
  172. if (c == '\\') {
  173. state = BACKSLASH;
  174. } else if (c == '\'' || c == '"') {
  175. q = c;
  176. state = QUOTE;
  177. } else {
  178. set:
  179. if ((size_t)(p - buf) >= mc)
  180. bb_error_msg_and_die("argument line too long");
  181. *p++ = c;
  182. }
  183. }
  184. }
  185. if (state == SPACE) { /* word's delimiter or EOF detected */
  186. if (q) {
  187. bb_error_msg_and_die("unmatched %s quote",
  188. q == '\'' ? "single" : "double");
  189. }
  190. /* word loaded */
  191. if (eof_str) {
  192. eof_str_detected = strcmp(s, eof_str) == 0;
  193. }
  194. if (!eof_str_detected) {
  195. size_t lenght = (p - buf);
  196. cur = xmalloc(sizeof(xlist_t) + lenght);
  197. cur->data = memcpy(cur + 1, s, lenght);
  198. cur->lenght = lenght;
  199. cur->link = NULL;
  200. if (prev == NULL) {
  201. list_arg = cur;
  202. } else {
  203. prev->link = cur;
  204. }
  205. prev = cur;
  206. line_l += lenght;
  207. if (line_l > mc) {
  208. /* stop memory usage :-) */
  209. break;
  210. }
  211. }
  212. s = NULL;
  213. state = NORM;
  214. }
  215. }
  216. return list_arg;
  217. }
  218. #else
  219. /* The variant does not support single quotes, double quotes or backslash */
  220. static xlist_t *process_stdin(xlist_t * list_arg,
  221. const char *eof_str, size_t mc, char *buf)
  222. {
  223. int c; /* current char */
  224. int eof_str_detected = 0;
  225. char *s = NULL; /* start word */
  226. char *p = NULL; /* pointer to end word */
  227. size_t line_l = 0; /* size loaded args line */
  228. xlist_t *cur;
  229. xlist_t *prev;
  230. for (prev = cur = list_arg; cur; cur = cur->link) {
  231. line_l += cur->lenght; /* previous allocated */
  232. if (prev != cur)
  233. prev = prev->link;
  234. }
  235. while (!eof_stdin_detected) {
  236. c = getchar();
  237. if (c == EOF) {
  238. eof_stdin_detected++;
  239. }
  240. if (eof_str_detected)
  241. continue;
  242. if (c == EOF || ISSPACE(c)) {
  243. if (s == NULL)
  244. continue;
  245. c = EOF;
  246. }
  247. if (s == NULL)
  248. s = p = buf;
  249. if ((p - buf) >= mc)
  250. bb_error_msg_and_die("argument line too long");
  251. *p++ = c == EOF ? 0 : c;
  252. if (c == EOF) { /* word's delimiter or EOF detected */
  253. /* word loaded */
  254. if (eof_str) {
  255. eof_str_detected = strcmp(s, eof_str) == 0;
  256. }
  257. if (!eof_str_detected) {
  258. size_t lenght = (p - buf);
  259. cur = xmalloc(sizeof(xlist_t) + lenght);
  260. cur->data = memcpy(cur + 1, s, lenght);
  261. cur->lenght = lenght;
  262. cur->link = NULL;
  263. if (prev == NULL) {
  264. list_arg = cur;
  265. } else {
  266. prev->link = cur;
  267. }
  268. prev = cur;
  269. line_l += lenght;
  270. if (line_l > mc) {
  271. /* stop memory usage :-) */
  272. break;
  273. }
  274. s = NULL;
  275. }
  276. }
  277. }
  278. return list_arg;
  279. }
  280. #endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */
  281. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  282. /* Prompt the user for a response, and
  283. if the user responds affirmatively, return true;
  284. otherwise, return false. Used "/dev/tty", not stdin. */
  285. static int xargs_ask_confirmation(void)
  286. {
  287. static FILE *tty_stream;
  288. int c, savec;
  289. if (!tty_stream) {
  290. tty_stream = fopen("/dev/tty", "r");
  291. if (!tty_stream)
  292. bb_perror_msg_and_die("/dev/tty");
  293. /* pranoidal security by vodz */
  294. fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC);
  295. }
  296. fputs(" ?...", stderr);
  297. fflush(stderr);
  298. c = savec = getc(tty_stream);
  299. while (c != EOF && c != '\n')
  300. c = getc(tty_stream);
  301. if (savec == 'y' || savec == 'Y')
  302. return 1;
  303. return 0;
  304. }
  305. # define OPT_INC_P 1
  306. #else
  307. # define OPT_INC_P 0
  308. # define xargs_ask_confirmation() 1
  309. #endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */
  310. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
  311. # define OPT_INC_X 1
  312. #else
  313. # define OPT_INC_X 0
  314. #endif
  315. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  316. static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED,
  317. size_t mc, char *buf)
  318. {
  319. int c; /* current char */
  320. char *s = NULL; /* start word */
  321. char *p = NULL; /* pointer to end word */
  322. size_t line_l = 0; /* size loaded args line */
  323. xlist_t *cur;
  324. xlist_t *prev;
  325. for (prev = cur = list_arg; cur; cur = cur->link) {
  326. line_l += cur->lenght; /* previous allocated */
  327. if (prev != cur)
  328. prev = prev->link;
  329. }
  330. while (!eof_stdin_detected) {
  331. c = getchar();
  332. if (c == EOF) {
  333. eof_stdin_detected++;
  334. if (s == NULL)
  335. break;
  336. c = 0;
  337. }
  338. if (s == NULL)
  339. s = p = buf;
  340. if ((size_t)(p - buf) >= mc)
  341. bb_error_msg_and_die("argument line too long");
  342. *p++ = c;
  343. if (c == 0) { /* word's delimiter or EOF detected */
  344. /* word loaded */
  345. size_t lenght = (p - buf);
  346. cur = xmalloc(sizeof(xlist_t) + lenght);
  347. cur->data = memcpy(cur + 1, s, lenght);
  348. cur->lenght = lenght;
  349. cur->link = NULL;
  350. if (prev == NULL) {
  351. list_arg = cur;
  352. } else {
  353. prev->link = cur;
  354. }
  355. prev = cur;
  356. line_l += lenght;
  357. if (line_l > mc) {
  358. /* stop memory usage :-) */
  359. break;
  360. }
  361. s = NULL;
  362. }
  363. }
  364. return list_arg;
  365. }
  366. # define READ_ARGS(l, e, nmc, mc) (*read_args)(l, e, nmc, mc)
  367. # define OPT_INC_0 1 /* future use */
  368. #else
  369. # define OPT_INC_0 0 /* future use */
  370. # define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc)
  371. #endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */
  372. #define OPT_VERBOSE (1<<0)
  373. #define OPT_NO_EMPTY (1<<1)
  374. #define OPT_UPTO_NUMBER (1<<2)
  375. #define OPT_UPTO_SIZE (1<<3)
  376. #define OPT_EOF_STRING (1<<4)
  377. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  378. #define OPT_INTERACTIVE (1<<5)
  379. #else
  380. #define OPT_INTERACTIVE (0) /* require for algorithm &| */
  381. #endif
  382. #define OPT_TERMINATE (1<<(5+OPT_INC_P))
  383. #define OPT_ZEROTERM (1<<(5+OPT_INC_P+OPT_INC_X))
  384. /* next future
  385. #define OPT_NEXT_OTHER (1<<(5+OPT_INC_P+OPT_INC_X+OPT_INC_0))
  386. */
  387. int xargs_main(int argc, char **argv)
  388. {
  389. char **args;
  390. int i, a, n;
  391. xlist_t *list = NULL;
  392. xlist_t *cur;
  393. int child_error = 0;
  394. char *max_args, *max_chars;
  395. int n_max_arg;
  396. size_t n_chars = 0;
  397. long orig_arg_max;
  398. const char *eof_str = "_";
  399. unsigned long opt;
  400. size_t n_max_chars;
  401. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  402. xlist_t *(*read_args) (xlist_t *, const char *, size_t, char *) = process_stdin;
  403. #endif
  404. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  405. bb_opt_complementally = "pt";
  406. #endif
  407. opt = bb_getopt_ulflags(argc, argv, "+trn:s:e::"
  408. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  409. "p"
  410. #endif
  411. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
  412. "x"
  413. #endif
  414. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  415. "0"
  416. #endif
  417. ,&max_args, &max_chars, &eof_str);
  418. a = argc - optind;
  419. argv += optind;
  420. if (a == 0) {
  421. /* default behavior is to echo all the filenames */
  422. *argv = "echo";
  423. a++;
  424. }
  425. orig_arg_max = ARG_MAX;
  426. if (orig_arg_max == -1)
  427. orig_arg_max = LONG_MAX;
  428. orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048. */
  429. if ((opt & OPT_UPTO_SIZE)) {
  430. n_max_chars = bb_xgetularg10_bnd(max_chars, 1, orig_arg_max);
  431. for (i = 0; i < a; i++) {
  432. n_chars += strlen(*argv) + 1;
  433. }
  434. if (n_max_chars < n_chars) {
  435. bb_error_msg_and_die("can not fit single argument within argument list size limit");
  436. }
  437. n_max_chars -= n_chars;
  438. } else {
  439. /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
  440. have it at 1 meg). Things will work fine with a large ARG_MAX but it
  441. will probably hurt the system more than it needs to; an array of this
  442. size is allocated. */
  443. if (orig_arg_max > 20 * 1024)
  444. orig_arg_max = 20 * 1024;
  445. n_max_chars = orig_arg_max;
  446. }
  447. max_chars = xmalloc(n_max_chars);
  448. if ((opt & OPT_UPTO_NUMBER)) {
  449. n_max_arg = bb_xgetularg10_bnd(max_args, 1, INT_MAX);
  450. } else {
  451. n_max_arg = n_max_chars;
  452. }
  453. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  454. if (opt & OPT_ZEROTERM)
  455. read_args = process0_stdin;
  456. #endif
  457. while ((list = READ_ARGS(list, eof_str, n_max_chars, max_chars)) != NULL ||
  458. (opt & OPT_NO_EMPTY) == 0)
  459. {
  460. opt |= OPT_NO_EMPTY;
  461. n = 0;
  462. n_chars = 0;
  463. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
  464. for (cur = list; cur;) {
  465. n_chars += cur->lenght;
  466. n++;
  467. cur = cur->link;
  468. if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
  469. if (opt & OPT_TERMINATE)
  470. bb_error_msg_and_die("argument list too long");
  471. break;
  472. }
  473. }
  474. #else
  475. for (cur = list; cur; cur = cur->link) {
  476. n_chars += cur->lenght;
  477. n++;
  478. if (n_chars > n_max_chars || n == n_max_arg) {
  479. break;
  480. }
  481. }
  482. #endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */
  483. /* allocating pointers for execvp:
  484. a*arg, n*arg from stdin, NULL */
  485. args = xcalloc(n + a + 1, sizeof(char *));
  486. /* Store the command to be executed
  487. (taken from the command line) */
  488. for (i = 0; i < a; i++)
  489. args[i] = argv[i];
  490. /* (taken from stdin) */
  491. for (cur = list; n; cur = cur->link) {
  492. args[i++] = cur->data;
  493. n--;
  494. }
  495. if ((opt & (OPT_INTERACTIVE | OPT_VERBOSE))) {
  496. for (i = 0; args[i]; i++) {
  497. if (i)
  498. fputc(' ', stderr);
  499. fputs(args[i], stderr);
  500. }
  501. if ((opt & OPT_INTERACTIVE) == 0)
  502. fputc('\n', stderr);
  503. }
  504. if ((opt & OPT_INTERACTIVE) == 0 || xargs_ask_confirmation() != 0) {
  505. child_error = xargs_exec(args);
  506. }
  507. /* clean up */
  508. for (i = a; args[i]; i++) {
  509. cur = list;
  510. list = list->link;
  511. free(cur);
  512. }
  513. free(args);
  514. if (child_error > 0 && child_error != 123) {
  515. break;
  516. }
  517. }
  518. #ifdef CONFIG_FEATURE_CLEAN_UP
  519. free(max_chars);
  520. #endif
  521. return child_error;
  522. }
  523. #ifdef TEST
  524. const char *bb_applet_name = "debug stuff usage";
  525. void bb_show_usage(void)
  526. {
  527. fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
  528. bb_applet_name);
  529. exit(1);
  530. }
  531. int main(int argc, char **argv)
  532. {
  533. return xargs_main(argc, argv);
  534. }
  535. #endif /* TEST */