xargs.c 22 KB

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