aboutsummaryrefslogtreecommitdiff
path: root/findutils
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2014-06-30 21:13:06 +0100
committerRon Yorston <rmy@pobox.com>2014-06-30 21:13:06 +0100
commit099e8b6438345baae560a629d548af07a8c3125c (patch)
tree71b5600b22b0019af675e4a991394ce32c8207c5 /findutils
parente19594cc6e49e78fa50a654f15cf9a04e77d054a (diff)
parent184b2669175e562d58894e22f6320cebf3316c25 (diff)
downloadbusybox-w32-099e8b6438345baae560a629d548af07a8c3125c.tar.gz
busybox-w32-099e8b6438345baae560a629d548af07a8c3125c.tar.bz2
busybox-w32-099e8b6438345baae560a629d548af07a8c3125c.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'findutils')
-rw-r--r--findutils/find.c159
-rw-r--r--findutils/xargs.c26
2 files changed, 154 insertions, 31 deletions
diff --git a/findutils/find.c b/findutils/find.c
index 6d34f4d68..56a7ed3ab 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -137,6 +137,16 @@
137//config: Support the 'find -exec' option for executing commands based upon 137//config: Support the 'find -exec' option for executing commands based upon
138//config: the files matched. 138//config: the files matched.
139//config: 139//config:
140//config:config FEATURE_FIND_EXEC_PLUS
141//config: bool "Enable -exec ... {} +"
142//config: default y
143//config: depends on FEATURE_FIND_EXEC
144//config: help
145//config: Support the 'find -exec ... {} +' option for executing commands
146//config: for all matched files at once.
147//config: Without this option, -exec + is a synonym for -exec ;
148//config: (IOW: it works correctly, but without expected speedup)
149//config:
140//config:config FEATURE_FIND_USER 150//config:config FEATURE_FIND_USER
141//config: bool "Enable -user: username/uid matching" 151//config: bool "Enable -user: username/uid matching"
142//config: default y 152//config: default y
@@ -319,6 +329,9 @@
319//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by" 329//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
320//usage: "\n file name. Fails if CMD exits with nonzero" 330//usage: "\n file name. Fails if CMD exits with nonzero"
321//usage: ) 331//usage: )
332//usage: IF_FEATURE_FIND_EXEC_PLUS(
333//usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names"
334//usage: )
322//usage: IF_FEATURE_FIND_DELETE( 335//usage: IF_FEATURE_FIND_DELETE(
323//usage: "\n -delete Delete current file/directory. Turns on -depth option" 336//usage: "\n -delete Delete current file/directory. Turns on -depth option"
324//usage: ) 337//usage: )
@@ -337,8 +350,12 @@
337# define FNM_CASEFOLD 0 350# define FNM_CASEFOLD 0
338#endif 351#endif
339 352
340#define dbg(...) ((void)0) 353#if 1
341/* #define dbg(...) bb_error_msg(__VA_ARGS__) */ 354# define dbg(...) ((void)0)
355#else
356# define dbg(...) bb_error_msg(__VA_ARGS__)
357#endif
358
342 359
343/* This is a NOEXEC applet. Be very careful! */ 360/* This is a NOEXEC applet. Be very careful! */
344 361
@@ -375,7 +392,20 @@ IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
375IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;)) 392IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
376IF_FEATURE_FIND_PRUNE( ACTS(prune)) 393IF_FEATURE_FIND_PRUNE( ACTS(prune))
377IF_FEATURE_FIND_DELETE( ACTS(delete)) 394IF_FEATURE_FIND_DELETE( ACTS(delete))
378IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;)) 395IF_FEATURE_FIND_EXEC( ACTS(exec,
396 char **exec_argv; /* -exec ARGS */
397 unsigned *subst_count;
398 int exec_argc; /* count of ARGS */
399 IF_FEATURE_FIND_EXEC_PLUS(
400 /*
401 * filelist is NULL if "exec ;"
402 * non-NULL if "exec +"
403 */
404 char **filelist;
405 int filelist_idx;
406 int file_len;
407 )
408 ))
379IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;)) 409IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
380IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;)) 410IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
381 411
@@ -389,6 +419,7 @@ struct globals {
389 smallint need_print; 419 smallint need_print;
390 smallint xdev_on; 420 smallint xdev_on;
391 recurse_flags_t recurse_flags; 421 recurse_flags_t recurse_flags;
422 IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
392} FIX_ALIASING; 423} FIX_ALIASING;
393#define G (*(struct globals*)&bb_common_bufsiz1) 424#define G (*(struct globals*)&bb_common_bufsiz1)
394#define INIT_G() do { \ 425#define INIT_G() do { \
@@ -398,6 +429,7 @@ struct globals {
398 /* we have to zero it out because of NOEXEC */ \ 429 /* we have to zero it out because of NOEXEC */ \
399 memset(&G, 0, sizeof(G)); \ 430 memset(&G, 0, sizeof(G)); \
400 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \ 431 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
432 IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \
401 G.need_print = 1; \ 433 G.need_print = 1; \
402 G.recurse_flags = ACTION_RECURSE; \ 434 G.recurse_flags = ACTION_RECURSE; \
403} while (0) 435} while (0)
@@ -452,7 +484,6 @@ static int exec_actions(action ***appp, const char *fileName, const struct stat
452 return rc ^ TRUE; /* restore TRUE bit */ 484 return rc ^ TRUE; /* restore TRUE bit */
453} 485}
454 486
455
456#if !FNM_CASEFOLD 487#if !FNM_CASEFOLD
457static char *strcpy_upcase(char *dst, const char *src) 488static char *strcpy_upcase(char *dst, const char *src)
458{ 489{
@@ -576,17 +607,57 @@ ACTF(inum)
576} 607}
577#endif 608#endif
578#if ENABLE_FEATURE_FIND_EXEC 609#if ENABLE_FEATURE_FIND_EXEC
579ACTF(exec) 610static int do_exec(action_exec *ap, const char *fileName)
580{ 611{
581 int i, rc; 612 int i, rc;
582#if ENABLE_USE_PORTABLE_CODE 613# if ENABLE_FEATURE_FIND_EXEC_PLUS
583 char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1)); 614 int size = ap->exec_argc + ap->filelist_idx + 1;
584#else /* gcc 4.3.1 generates smaller code: */ 615# else
585 char *argv[ap->exec_argc + 1]; 616 int size = ap->exec_argc + 1;
586#endif 617# endif
587 for (i = 0; i < ap->exec_argc; i++) 618# if ENABLE_USE_PORTABLE_CODE
588 argv[i] = xmalloc_substitute_string(ap->exec_argv[i], ap->subst_count[i], "{}", fileName); 619 char **argv = alloca(sizeof(char*) * size);
589 argv[i] = NULL; /* terminate the list */ 620# else /* gcc 4.3.1 generates smaller code: */
621 char *argv[size];
622# endif
623 char **pp = argv;
624
625 for (i = 0; i < ap->exec_argc; i++) {
626 const char *arg = ap->exec_argv[i];
627
628# if ENABLE_FEATURE_FIND_EXEC_PLUS
629 if (ap->filelist) {
630 /* Handling "-exec +"
631 * Only one exec_argv[i] has substitution in it.
632 * Expand that one exec_argv[i] into file list.
633 */
634 if (ap->subst_count[i] == 0) {
635 *pp++ = xstrdup(arg);
636 } else {
637 int j = 0;
638 while (ap->filelist[j]) {
639 /* 2nd arg here should be ap->subst_count[i], but it is always 1: */
640 *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
641 free(ap->filelist[j]);
642 j++;
643 }
644 }
645 } else
646# endif
647 {
648 /* Handling "-exec ;" */
649 *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName);
650 }
651 }
652 *pp = NULL; /* terminate the list */
653
654# if ENABLE_FEATURE_FIND_EXEC_PLUS
655 if (ap->filelist) {
656 ap->filelist[0] = NULL;
657 ap->filelist_idx = 0;
658 ap->file_len = 0;
659 }
660# endif
590 661
591 rc = spawn_and_wait(argv); 662 rc = spawn_and_wait(argv);
592 if (rc < 0) 663 if (rc < 0)
@@ -597,6 +668,48 @@ ACTF(exec)
597 free(argv[i++]); 668 free(argv[i++]);
598 return rc == 0; /* return 1 if exitcode 0 */ 669 return rc == 0; /* return 1 if exitcode 0 */
599} 670}
671ACTF(exec)
672{
673# if ENABLE_FEATURE_FIND_EXEC_PLUS
674 if (ap->filelist) {
675 int rc;
676
677 ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
678 ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
679 ap->file_len += strlen(fileName) + sizeof(char*) + 1;
680 /* If we have lots of files already, exec the command */
681 rc = 1;
682 if (ap->file_len >= G.max_argv_len)
683 rc = do_exec(ap, NULL);
684 return rc;
685 }
686# endif
687 return do_exec(ap, fileName);
688}
689# if ENABLE_FEATURE_FIND_EXEC_PLUS
690static int flush_exec_plus(void)
691{
692 action *ap;
693 action **app;
694 action ***appp = G.actions;
695 while ((app = *appp++) != NULL) {
696 while ((ap = *app++) != NULL) {
697 if (ap->f == (action_fp)func_exec) {
698 action_exec *ae = (void*)ap;
699 if (ae->filelist_idx != 0) {
700 int rc = do_exec(ae, NULL);
701# if ENABLE_FEATURE_FIND_NOT
702 if (ap->invert) rc = !rc;
703# endif
704 if (rc == 0)
705 return 1;
706 }
707 }
708 }
709 }
710 return 0;
711}
712# endif
600#endif 713#endif
601#if ENABLE_FEATURE_FIND_USER 714#if ENABLE_FEATURE_FIND_USER
602ACTF(user) 715ACTF(user)
@@ -1037,6 +1150,7 @@ static action*** parse_params(char **argv)
1037 else if (parm == PARM_exec) { 1150 else if (parm == PARM_exec) {
1038 int i; 1151 int i;
1039 action_exec *ap; 1152 action_exec *ap;
1153 IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
1040 dbg("%d", __LINE__); 1154 dbg("%d", __LINE__);
1041 G.need_print = 0; 1155 G.need_print = 0;
1042 ap = ALLOC_ACTION(exec); 1156 ap = ALLOC_ACTION(exec);
@@ -1049,10 +1163,13 @@ static action*** parse_params(char **argv)
1049 // executes "echo Foo >FILENAME<", 1163 // executes "echo Foo >FILENAME<",
1050 // find -exec echo Foo ">{}<" "+" 1164 // find -exec echo Foo ">{}<" "+"
1051 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...". 1165 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
1052 // TODO (so far we treat "+" just like ";")
1053 if ((argv[0][0] == ';' || argv[0][0] == '+') 1166 if ((argv[0][0] == ';' || argv[0][0] == '+')
1054 && argv[0][1] == '\0' 1167 && argv[0][1] == '\0'
1055 ) { 1168 ) {
1169# if ENABLE_FEATURE_FIND_EXEC_PLUS
1170 if (argv[0][0] == '+')
1171 ap->filelist = xzalloc(sizeof(ap->filelist[0]));
1172# endif
1056 break; 1173 break;
1057 } 1174 }
1058 argv++; 1175 argv++;
@@ -1062,8 +1179,17 @@ static action*** parse_params(char **argv)
1062 bb_error_msg_and_die(bb_msg_requires_arg, arg); 1179 bb_error_msg_and_die(bb_msg_requires_arg, arg);
1063 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); 1180 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
1064 i = ap->exec_argc; 1181 i = ap->exec_argc;
1065 while (i--) 1182 while (i--) {
1066 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}"); 1183 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
1184 IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
1185 }
1186# if ENABLE_FEATURE_FIND_EXEC_PLUS
1187 /*
1188 * coreutils expects {} to appear only once in "-exec +"
1189 */
1190 if (all_subst != 1 && ap->filelist)
1191 bb_error_msg_and_die("only one '{}' allowed for -exec +");
1192# endif
1067 } 1193 }
1068#endif 1194#endif
1069#if ENABLE_FEATURE_FIND_PAREN 1195#if ENABLE_FEATURE_FIND_PAREN
@@ -1335,8 +1461,11 @@ int find_main(int argc UNUSED_PARAM, char **argv)
1335 0) /* depth */ 1461 0) /* depth */
1336 ) { 1462 ) {
1337 status = EXIT_FAILURE; 1463 status = EXIT_FAILURE;
1464 goto out;
1338 } 1465 }
1339 } 1466 }
1340 1467
1468 IF_FEATURE_FIND_EXEC_PLUS(status = flush_exec_plus();)
1469out:
1341 return status; 1470 return status;
1342} 1471}
diff --git a/findutils/xargs.c b/findutils/xargs.c
index 0ba5b566d..76c4747fe 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -523,12 +523,7 @@ int xargs_main(int argc, char **argv)
523 argc++; 523 argc++;
524 } 524 }
525 525
526 /* -s NUM default. fileutils-4.4.2 uses 128k, but I heasitate 526 /*
527 * to use such a big value - first need to change code to use
528 * growable buffer instead of fixed one.
529 */
530 n_max_chars = 32 * 1024;
531 /* Make smaller if system does not allow our default value.
532 * The Open Group Base Specifications Issue 6: 527 * The Open Group Base Specifications Issue 6:
533 * "The xargs utility shall limit the command line length such that 528 * "The xargs utility shall limit the command line length such that
534 * when the command line is invoked, the combined argument 529 * when the command line is invoked, the combined argument
@@ -536,16 +531,15 @@ int xargs_main(int argc, char **argv)
536 * in the System Interfaces volume of IEEE Std 1003.1-2001) 531 * in the System Interfaces volume of IEEE Std 1003.1-2001)
537 * shall not exceed {ARG_MAX}-2048 bytes". 532 * shall not exceed {ARG_MAX}-2048 bytes".
538 */ 533 */
539 { 534 n_max_chars = bb_arg_max();
540 long arg_max = 0; 535 if (n_max_chars > 32 * 1024)
541#if defined _SC_ARG_MAX 536 n_max_chars = 32 * 1024;
542 arg_max = sysconf(_SC_ARG_MAX) - 2048; 537 /*
543#elif defined ARG_MAX 538 * POSIX suggests substracting 2048 bytes from sysconf(_SC_ARG_MAX)
544 arg_max = ARG_MAX - 2048; 539 * so that the process may safely modify its environment.
545#endif 540 */
546 if (arg_max > 0 && n_max_chars > arg_max) 541 n_max_chars -= 2048;
547 n_max_chars = arg_max; 542
548 }
549 if (opt & OPT_UPTO_SIZE) { 543 if (opt & OPT_UPTO_SIZE) {
550 n_max_chars = xatou_range(max_chars, 1, INT_MAX); 544 n_max_chars = xatou_range(max_chars, 1, INT_MAX);
551 } 545 }