xargs.c 21 KB

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