xargs.c 21 KB

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