From 724c4ab119f8c1ef5b589d13ce4b177acdbcb2ee Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Thu, 14 Dec 2023 13:30:28 +0800 Subject: [PATCH] apply: capture system() failure * According with OpenBSD, "apply 1 2 3" should print an error message each time "1" command fails to run, then exit with error code * "apply false 2 3" should not print an error message because "false" runs successfully, then exit with error code * The return value from system() was not checked previously so no feedback was provided if something went wrong * Prefix "usage:" to the usage string * Bump version --- bin/apply | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/bin/apply b/bin/apply index 576380d6..bc8c2521 100755 --- a/bin/apply +++ b/bin/apply @@ -14,20 +14,13 @@ License: perl use strict; +use File::Basename qw(basename); + use constant EX_SUCCESS => 0; use constant EX_FAILURE => 1; -my ($VERSION) = '1.2'; - -sub usage () { - require File::Basename; - $0 = File::Basename::basename ($0); - print <= $argc) { if (@thingies) { (my $new_command = $command) =~ s/${magic}(\d+)/$ARGV [$1 - 1]/ge; - system $new_command; # Reinterpreted by the shell! + my $rc = system $new_command; # Reinterpreted by the shell! + checkerr($rc); splice @ARGV, 0, $argc; } else { if ($argc) { - system $command, splice @ARGV, 0, $argc; + my $rc = system $command, splice @ARGV, 0, $argc; + checkerr($rc); } else { - system $command; + my $rc = system $command; + checkerr($rc); shift; } } } -exit EX_SUCCESS; +exit $err; + +sub checkerr { + my $status = shift; + if ($status != 0) { + if ($status == -1) { + warn "$Program: command failed: $!\n"; + } + $err = EX_FAILURE; + } +} + +sub usage { + warn "$Program (Perl bin utils) $VERSION\n"; + warn "usage: $Program [-ac] [-#] command argument [argument ...]\n"; + exit EX_FAILURE; +} __END__