[PATCH] xargs: support -a args-file

Johannes Schindelin johannes.schindelin at gmx.de
Fri Aug 25 20:42:05 UTC 2017


The GNU-specific option -a lets xargs read the arguments from a file
rather than from stdin.

This is particularly convenient when debugging in gdb interactively,
and it might be of more general use.

function                                             old     new   delta
xargs_main                                           841     923     +82
.rodata                                           164296  164330     +34
packed_usage                                       32073   32085     +12
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 128/0)             Total: 128 bytes

Signed-off-by: Johannes Schindelin <johannes.schindelin at gmx.de>
---

	As stated in the commit message, this was used for debugging, so I
	could imagine that we'd want it to default to N...

 findutils/xargs.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/findutils/xargs.c b/findutils/xargs.c
index 77e01ef6c..31f33c697 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -64,6 +64,11 @@
 //config:	bool "Enable -P N: processes to run in parallel"
 //config:	default y
 //config:	depends on XARGS
+//config:
+//config:config FEATURE_XARGS_SUPPORT_ARGS_FILE
+//config:	bool "Enable --arg-file PATH: use file instead of stdin"
+//config:	default y
+//config:	depends on XARGS
 
 //applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
 
@@ -517,6 +522,9 @@ static int xargs_ask_confirmation(void)
 //usage:	IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
 //usage:     "\n	-0	Input is separated by NUL characters"
 //usage:	)
+//usage:	IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(
+//usage:     "\n	-a PATH	Read args from file instead of stdin"
+//usage:	)
 //usage:     "\n	-t	Print the command on stderr before execution"
 //usage:     "\n	-e[STR]	STR stops input processing"
 //usage:     "\n	-n N	Pass no more than N args to PROG"
@@ -565,7 +573,8 @@ enum {
 	IF_FEATURE_XARGS_SUPPORT_TERMOPT(     "x") \
 	IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(   "0") \
 	IF_FEATURE_XARGS_SUPPORT_REPL_STR(    "I:i::") \
-	IF_FEATURE_XARGS_SUPPORT_PARALLEL(    "P:+")
+	IF_FEATURE_XARGS_SUPPORT_PARALLEL(    "P:+") \
+	IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(   "a:")
 
 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int xargs_main(int argc UNUSED_PARAM, char **argv)
@@ -584,6 +593,7 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
 #else
 #define read_args process_stdin
 #endif
+	IF_FEATURE_XARGS_SUPPORT_PARALLEL(char *opt_a = NULL;)
 
 	INIT_G();
 
@@ -592,6 +602,7 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
 		&max_args, &max_chars, &G.eof_str, &G.eof_str
 		IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
 		IF_FEATURE_XARGS_SUPPORT_PARALLEL(, &G.max_procs)
+		IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(, &opt_a)
 	);
 
 #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
@@ -599,6 +610,16 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
 		G.max_procs = 100; /* let's not go crazy high */
 #endif
 
+#if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE
+	if (opt_a) {
+		int fd = open(opt_a, O_RDONLY);
+		if (fd < 0)
+			bb_perror_msg_and_die("can't open '%s'", opt_a);
+		if (dup2(fd, 0) < 0)
+			bb_perror_msg_and_die("Could not reopen stdin");
+	}
+#endif
+
 	/* -E ""? You may wonder why not just omit -E?
 	 * This is used for portability:
 	 * old xargs was using "_" as default for -E / -e */

base-commit: 14551b7036acf98f81d76674f351ce99148762c8
-- 
2.14.1.windows.1.48.g37d08f901a6

Published-As: https://github.com/dscho/busybox-w32/releases/tag/xargs-a-v1
Fetch-It-Via: git fetch https://github.com/dscho/busybox-w32 xargs-a-v1


More information about the busybox mailing list