---
 tests/xargs.test   | 2 ++
 toys/posix/xargs.c | 8 ++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
From 4f06d35448daf06e538a7f71ecd43ac978ff2b75 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Wed, 5 Nov 2025 11:50:17 -0500
Subject: [PATCH] xargs: add --process-slot-var.

---
 tests/xargs.test   | 2 ++
 toys/posix/xargs.c | 8 ++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/tests/xargs.test b/tests/xargs.test
index f4875997..c5f4edad 100644
--- a/tests/xargs.test
+++ b/tests/xargs.test
@@ -82,6 +82,8 @@ testing "parallel sleep" "
   B=\`xargs_sleep -P 2\` &&
   C=\`xargs_sleep -P 0\` &&
   [ \${A} -gt \${B} -a \${B} -gt \${C} ] && echo OK || echo FAIL" "OK\n" "" ""
+testing "--process-slot-var" "xargs -n 1 -P 1 --process-slot-var=V printenv V"\
+  "0\n0\n0\n" "" "one\ntwo\nthree\n"
 
 # TODO: what exactly is -x supposed to do? why does coreutils output "one"?
 #testing "-x" "xargs -x -s 9 || echo expected" "one\nexpected\n" "" "one\ntwo\nthree"
diff --git a/toys/posix/xargs.c b/toys/posix/xargs.c
index 6b65c744..7e5326bc 100644
--- a/toys/posix/xargs.c
+++ b/toys/posix/xargs.c
@@ -9,7 +9,7 @@
  * TODO: -L	Max number of lines of input per command
  * TODO: -x	Exit if can't fit everything in one command
 
-USE_XARGS(NEWTOY(xargs, "^a:E:P#<0(null)=1optr(no-run-if-empty)n#<1(max-args)s#0[!0E]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_XARGS(NEWTOY(xargs, "^(process-slot-var):a:E:P#<0(null)=1optr(no-run-if-empty)n#<1(max-args)s#0[!0E]", TOYFLAG_USR|TOYFLAG_BIN))
 
 config XARGS
   bool "xargs"
@@ -31,6 +31,8 @@ config XARGS
     -r	Don't run with empty input (otherwise always run command once)
     -s	Size in bytes per command line
     -t	Trace, print command line to stderr
+
+    --process-slot-var NAME	Set environment variable NAME in children
 */
 
 #define FOR_xargs
@@ -38,7 +40,7 @@ config XARGS
 
 GLOBALS(
   long s, n, P;
-  char *E, *a;
+  char *E, *a, *process_slot_var;
 
   long entries, bytes, np;
   char delim;
@@ -208,6 +210,8 @@ void xargs_main(void)
 
     if (!(pid = XVFORK())) {
       if (!TT.a) close(0);
+      if (TT.process_slot_var)
+        xsetenv(xmprintf("%s=%ld", TT.process_slot_var, TT.np), 0);
       xopen_stdio(FLAG(o) ? "/dev/tty" : "/dev/null", O_RDONLY|O_CLOEXEC);
       xexec(out);
     }
-- 
2.51.2.1026.g39e6a42477-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to