xargs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini xargs implementation for busybox
  4. * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
  5. *
  6. * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
  7. *
  8. * Special thanks
  9. * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
  10. * - Mike Rendell <michael@cs.mun.ca>
  11. * and David MacKenzie <djm@gnu.ai.mit.edu>.
  12. *
  13. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  14. *
  15. * xargs is described in the Single Unix Specification v3 at
  16. * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
  17. *
  18. */
  19. #include "libbb.h"
  20. /* This is a NOEXEC applet. Be very careful! */
  21. /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
  22. We try to make it as large as possible. */
  23. #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
  24. #define ARG_MAX sysconf (_SC_ARG_MAX)
  25. #endif
  26. #ifndef ARG_MAX
  27. #define ARG_MAX 470
  28. #endif
  29. #ifdef TEST
  30. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
  31. # define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
  32. # endif
  33. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
  34. # define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
  35. # endif
  36. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
  37. # define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
  38. # endif
  39. # ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  40. # define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
  41. # endif
  42. #endif
  43. /*
  44. This function has special algorithm.
  45. Don't use fork and include to main!
  46. */
  47. static int xargs_exec(char **args)
  48. {
  49. int status;
  50. status = spawn_and_wait(args);
  51. if (status < 0) {
  52. bb_simple_perror_msg(args[0]);
  53. return errno == ENOENT ? 127 : 126;
  54. }
  55. if (status == 255) {
  56. bb_error_msg("%s: exited with status 255; aborting", args[0]);
  57. return 124;
  58. }
  59. /* Huh? I think we won't see this, ever. We don't wait with WUNTRACED!
  60. if (WIFSTOPPED(status)) {
  61. bb_error_msg("%s: stopped by signal %d",
  62. args[0], WSTOPSIG(status));
  63. return 125;
  64. }
  65. */
  66. if (status >= 1000) {
  67. bb_error_msg("%s: terminated by signal %d",
  68. args[0], status - 1000);
  69. return 125;
  70. }
  71. if (status)
  72. return 123;
  73. return 0;
  74. }
  75. typedef struct xlist_t {
  76. struct xlist_t *link;
  77. size_t length;
  78. char xstr[1];
  79. } xlist_t;
  80. static smallint eof_stdin_detected;
  81. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  82. #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
  83. || (c) == '\f' || (c) == '\v')
  84. #if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
  85. static xlist_t *process_stdin(xlist_t *list_arg,
  86. const char *eof_str, size_t mc, char *buf)
  87. {
  88. #define NORM 0
  89. #define QUOTE 1
  90. #define BACKSLASH 2
  91. #define SPACE 4
  92. char *s = NULL; /* start word */
  93. char *p = NULL; /* pointer to end word */
  94. char q = '\0'; /* quote char */
  95. char state = NORM;
  96. char eof_str_detected = 0;
  97. size_t line_l = 0; /* size loaded args line */
  98. int c; /* current char */
  99. xlist_t *cur;
  100. xlist_t *prev;
  101. prev = cur = list_arg;
  102. while (1) {
  103. if (!cur) break;
  104. prev = cur;
  105. line_l += cur->length;
  106. cur = cur->link;
  107. }
  108. while (!eof_stdin_detected) {
  109. c = getchar();
  110. if (c == EOF) {
  111. eof_stdin_detected = 1;
  112. if (s)
  113. goto unexpected_eof;
  114. break;
  115. }
  116. if (eof_str_detected)
  117. continue;
  118. if (state == BACKSLASH) {
  119. state = NORM;
  120. goto set;
  121. } else if (state == QUOTE) {
  122. if (c != q)
  123. goto set;
  124. q = '\0';
  125. state = NORM;
  126. } else { /* if (state == NORM) */
  127. if (ISSPACE(c)) {
  128. if (s) {
  129. unexpected_eof:
  130. state = SPACE;
  131. c = '\0';
  132. goto set;
  133. }
  134. } else {
  135. if (s == NULL)
  136. s = p = buf;
  137. if (c == '\\') {
  138. state = BACKSLASH;
  139. } else if (c == '\'' || c == '"') {
  140. q = c;
  141. state = QUOTE;
  142. } else {
  143. set:
  144. if ((size_t)(p - buf) >= mc)
  145. bb_error_msg_and_die("argument line too long");
  146. *p++ = c;
  147. }
  148. }
  149. }
  150. if (state == SPACE) { /* word's delimiter or EOF detected */
  151. if (q) {
  152. bb_error_msg_and_die("unmatched %s quote",
  153. q == '\'' ? "single" : "double");
  154. }
  155. /* word loaded */
  156. if (eof_str) {
  157. eof_str_detected = (strcmp(s, eof_str) == 0);
  158. }
  159. if (!eof_str_detected) {
  160. size_t length = (p - buf);
  161. /* Dont xzalloc - it can be quite big */
  162. cur = xmalloc(offsetof(xlist_t, xstr) + length);
  163. cur->link = NULL;
  164. cur->length = length;
  165. memcpy(cur->xstr, s, length);
  166. if (prev == NULL) {
  167. list_arg = cur;
  168. } else {
  169. prev->link = cur;
  170. }
  171. prev = cur;
  172. line_l += length;
  173. if (line_l > mc) {
  174. /* stop memory usage :-) */
  175. break;
  176. }
  177. }
  178. s = NULL;
  179. state = NORM;
  180. }
  181. }
  182. return list_arg;
  183. }
  184. #else
  185. /* The variant does not support single quotes, double quotes or backslash */
  186. static xlist_t *process_stdin(xlist_t *list_arg,
  187. const char *eof_str, size_t mc, char *buf)
  188. {
  189. int c; /* current char */
  190. char eof_str_detected = 0;
  191. char *s = NULL; /* start word */
  192. char *p = NULL; /* pointer to end word */
  193. size_t line_l = 0; /* size loaded args line */
  194. xlist_t *cur;
  195. xlist_t *prev;
  196. prev = cur = list_arg;
  197. while (1) {
  198. if (!cur) break;
  199. prev = cur;
  200. line_l += cur->length;
  201. cur = cur->link;
  202. }
  203. while (!eof_stdin_detected) {
  204. c = getchar();
  205. if (c == EOF) {
  206. eof_stdin_detected = 1;
  207. }
  208. if (eof_str_detected)
  209. continue;
  210. if (c == EOF || ISSPACE(c)) {
  211. if (s == NULL)
  212. continue;
  213. c = EOF;
  214. }
  215. if (s == NULL)
  216. s = p = buf;
  217. if ((p - buf) >= mc)
  218. bb_error_msg_and_die("argument line too long");
  219. *p++ = (c == EOF ? '\0' : c);
  220. if (c == EOF) { /* word's delimiter or EOF detected */
  221. /* word loaded */
  222. if (eof_str) {
  223. eof_str_detected = (strcmp(s, eof_str) == 0);
  224. }
  225. if (!eof_str_detected) {
  226. size_t length = (p - buf);
  227. /* Dont xzalloc - it can be quite big */
  228. cur = xmalloc(offsetof(xlist_t, xstr) + length);
  229. cur->link = NULL;
  230. cur->length = length;
  231. memcpy(cur->xstr, s, length);
  232. if (prev == NULL) {
  233. list_arg = cur;
  234. } else {
  235. prev->link = cur;
  236. }
  237. prev = cur;
  238. line_l += length;
  239. if (line_l > mc) {
  240. /* stop memory usage :-) */
  241. break;
  242. }
  243. s = NULL;
  244. }
  245. }
  246. }
  247. return list_arg;
  248. }
  249. #endif /* FEATURE_XARGS_SUPPORT_QUOTES */
  250. #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
  251. /* Prompt the user for a response, and
  252. if the user responds affirmatively, return true;
  253. otherwise, return false. Uses "/dev/tty", not stdin. */
  254. static int xargs_ask_confirmation(void)
  255. {
  256. FILE *tty_stream;
  257. int c, savec;
  258. tty_stream = xfopen(CURRENT_TTY, "r");
  259. fputs(" ?...", stderr);
  260. fflush(stderr);
  261. c = savec = getc(tty_stream);
  262. while (c != EOF && c != '\n')
  263. c = getc(tty_stream);
  264. fclose(tty_stream);
  265. return (savec == 'y' || savec == 'Y');
  266. }
  267. #else
  268. # define xargs_ask_confirmation() 1
  269. #endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
  270. #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  271. static xlist_t *process0_stdin(xlist_t *list_arg,
  272. const char *eof_str ATTRIBUTE_UNUSED, size_t mc, char *buf)
  273. {
  274. int c; /* current char */
  275. char *s = NULL; /* start word */
  276. char *p = NULL; /* pointer to end word */
  277. size_t line_l = 0; /* size loaded args line */
  278. xlist_t *cur;
  279. xlist_t *prev;
  280. prev = cur = list_arg;
  281. while (1) {
  282. if (!cur) break;
  283. prev = cur;
  284. line_l += cur->length;
  285. cur = cur->link;
  286. }
  287. while (!eof_stdin_detected) {
  288. c = getchar();
  289. if (c == EOF) {
  290. eof_stdin_detected = 1;
  291. if (s == NULL)
  292. break;
  293. c = '\0';
  294. }
  295. if (s == NULL)
  296. s = p = buf;
  297. if ((size_t)(p - buf) >= mc)
  298. bb_error_msg_and_die("argument line too long");
  299. *p++ = c;
  300. if (c == '\0') { /* word's delimiter or EOF detected */
  301. /* word loaded */
  302. size_t length = (p - buf);
  303. /* Dont xzalloc - it can be quite big */
  304. cur = xmalloc(offsetof(xlist_t, xstr) + length);
  305. cur->link = NULL;
  306. cur->length = length;
  307. memcpy(cur->xstr, s, length);
  308. if (prev == NULL) {
  309. list_arg = cur;
  310. } else {
  311. prev->link = cur;
  312. }
  313. prev = cur;
  314. line_l += length;
  315. if (line_l > mc) {
  316. /* stop memory usage :-) */
  317. break;
  318. }
  319. s = NULL;
  320. }
  321. }
  322. return list_arg;
  323. }
  324. #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
  325. /* Correct regardless of combination of CONFIG_xxx */
  326. enum {
  327. OPTBIT_VERBOSE = 0,
  328. OPTBIT_NO_EMPTY,
  329. OPTBIT_UPTO_NUMBER,
  330. OPTBIT_UPTO_SIZE,
  331. OPTBIT_EOF_STRING,
  332. USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
  333. USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
  334. USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
  335. OPT_VERBOSE = 1<<OPTBIT_VERBOSE ,
  336. OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY ,
  337. OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER,
  338. OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE ,
  339. OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING ,
  340. OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0,
  341. OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0,
  342. OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0,
  343. };
  344. #define OPTION_STR "+trn:s:e::" \
  345. USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
  346. USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
  347. USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
  348. int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  349. int xargs_main(int argc, char **argv)
  350. {
  351. char **args;
  352. int i, n;
  353. xlist_t *list = NULL;
  354. xlist_t *cur;
  355. int child_error = 0;
  356. char *max_args, *max_chars;
  357. int n_max_arg;
  358. size_t n_chars = 0;
  359. long orig_arg_max;
  360. const char *eof_str = "_";
  361. unsigned opt;
  362. size_t n_max_chars;
  363. #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  364. xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
  365. #else
  366. #define read_args process_stdin
  367. #endif
  368. opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &eof_str);
  369. if (opt & OPT_ZEROTERM)
  370. USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
  371. argv += optind;
  372. argc -= optind;
  373. if (!argc) {
  374. /* default behavior is to echo all the filenames */
  375. *argv = (char*)"echo";
  376. argc++;
  377. }
  378. orig_arg_max = ARG_MAX;
  379. if (orig_arg_max == -1)
  380. orig_arg_max = LONG_MAX;
  381. orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
  382. if (opt & OPT_UPTO_SIZE) {
  383. n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
  384. for (i = 0; i < argc; i++) {
  385. n_chars += strlen(*argv) + 1;
  386. }
  387. if (n_max_chars < n_chars) {
  388. bb_error_msg_and_die("cannot fit single argument within argument list size limit");
  389. }
  390. n_max_chars -= n_chars;
  391. } else {
  392. /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
  393. have it at 1 meg). Things will work fine with a large ARG_MAX but it
  394. will probably hurt the system more than it needs to; an array of this
  395. size is allocated. */
  396. if (orig_arg_max > 20 * 1024)
  397. orig_arg_max = 20 * 1024;
  398. n_max_chars = orig_arg_max;
  399. }
  400. max_chars = xmalloc(n_max_chars);
  401. if (opt & OPT_UPTO_NUMBER) {
  402. n_max_arg = xatoul_range(max_args, 1, INT_MAX);
  403. } else {
  404. n_max_arg = n_max_chars;
  405. }
  406. while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
  407. !(opt & OPT_NO_EMPTY))
  408. {
  409. opt |= OPT_NO_EMPTY;
  410. n = 0;
  411. n_chars = 0;
  412. #if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
  413. for (cur = list; cur;) {
  414. n_chars += cur->length;
  415. n++;
  416. cur = cur->link;
  417. if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
  418. if (opt & OPT_TERMINATE)
  419. bb_error_msg_and_die("argument list too long");
  420. break;
  421. }
  422. }
  423. #else
  424. for (cur = list; cur; cur = cur->link) {
  425. n_chars += cur->length;
  426. n++;
  427. if (n_chars > n_max_chars || n == n_max_arg) {
  428. break;
  429. }
  430. }
  431. #endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
  432. /* allocate pointers for execvp:
  433. argc*arg, n*arg from stdin, NULL */
  434. args = xzalloc((n + argc + 1) * sizeof(char *));
  435. /* store the command to be executed
  436. (taken from the command line) */
  437. for (i = 0; i < argc; i++)
  438. args[i] = argv[i];
  439. /* (taken from stdin) */
  440. for (cur = list; n; cur = cur->link) {
  441. args[i++] = cur->xstr;
  442. n--;
  443. }
  444. if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
  445. for (i = 0; args[i]; i++) {
  446. if (i)
  447. fputc(' ', stderr);
  448. fputs(args[i], stderr);
  449. }
  450. if (!(opt & OPT_INTERACTIVE))
  451. fputc('\n', stderr);
  452. }
  453. if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
  454. child_error = xargs_exec(args);
  455. }
  456. /* clean up */
  457. for (i = argc; args[i]; i++) {
  458. cur = list;
  459. list = list->link;
  460. free(cur);
  461. }
  462. free(args);
  463. if (child_error > 0 && child_error != 123) {
  464. break;
  465. }
  466. }
  467. if (ENABLE_FEATURE_CLEAN_UP)
  468. free(max_chars);
  469. return child_error;
  470. }
  471. #ifdef TEST
  472. const char *applet_name = "debug stuff usage";
  473. void bb_show_usage(void)
  474. {
  475. fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
  476. applet_name);
  477. exit(1);
  478. }
  479. int main(int argc, char **argv)
  480. {
  481. return xargs_main(argc, argv);
  482. }
  483. #endif /* TEST */