xargs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini xargs implementation for busybox
  4. * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
  5. *
  6. * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
  7. *
  8. * Special thanks
  9. * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
  10. * - Mike Rendell <michael@cs.mun.ca>
  11. * and David MacKenzie <djm@gnu.ai.mit.edu>.
  12. *
  13. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  14. *
  15. * xargs is described in the Single Unix Specification v3 at
  16. * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
  17. *
  18. */
  19. #include "libbb.h"
  20. /* This is a NOEXEC applet. Be very careful! */
  21. /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
  22. We try to make it as large as possible. */
  23. #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
  24. #define ARG_MAX sysconf (_SC_ARG_MAX)
  25. #endif
  26. #ifndef ARG_MAX
  27. #define ARG_MAX 470
  28. #endif
  29. #ifdef TEST
  30. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
  31. # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
  32. # endif
  33. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
  34. # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
  35. # endif
  36. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
  37. # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
  38. # endif
  39. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  40. # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
  41. # endif
  42. #endif
  43. /*
  44. This function has special algorithm.
  45. Don't use fork and include to main!
  46. */
  47. static int xargs_exec(char **args)
  48. {
  49. int status;
  50. status = spawn_and_wait(args);
  51. if (status < 0) {
  52. bb_simple_perror_msg(args[0]);
  53. return errno == ENOENT ? 127 : 126;
  54. }
  55. if (status == 255) {
  56. bb_error_msg("%s: exited with status 255; aborting", args[0]);
  57. return 124;
  58. }
  59. /* Huh? I think we won't see this, ever. We don't wait with WUNTRACED!
  60. if (WIFSTOPPED(status)) {
  61. bb_error_msg("%s: stopped by signal %d",
  62. args[0], WSTOPSIG(status));
  63. return 125;
  64. }
  65. */
  66. if (status >= 1000) {
  67. bb_error_msg("%s: terminated by signal %d",
  68. args[0], status - 1000);
  69. return 125;
  70. }
  71. if (status)
  72. return 123;
  73. return 0;
  74. }
  75. typedef struct xlist_t {
  76. struct xlist_t *link;
  77. size_t length;
  78. char xstr[1];
  79. } xlist_t;
  80. static smallint eof_stdin_detected;
  81. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  82. #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
  83. || (c) == '\f' || (c) == '\v')
  84. #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
  85. static xlist_t *process_stdin(xlist_t *list_arg,
  86. const char *eof_str, size_t mc, char *buf)
  87. {
  88. #define NORM 0
  89. #define QUOTE 1
  90. #define BACKSLASH 2
  91. #define SPACE 4
  92. char *s = NULL; /* start word */
  93. char *p = NULL; /* pointer to end word */
  94. char q = '\0'; /* quote char */
  95. char state = NORM;
  96. char eof_str_detected = 0;
  97. size_t line_l = 0; /* size loaded args line */
  98. int c; /* current char */
  99. xlist_t *cur;
  100. xlist_t *prev;
  101. prev = cur = list_arg;
  102. while (1) {
  103. if (!cur) break;
  104. prev = cur;
  105. line_l += cur->length;
  106. cur = cur->link;
  107. }
  108. while (!eof_stdin_detected) {
  109. c = getchar();
  110. if (c == EOF) {
  111. eof_stdin_detected = 1;
  112. if (s)
  113. goto unexpected_eof;
  114. break;
  115. }
  116. if (eof_str_detected)
  117. continue;
  118. if (state == BACKSLASH) {
  119. state = NORM;
  120. goto set;
  121. } else if (state == QUOTE) {
  122. if (c != q)
  123. goto set;
  124. q = '\0';
  125. state = NORM;
  126. } else { /* if (state == NORM) */
  127. if (ISSPACE(c)) {
  128. if (s) {
  129. unexpected_eof:
  130. state = SPACE;
  131. c = '\0';
  132. goto set;
  133. }
  134. } else {
  135. if (s == NULL)
  136. s = p = buf;
  137. if (c == '\\') {
  138. state = BACKSLASH;
  139. } else if (c == '\'' || c == '"') {
  140. q = c;
  141. state = QUOTE;
  142. } else {
  143. set:
  144. if ((size_t)(p - buf) >= mc)
  145. bb_error_msg_and_die("argument line too long");
  146. *p++ = c;
  147. }
  148. }
  149. }
  150. if (state == SPACE) { /* word's delimiter or EOF detected */
  151. if (q) {
  152. bb_error_msg_and_die("unmatched %s quote",
  153. q == '\'' ? "single" : "double");
  154. }
  155. /* word loaded */
  156. if (eof_str) {
  157. eof_str_detected = (strcmp(s, eof_str) == 0);
  158. }
  159. if (!eof_str_detected) {
  160. size_t length = (p - buf);
  161. /* Dont xzalloc - it can be quite big */
  162. cur = xmalloc(offsetof(xlist_t, xstr) + length);
  163. cur->link = NULL;
  164. cur->length = length;
  165. memcpy(cur->xstr, s, length);
  166. if (prev == NULL) {
  167. list_arg = cur;
  168. } else {
  169. prev->link = cur;
  170. }
  171. prev = cur;
  172. line_l += length;
  173. if (line_l > mc) {
  174. /* stop memory usage :-) */
  175. break;
  176. }
  177. }
  178. s = NULL;
  179. state = NORM;
  180. }
  181. }
  182. return list_arg;
  183. }
  184. #else
  185. /* The variant does not support single quotes, double quotes or backslash */
  186. static xlist_t *process_stdin(xlist_t *list_arg,
  187. const char *eof_str, size_t mc, char *buf)
  188. {
  189. int c; /* current char */
  190. char eof_str_detected = 0;
  191. char *s = NULL; /* start word */
  192. char *p = NULL; /* pointer to end word */
  193. size_t line_l = 0; /* size loaded args line */
  194. xlist_t *cur;
  195. xlist_t *prev;
  196. prev = cur = list_arg;
  197. while (1) {
  198. if (!cur) break;
  199. prev = cur;
  200. line_l += cur->length;
  201. cur = cur->link;
  202. }
  203. while (!eof_stdin_detected) {
  204. c = getchar();
  205. if (c == EOF) {
  206. eof_stdin_detected = 1;
  207. }
  208. if (eof_str_detected)
  209. continue;
  210. if (c == EOF || ISSPACE(c)) {
  211. if (s == NULL)
  212. continue;
  213. c = EOF;
  214. }
  215. if (s == NULL)
  216. s = p = buf;
  217. if ((size_t)(p - buf) >= mc)
  218. bb_error_msg_and_die("argument line too long");
  219. *p++ = (c == EOF ? '\0' : c);
  220. if (c == EOF) { /* word's delimiter or EOF detected */
  221. /* word loaded */
  222. if (eof_str) {
  223. eof_str_detected = (strcmp(s, eof_str) == 0);
  224. }
  225. if (!eof_str_detected) {
  226. size_t length = (p - buf);
  227. /* Dont xzalloc - it can be quite big */
  228. cur = xmalloc(offsetof(xlist_t, xstr) + length);
  229. cur->link = NULL;
  230. cur->length = length;
  231. memcpy(cur->xstr, s, length);
  232. if (prev == NULL) {
  233. list_arg = cur;
  234. } else {
  235. prev->link = cur;
  236. }
  237. prev = cur;
  238. line_l += length;
  239. if (line_l > mc) {
  240. /* stop memory usage :-) */
  241. break;
  242. }
  243. s = NULL;
  244. }
  245. }
  246. }
  247. return list_arg;
  248. }
  249. #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
  250. #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
  251. /* Prompt the user for a response, and
  252. if the user responds affirmatively, return true;
  253. otherwise, return false. Uses "/dev/tty", not stdin. */
  254. static int xargs_ask_confirmation(void)
  255. {
  256. FILE *tty_stream;
  257. int c, savec;
  258. tty_stream = xfopen_for_read(CURRENT_TTY);
  259. fputs(" ?...", stderr);
  260. fflush_all();
  261. c = savec = getc(tty_stream);
  262. while (c != EOF && c != '\n')
  263. c = getc(tty_stream);
  264. fclose(tty_stream);
  265. return (savec == 'y' || savec == 'Y');
  266. }
  267. #else
  268. # define xargs_ask_confirmation() 1
  269. #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
  270. #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  271. static xlist_t *process0_stdin(xlist_t *list_arg,
  272. const char *eof_str UNUSED_PARAM, size_t mc, char *buf)
  273. {
  274. int c; /* current char */
  275. char *s = NULL; /* start word */
  276. char *p = NULL; /* pointer to end word */
  277. size_t line_l = 0; /* size loaded args line */
  278. xlist_t *cur;
  279. xlist_t *prev;
  280. prev = cur = list_arg;
  281. while (1) {
  282. if (!cur) break;
  283. prev = cur;
  284. line_l += cur->length;
  285. cur = cur->link;
  286. }
  287. while (!eof_stdin_detected) {
  288. c = getchar();
  289. if (c == EOF) {
  290. eof_stdin_detected = 1;
  291. if (s == NULL)
  292. break;
  293. c = '\0';
  294. }
  295. if (s == NULL)
  296. s = p = buf;
  297. if ((size_t)(p - buf) >= mc)
  298. bb_error_msg_and_die("argument line too long");
  299. *p++ = c;
  300. if (c == '\0') { /* word's delimiter or EOF detected */
  301. /* word loaded */
  302. size_t length = (p - buf);
  303. /* Dont xzalloc - it can be quite big */
  304. cur = xmalloc(offsetof(xlist_t, xstr) + length);
  305. cur->link = NULL;
  306. cur->length = length;
  307. memcpy(cur->xstr, s, length);
  308. if (prev == NULL) {
  309. list_arg = cur;
  310. } else {
  311. prev->link = cur;
  312. }
  313. prev = cur;
  314. line_l += length;
  315. if (line_l > mc) {
  316. /* stop memory usage :-) */
  317. break;
  318. }
  319. s = NULL;
  320. }
  321. }
  322. return list_arg;
  323. }
  324. #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
  325. /* Correct regardless of combination of CONFIG_xxx */
  326. enum {
  327. OPTBIT_VERBOSE = 0,
  328. OPTBIT_NO_EMPTY,
  329. OPTBIT_UPTO_NUMBER,
  330. OPTBIT_UPTO_SIZE,
  331. OPTBIT_EOF_STRING,
  332. OPTBIT_EOF_STRING1,
  333. IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
  334. IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
  335. IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
  336. OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
  337. OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
  338. OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
  339. OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
  340. OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
  341. OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
  342. OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
  343. OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
  344. OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
  345. };
  346. #define OPTION_STR "+trn:s:e::E:" \
  347. IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
  348. IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
  349. IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
  350. int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  351. int xargs_main(int argc, char **argv)
  352. {
  353. char **args;
  354. int i, n;
  355. xlist_t *list = NULL;
  356. xlist_t *cur;
  357. int child_error = 0;
  358. char *max_args, *max_chars;
  359. int n_max_arg;
  360. size_t n_chars = 0;
  361. long orig_arg_max;
  362. const char *eof_str = NULL;
  363. unsigned opt;
  364. size_t n_max_chars;
  365. #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  366. xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
  367. #else
  368. #define read_args process_stdin
  369. #endif
  370. opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
  371. /* -E ""? You may wonder why not just omit -E?
  372. * This is used for portability:
  373. * old xargs was using "_" as default for -E / -e */
  374. if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
  375. eof_str = NULL;
  376. if (opt & OPT_ZEROTERM)
  377. IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
  378. argv += optind;
  379. argc -= optind;
  380. if (!argc) {
  381. /* default behavior is to echo all the filenames */
  382. *argv = (char*)"echo";
  383. argc++;
  384. }
  385. orig_arg_max = ARG_MAX;
  386. if (orig_arg_max == -1)
  387. orig_arg_max = LONG_MAX;
  388. orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
  389. if (opt & OPT_UPTO_SIZE) {
  390. n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
  391. for (i = 0; i < argc; i++) {
  392. n_chars += strlen(*argv) + 1;
  393. }
  394. if (n_max_chars < n_chars) {
  395. bb_error_msg_and_die("can't fit single argument within argument list size limit");
  396. }
  397. n_max_chars -= n_chars;
  398. } else {
  399. /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
  400. have it at 1 meg). Things will work fine with a large ARG_MAX but it
  401. will probably hurt the system more than it needs to; an array of this
  402. size is allocated. */
  403. if (orig_arg_max > 20 * 1024)
  404. orig_arg_max = 20 * 1024;
  405. n_max_chars = orig_arg_max;
  406. }
  407. max_chars = xmalloc(n_max_chars);
  408. if (opt & OPT_UPTO_NUMBER) {
  409. n_max_arg = xatoul_range(max_args, 1, INT_MAX);
  410. } else {
  411. n_max_arg = n_max_chars;
  412. }
  413. while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
  414. !(opt & OPT_NO_EMPTY))
  415. {
  416. opt |= OPT_NO_EMPTY;
  417. n = 0;
  418. n_chars = 0;
  419. #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
  420. for (cur = list; cur;) {
  421. n_chars += cur->length;
  422. n++;
  423. cur = cur->link;
  424. if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
  425. if (opt & OPT_TERMINATE)
  426. bb_error_msg_and_die("argument list too long");
  427. break;
  428. }
  429. }
  430. #else
  431. for (cur = list; cur; cur = cur->link) {
  432. n_chars += cur->length;
  433. n++;
  434. if (n_chars > n_max_chars || n == n_max_arg) {
  435. break;
  436. }
  437. }
  438. #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
  439. /* allocate pointers for execvp:
  440. argc*arg, n*arg from stdin, NULL */
  441. args = xzalloc((n + argc + 1) * sizeof(char *));
  442. /* store the command to be executed
  443. (taken from the command line) */
  444. for (i = 0; i < argc; i++)
  445. args[i] = argv[i];
  446. /* (taken from stdin) */
  447. for (cur = list; n; cur = cur->link) {
  448. args[i++] = cur->xstr;
  449. n--;
  450. }
  451. if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
  452. for (i = 0; args[i]; i++) {
  453. if (i)
  454. fputc(' ', stderr);
  455. fputs(args[i], stderr);
  456. }
  457. if (!(opt & OPT_INTERACTIVE))
  458. fputc('\n', stderr);
  459. }
  460. if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
  461. child_error = xargs_exec(args);
  462. }
  463. /* clean up */
  464. for (i = argc; args[i]; i++) {
  465. cur = list;
  466. list = list->link;
  467. free(cur);
  468. }
  469. free(args);
  470. if (child_error > 0 && child_error != 123) {
  471. break;
  472. }
  473. } /* while */
  474. if (ENABLE_FEATURE_CLEAN_UP)
  475. free(max_chars);
  476. return child_error;
  477. }
  478. #ifdef TEST
  479. const char *applet_name = "debug stuff usage";
  480. void bb_show_usage(void)
  481. {
  482. fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
  483. applet_name);
  484. exit(EXIT_FAILURE);
  485. }
  486. int main(int argc, char **argv)
  487. {
  488. return xargs_main(argc, argv);
  489. }
  490. #endif /* TEST */