Skip to content

Commit

Permalink
Merge pull request #248 from mknos/xargs-forever
Browse files Browse the repository at this point in the history
xargs: infinite loop with -n
  • Loading branch information
briandfoy authored Sep 19, 2023
2 parents 4d6c214 + 7434a6c commit 08b048a
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions bin/xargs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ License:
# Gurusamy Sarathy <[email protected]>
#

use File::Basename qw(basename);
use Getopt::Std;
use Text::ParseWords;

use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

my $Program = basename($0);

my %o;
getopts('tn:l:s:I:', \%o) or die <<USAGE;
Usage:
$0 [-t] [-n num] [-l num] [-s size] [-I repl] prog [args]
$Program [-t] [-n num] [-l num] [-s size] [-I repl] prog [args]
-t trace execution (prints commands to STDERR)
-n num pass at most 'num' arguments in each invocation of 'prog'
Expand All @@ -33,6 +39,18 @@ Usage:
before execution
USAGE

for my $opt (qw( l n s )) {
next unless (defined $o{$opt});
if (!length($o{$opt}) || $o{$opt} =~ m/\D/) {
warn "$Program: option $opt: invalid number '$o{$opt}'\n";
exit EX_FAILURE;
}
if ($o{$opt} == 0) {
warn "$Program: option $opt: number must be > 0\n";
exit EX_FAILURE;
}
}

my @args = ();

$o{I} ||= '{}' if exists $o{I};
Expand All @@ -53,15 +71,15 @@ while (1) {
my @run = @ARGV;
push @run, 'echo' unless (@run);
if ($o{I}) {
exit(0) unless length $line;
exit(EX_SUCCESS) unless length $line;
for (@run) { s/\Q$o{I}\E/$line/g; }
}
elsif ($o{n}) {
exit(0) unless @args;
exit(EX_SUCCESS) unless @args;
push @run, splice(@args, 0, $o{n});
}
else {
exit(0) unless @args;
exit(EX_SUCCESS) unless @args;
push @run, @args;
@args = ();
}
Expand Down

0 comments on commit 08b048a

Please sign in to comment.