aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xscripts/test/xargs.test35
-rw-r--r--toys/xargs.c17
2 files changed, 48 insertions, 4 deletions
diff --git a/scripts/test/xargs.test b/scripts/test/xargs.test
new file mode 100755
index 00000000..2d3946b3
--- /dev/null
+++ b/scripts/test/xargs.test
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+testing "xargs" "xargs && echo yes" "hello\nyes\n" "" "hello"
+testing "xargs spaces" "xargs" \
+ "one two three four\n" "" "one two\tthree \nfour\n\n"
+
+testing "xargs -n 0" "xargs -n 0 2>/dev/null || echo ok" "ok\n" \
+ "" "one \ntwo\n three"
+testing "xargs -n 2" "xargs -n 2" "one two\nthree\n" "" "one \ntwo\n three"
+testing "xargs -n exact match" "xargs -n 3" "one two three\n" "" "one two three"
+testing "xargs2" "xargs -n2" "one two\nthree four\nfive\n" "" \
+ "one two three four five"
+testing "xargs -s too long" "xargs -s 9 echo 2>/dev/null || echo ok" \
+ "one\ntwo\nok\n" "" "one two three"
+testing "xargs -s 13" "xargs -s 13 echo" "one two\nthree\n" "" "one \ntwo\n three"
+testing "xargs -s 12" "xargs -s 12 echo" "one\ntwo\nthree\n" "" "one \ntwo\n three"
+
+touch one two three
+testing "xargs command -opt" "xargs -n2 ls -1" "one\ntwo\nthree\n" "" \
+ "one two three"
+rm one two three
+
+exit
+
+testing "xargs -n exact match"
+testing "xargs -s exact match"
+testing "xargs -s 0"
+testing "xargs -s impossible"
+
+# xargs command_not_found - returns 127
+# xargs false - returns 1
diff --git a/toys/xargs.c b/toys/xargs.c
index 67e88cfc..8b873d57 100644
--- a/toys/xargs.c
+++ b/toys/xargs.c
@@ -6,7 +6,7 @@
*
* See http://opengroup.org/onlinepubs/9699919799/utilities/xargs.html
-USE_XARGS(NEWTOY(xargs, "n#s#0", TOYFLAG_USR|TOYFLAG_BIN))
+USE_XARGS(NEWTOY(xargs, "^I:E:L#ptxrn#<1s#0", TOYFLAG_USR|TOYFLAG_BIN))
config XARGS
bool "xargs"
@@ -16,9 +16,17 @@ config XARGS
Run command line one or more times, appending arguments from stdin.
+ If command exits with 255, don't launch another even if arguments remain.
+
-s Size in bytes per command line
-n Max number of arguments per command
-0 Each argument is NULL terminated, no whitespace or quote processing
+ #-p Prompt for y/n from tty before running each command
+ #-t Trace, print command line to stderr
+ #-x Exit if can't fit everything in one command
+ #-r Don't run command with empty input
+ #-L Max number of lines of input per command
+ #-E stop at line matching string
*/
#include "toys.h"
@@ -64,7 +72,7 @@ static char *handle_entries(char *data, char **entry)
}
if (TT.entries >= TT.max_entries && TT.max_entries)
- return *s ? (char *)1: s;
+ return *s ? s : (char *)1;
if (!*s) break;
save = s;
@@ -106,10 +114,11 @@ void xargs_main(void)
bytes += strlen(toys.optargs[entries]);
// Loop through exec chunks.
- while (!done) {
+ while (data || !done) {
+ char **out;
+
TT.entries = 0;
TT.bytes = bytes;
- char **out;
// Loop reading input
for (;;) {