Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

touch: exit non-zero on failure #468

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 28 additions & 36 deletions bin/touch
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,27 @@ License: perl


use strict;
use Getopt::Std;

sub parse_time ($);
use File::Basename qw(basename);
use Getopt::Std qw(getopts);

my ($VERSION) = '1.2';

my $warnings = 0;

# Print a usage message on a unknown option.
# Requires my patch to Getopt::Std of 25 Feb 1999.
$SIG {__WARN__} = sub {
require File::Basename;
$0 = File::Basename::basename ($0);
if (substr ($_ [0], 0, 14) eq "Unknown option") {
warn <<EOF;
$0 (Perl bin utils) $VERSION
$0 [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file [files ...]
EOF
exit;
}
else {
$warnings = 1;
warn "$0: @_";
}
};
use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

my $Program = basename($0);

$SIG {__DIE__} = sub {
require File::Basename;
$0 = File::Basename::basename ($0);
die "$0: @_";
};
sub parse_time ($);

my ($VERSION) = '1.3';

# Get the options.
getopts ('acmfr:t:', \my %options);
my $rc = EX_SUCCESS;

warn "Unknown option" unless @ARGV;
my %options;
getopts('acmfr:t:', \%options) or usage();
unless (@ARGV) {
warn "$Program: missing file agument\n";
usage();
}

my $access_time = exists $options {a} || !exists $options {m};
my $modification_time = exists $options {m} || !exists $options {a};
Expand Down Expand Up @@ -76,7 +61,8 @@ foreach my $file (@ARGV) {
local *FILE;
require Fcntl; # Import
sysopen FILE, $file, Fcntl::O_CREAT () or do {
warn "$file: $!\n";
warn "$Program: $file: $!\n";
$rc = EX_FAILURE;
next;
};
close FILE;
Expand All @@ -86,21 +72,27 @@ foreach my $file (@ARGV) {
}

my ($aorig, $morig) = (stat $file) [8, 9] or do {
warn "$file: $!\n";
warn "$Program: $file: $!\n";
$rc = EX_FAILURE;
next;
};

my $aset = $access_time ? $atime : $aorig;
my $mset = $modification_time ? $mtime : $morig;

utime $aset, $mset, $file or do {
warn "$file: $!\n";
warn "$Program: $file: $!\n";
$rc = EX_FAILURE;
next;
};
}
exit $rc;


exit $warnings;
sub usage {
warn "$Program (Perl bin utils) $VERSION\n";
warn "usage: $Program [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file...\n";
exit EX_FAILURE;
}

sub parse_time ($) {
my $time = shift;
Expand Down