3
0

xargs.c 21 KB

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