xargs.c 21 KB

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