xargs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini xargs implementation for busybox
  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. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  13. *
  14. * xargs is described in the Single Unix Specification v3 at
  15. * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
  16. */
  17. //config:config XARGS
  18. //config: bool "xargs"
  19. //config: default y
  20. //config: help
  21. //config: xargs is used to execute a specified command for
  22. //config: every item from standard input.
  23. //config:
  24. //config:config FEATURE_XARGS_SUPPORT_CONFIRMATION
  25. //config: bool "Enable -p: prompt and confirmation"
  26. //config: default y
  27. //config: depends on XARGS
  28. //config: help
  29. //config: Support -p: prompt the user whether to run each command
  30. //config: line and read a line from the terminal.
  31. //config:
  32. //config:config FEATURE_XARGS_SUPPORT_QUOTES
  33. //config: bool "Enable single and double quotes and backslash"
  34. //config: default y
  35. //config: depends on XARGS
  36. //config: help
  37. //config: Support quoting in the input.
  38. //config:
  39. //config:config FEATURE_XARGS_SUPPORT_TERMOPT
  40. //config: bool "Enable -x: exit if -s or -n is exceeded"
  41. //config: default y
  42. //config: depends on XARGS
  43. //config: help
  44. //config: Support -x: exit if the command size (see the -s or -n option)
  45. //config: is exceeded.
  46. //config:
  47. //config:config FEATURE_XARGS_SUPPORT_ZERO_TERM
  48. //config: bool "Enable -0: NUL-terminated input"
  49. //config: default y
  50. //config: depends on XARGS
  51. //config: help
  52. //config: Support -0: input items are terminated by a NUL character
  53. //config: instead of whitespace, and the quotes and backslash
  54. //config: are not special.
  55. //config:
  56. //config:config FEATURE_XARGS_SUPPORT_REPL_STR
  57. //config: bool "Enable -I STR: string to replace"
  58. //config: default y
  59. //config: depends on XARGS
  60. //config: help
  61. //config: Support -I STR and -i[STR] options.
  62. //applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
  63. //kbuild:lib-$(CONFIG_XARGS) += xargs.o
  64. #include "libbb.h"
  65. #include "common_bufsiz.h"
  66. /* This is a NOEXEC applet. Be very careful! */
  67. //#define dbg_msg(...) bb_error_msg(__VA_ARGS__)
  68. #define dbg_msg(...) ((void)0)
  69. #ifdef TEST
  70. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
  71. # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
  72. # endif
  73. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
  74. # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
  75. # endif
  76. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
  77. # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
  78. # endif
  79. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  80. # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
  81. # endif
  82. #endif
  83. struct globals {
  84. char **args;
  85. #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
  86. char **argv;
  87. const char *repl_str;
  88. char eol_ch;
  89. #endif
  90. const char *eof_str;
  91. int idx;
  92. } FIX_ALIASING;
  93. #define G (*(struct globals*)bb_common_bufsiz1)
  94. #define INIT_G() do { \
  95. setup_common_bufsiz(); \
  96. G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
  97. IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
  98. IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
  99. } while (0)
  100. static int xargs_exec(void)
  101. {
  102. int status;
  103. status = spawn_and_wait(G.args);
  104. if (status < 0) {
  105. bb_simple_perror_msg(G.args[0]);
  106. return errno == ENOENT ? 127 : 126;
  107. }
  108. if (status == 255) {
  109. bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
  110. return 124;
  111. }
  112. if (status >= 0x180) {
  113. bb_error_msg("'%s' terminated by signal %d",
  114. G.args[0], status - 0x180);
  115. return 125;
  116. }
  117. if (status)
  118. return 123;
  119. return 0;
  120. }
  121. /* In POSIX/C locale isspace is only these chars: "\t\n\v\f\r" and space.
  122. * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
  123. */
  124. #define ISSPACE(a) ({ unsigned char xargs__isspace = (a) - 9; xargs__isspace == (' ' - 9) || xargs__isspace <= (13 - 9); })
  125. static void store_param(char *s)
  126. {
  127. /* Grow by 256 elements at once */
  128. if (!(G.idx & 0xff)) { /* G.idx == N*256 */
  129. /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
  130. G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
  131. }
  132. G.args[G.idx++] = s;
  133. }
  134. /* process[0]_stdin:
  135. * Read characters into buf[n_max_chars+1], and when parameter delimiter
  136. * is seen, store the address of a new parameter to args[].
  137. * If reading discovers that last chars do not form the complete
  138. * parameter, the pointer to the first such "tail character" is returned.
  139. * (buf has extra byte at the end to accommodate terminating NUL
  140. * of "tail characters" string).
  141. * Otherwise, the returned pointer points to NUL byte.
  142. * On entry, buf[] may contain some "seed chars" which are to become
  143. * the beginning of the first parameter.
  144. */
  145. #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
  146. static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
  147. {
  148. #define NORM 0
  149. #define QUOTE 1
  150. #define BACKSLASH 2
  151. #define SPACE 4
  152. char q = '\0'; /* quote char */
  153. char state = NORM;
  154. char *s = buf; /* start of the word */
  155. char *p = s + strlen(buf); /* end of the word */
  156. buf += n_max_chars; /* past buffer's end */
  157. /* "goto ret" is used instead of "break" to make control flow
  158. * more obvious: */
  159. while (1) {
  160. int c = getchar();
  161. if (c == EOF) {
  162. if (p != s)
  163. goto close_word;
  164. goto ret;
  165. }
  166. if (state == BACKSLASH) {
  167. state = NORM;
  168. goto set;
  169. }
  170. if (state == QUOTE) {
  171. if (c != q)
  172. goto set;
  173. q = '\0';
  174. state = NORM;
  175. } else { /* if (state == NORM) */
  176. if (ISSPACE(c)) {
  177. if (p != s) {
  178. close_word:
  179. state = SPACE;
  180. c = '\0';
  181. goto set;
  182. }
  183. } else {
  184. if (c == '\\') {
  185. state = BACKSLASH;
  186. } else if (c == '\'' || c == '"') {
  187. q = c;
  188. state = QUOTE;
  189. } else {
  190. set:
  191. *p++ = c;
  192. }
  193. }
  194. }
  195. if (state == SPACE) { /* word's delimiter or EOF detected */
  196. if (q) {
  197. bb_error_msg_and_die("unmatched %s quote",
  198. q == '\'' ? "single" : "double");
  199. }
  200. /* A full word is loaded */
  201. if (G.eof_str) {
  202. if (strcmp(s, G.eof_str) == 0) {
  203. while (getchar() != EOF)
  204. continue;
  205. p = s;
  206. goto ret;
  207. }
  208. }
  209. store_param(s);
  210. dbg_msg("args[]:'%s'", s);
  211. s = p;
  212. n_max_arg--;
  213. if (n_max_arg == 0) {
  214. goto ret;
  215. }
  216. state = NORM;
  217. }
  218. if (p == buf) {
  219. goto ret;
  220. }
  221. }
  222. ret:
  223. *p = '\0';
  224. /* store_param(NULL) - caller will do it */
  225. dbg_msg("return:'%s'", s);
  226. return s;
  227. }
  228. #else
  229. /* The variant does not support single quotes, double quotes or backslash */
  230. static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
  231. {
  232. char *s = buf; /* start of the word */
  233. char *p = s + strlen(buf); /* end of the word */
  234. buf += n_max_chars; /* past buffer's end */
  235. while (1) {
  236. int c = getchar();
  237. if (c == EOF) {
  238. if (p == s)
  239. goto ret;
  240. }
  241. if (c == EOF || ISSPACE(c)) {
  242. if (p == s)
  243. continue;
  244. c = EOF;
  245. }
  246. *p++ = (c == EOF ? '\0' : c);
  247. if (c == EOF) { /* word's delimiter or EOF detected */
  248. /* A full word is loaded */
  249. if (G.eof_str) {
  250. if (strcmp(s, G.eof_str) == 0) {
  251. while (getchar() != EOF)
  252. continue;
  253. p = s;
  254. goto ret;
  255. }
  256. }
  257. store_param(s);
  258. dbg_msg("args[]:'%s'", s);
  259. s = p;
  260. n_max_arg--;
  261. if (n_max_arg == 0) {
  262. goto ret;
  263. }
  264. }
  265. if (p == buf) {
  266. goto ret;
  267. }
  268. }
  269. ret:
  270. *p = '\0';
  271. /* store_param(NULL) - caller will do it */
  272. dbg_msg("return:'%s'", s);
  273. return s;
  274. }
  275. #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
  276. #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  277. static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf)
  278. {
  279. char *s = buf; /* start of the word */
  280. char *p = s + strlen(buf); /* end of the word */
  281. buf += n_max_chars; /* past buffer's end */
  282. while (1) {
  283. int c = getchar();
  284. if (c == EOF) {
  285. if (p == s)
  286. goto ret;
  287. c = '\0';
  288. }
  289. *p++ = c;
  290. if (c == '\0') { /* NUL or EOF detected */
  291. /* A full word is loaded */
  292. store_param(s);
  293. dbg_msg("args[]:'%s'", s);
  294. s = p;
  295. n_max_arg--;
  296. if (n_max_arg == 0) {
  297. goto ret;
  298. }
  299. }
  300. if (p == buf) {
  301. goto ret;
  302. }
  303. }
  304. ret:
  305. *p = '\0';
  306. /* store_param(NULL) - caller will do it */
  307. dbg_msg("return:'%s'", s);
  308. return s;
  309. }
  310. #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
  311. #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
  312. /*
  313. * Used if -I<repl> was specified.
  314. * In this mode, words aren't appended to PROG ARGS.
  315. * Instead, entire input line is read, then <repl> string
  316. * in every PROG and ARG is replaced with the line:
  317. * echo -e "ho ho\nhi" | xargs -I_ cmd __ _
  318. * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'".
  319. * -n MAX_ARGS seems to be ignored.
  320. * Tested with GNU findutils 4.5.10.
  321. */
  322. //FIXME: n_max_chars is not handled the same way as in GNU findutils.
  323. //FIXME: quoting is not implemented.
  324. static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf)
  325. {
  326. int i;
  327. char *end, *p;
  328. /* Free strings from last invocation, if any */
  329. for (i = 0; G.args && G.args[i]; i++)
  330. if (G.args[i] != G.argv[i])
  331. free(G.args[i]);
  332. end = buf + n_max_chars;
  333. p = buf;
  334. while (1) {
  335. int c = getchar();
  336. if (c == EOF || c == G.eol_ch) {
  337. if (p == buf)
  338. goto ret; /* empty line */
  339. c = '\0';
  340. }
  341. *p++ = c;
  342. if (c == '\0') { /* EOL or EOF detected */
  343. i = 0;
  344. while (G.argv[i]) {
  345. char *arg = G.argv[i];
  346. int count = count_strstr(arg, G.repl_str);
  347. if (count != 0)
  348. arg = xmalloc_substitute_string(arg, count, G.repl_str, buf);
  349. store_param(arg);
  350. dbg_msg("args[]:'%s'", arg);
  351. i++;
  352. }
  353. p = buf;
  354. goto ret;
  355. }
  356. if (p == end) {
  357. goto ret;
  358. }
  359. }
  360. ret:
  361. *p = '\0';
  362. /* store_param(NULL) - caller will do it */
  363. dbg_msg("return:'%s'", buf);
  364. return buf;
  365. }
  366. #endif
  367. #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
  368. /* Prompt the user for a response, and
  369. * if user responds affirmatively, return true;
  370. * otherwise, return false. Uses "/dev/tty", not stdin.
  371. */
  372. static int xargs_ask_confirmation(void)
  373. {
  374. FILE *tty_stream;
  375. int c, savec;
  376. tty_stream = xfopen_for_read(CURRENT_TTY);
  377. fputs(" ?...", stderr);
  378. fflush_all();
  379. c = savec = getc(tty_stream);
  380. while (c != EOF && c != '\n')
  381. c = getc(tty_stream);
  382. fclose(tty_stream);
  383. return (savec == 'y' || savec == 'Y');
  384. }
  385. #else
  386. # define xargs_ask_confirmation() 1
  387. #endif
  388. //usage:#define xargs_trivial_usage
  389. //usage: "[OPTIONS] [PROG ARGS]"
  390. //usage:#define xargs_full_usage "\n\n"
  391. //usage: "Run PROG on every item given by stdin\n"
  392. //usage: IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
  393. //usage: "\n -p Ask user whether to run each command"
  394. //usage: )
  395. //usage: "\n -r Don't run command if input is empty"
  396. //usage: IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
  397. //usage: "\n -0 Input is separated by NUL characters"
  398. //usage: )
  399. //usage: "\n -t Print the command on stderr before execution"
  400. //usage: "\n -e[STR] STR stops input processing"
  401. //usage: "\n -n N Pass no more than N args to PROG"
  402. //usage: "\n -s N Pass command line of no more than N bytes"
  403. //usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR(
  404. //usage: "\n -I STR Replace STR within PROG ARGS with input line"
  405. //usage: )
  406. //usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT(
  407. //usage: "\n -x Exit if size is exceeded"
  408. //usage: )
  409. //usage:#define xargs_example_usage
  410. //usage: "$ ls | xargs gzip\n"
  411. //usage: "$ find . -name '*.c' -print | xargs rm\n"
  412. /* Correct regardless of combination of CONFIG_xxx */
  413. enum {
  414. OPTBIT_VERBOSE = 0,
  415. OPTBIT_NO_EMPTY,
  416. OPTBIT_UPTO_NUMBER,
  417. OPTBIT_UPTO_SIZE,
  418. OPTBIT_EOF_STRING,
  419. OPTBIT_EOF_STRING1,
  420. IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
  421. IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
  422. IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
  423. IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,)
  424. IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,)
  425. OPT_VERBOSE = 1 << OPTBIT_VERBOSE ,
  426. OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY ,
  427. OPT_UPTO_NUMBER = 1 << OPTBIT_UPTO_NUMBER,
  428. OPT_UPTO_SIZE = 1 << OPTBIT_UPTO_SIZE ,
  429. OPT_EOF_STRING = 1 << OPTBIT_EOF_STRING , /* GNU: -e[<param>] */
  430. OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
  431. OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0,
  432. OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0,
  433. OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0,
  434. OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0,
  435. OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0,
  436. };
  437. #define OPTION_STR "+trn:s:e::E:" \
  438. IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
  439. IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
  440. IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \
  441. IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::")
  442. int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  443. int xargs_main(int argc, char **argv)
  444. {
  445. int i;
  446. int child_error = 0;
  447. char *max_args;
  448. char *max_chars;
  449. char *buf;
  450. unsigned opt;
  451. int n_max_chars;
  452. int n_max_arg;
  453. #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \
  454. || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
  455. char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin;
  456. #else
  457. #define read_args process_stdin
  458. #endif
  459. INIT_G();
  460. #if ENABLE_DESKTOP && ENABLE_LONG_OPTS
  461. /* For example, Fedora's build system uses --no-run-if-empty */
  462. applet_long_options =
  463. "no-run-if-empty\0" No_argument "r"
  464. ;
  465. #endif
  466. opt = getopt32(argv, OPTION_STR,
  467. &max_args, &max_chars, &G.eof_str, &G.eof_str
  468. IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
  469. );
  470. /* -E ""? You may wonder why not just omit -E?
  471. * This is used for portability:
  472. * old xargs was using "_" as default for -E / -e */
  473. if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0')
  474. G.eof_str = NULL;
  475. if (opt & OPT_ZEROTERM) {
  476. IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;)
  477. IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';)
  478. }
  479. argv += optind;
  480. argc -= optind;
  481. if (!argv[0]) {
  482. /* default behavior is to echo all the filenames */
  483. *--argv = (char*)"echo";
  484. argc++;
  485. }
  486. /*
  487. * The Open Group Base Specifications Issue 6:
  488. * "The xargs utility shall limit the command line length such that
  489. * when the command line is invoked, the combined argument
  490. * and environment lists (see the exec family of functions
  491. * in the System Interfaces volume of IEEE Std 1003.1-2001)
  492. * shall not exceed {ARG_MAX}-2048 bytes".
  493. */
  494. n_max_chars = bb_arg_max();
  495. if (n_max_chars > 32 * 1024)
  496. n_max_chars = 32 * 1024;
  497. /*
  498. * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
  499. * so that the process may safely modify its environment.
  500. */
  501. n_max_chars -= 2048;
  502. if (opt & OPT_UPTO_SIZE) {
  503. n_max_chars = xatou_range(max_chars, 1, INT_MAX);
  504. }
  505. /* Account for prepended fixed arguments */
  506. {
  507. size_t n_chars = 0;
  508. for (i = 0; argv[i]; i++) {
  509. n_chars += strlen(argv[i]) + 1;
  510. }
  511. n_max_chars -= n_chars;
  512. }
  513. /* Sanity check */
  514. if (n_max_chars <= 0) {
  515. bb_error_msg_and_die("can't fit single argument within argument list size limit");
  516. }
  517. buf = xzalloc(n_max_chars + 1);
  518. n_max_arg = n_max_chars;
  519. if (opt & OPT_UPTO_NUMBER) {
  520. n_max_arg = xatou_range(max_args, 1, INT_MAX);
  521. /* Not necessary, we use growable args[]: */
  522. /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */
  523. }
  524. #if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR
  525. if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) {
  526. /*
  527. * -I<str>:
  528. * Unmodified args are kept in G.argv[i],
  529. * G.args[i] receives malloced G.argv[i] with <str> replaced
  530. * with input line. Setting this up:
  531. */
  532. G.args = NULL;
  533. G.argv = argv;
  534. argc = 0;
  535. read_args = process_stdin_with_replace;
  536. /* Make -I imply -r. GNU findutils seems to do the same: */
  537. /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */
  538. opt |= OPT_NO_EMPTY;
  539. } else
  540. #endif
  541. {
  542. /* Allocate pointers for execvp.
  543. * We can statically allocate (argc + n_max_arg + 1) elements
  544. * and do not bother with resizing args[], but on 64-bit machines
  545. * this results in args[] vector which is ~8 times bigger
  546. * than n_max_chars! That is, with n_max_chars == 20k,
  547. * args[] will take 160k (!), which will most likely be
  548. * almost entirely unused.
  549. *
  550. * See store_param() for matching 256-step growth logic
  551. */
  552. G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff));
  553. /* Store the command to be executed, part 1 */
  554. for (i = 0; argv[i]; i++)
  555. G.args[i] = argv[i];
  556. }
  557. while (1) {
  558. char *rem;
  559. G.idx = argc;
  560. rem = read_args(n_max_chars, n_max_arg, buf);
  561. store_param(NULL);
  562. if (!G.args[argc]) {
  563. if (*rem != '\0')
  564. bb_error_msg_and_die("argument line too long");
  565. if (opt & OPT_NO_EMPTY)
  566. break;
  567. }
  568. opt |= OPT_NO_EMPTY;
  569. if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
  570. const char *fmt = " %s" + 1;
  571. char **args = G.args;
  572. for (i = 0; args[i]; i++) {
  573. fprintf(stderr, fmt, args[i]);
  574. fmt = " %s";
  575. }
  576. if (!(opt & OPT_INTERACTIVE))
  577. bb_putchar_stderr('\n');
  578. }
  579. if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
  580. child_error = xargs_exec();
  581. }
  582. if (child_error > 0 && child_error != 123) {
  583. break;
  584. }
  585. overlapping_strcpy(buf, rem);
  586. } /* while */
  587. if (ENABLE_FEATURE_CLEAN_UP) {
  588. free(G.args);
  589. free(buf);
  590. }
  591. return child_error;
  592. }
  593. #ifdef TEST
  594. const char *applet_name = "debug stuff usage";
  595. void bb_show_usage(void)
  596. {
  597. fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
  598. applet_name);
  599. exit(EXIT_FAILURE);
  600. }
  601. int main(int argc, char **argv)
  602. {
  603. return xargs_main(argc, argv);
  604. }
  605. #endif /* TEST */