3
0

xargs.c 17 KB

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