| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: touch,v 1.2 2004/08/05 14:17:44 cwest Exp $ | ||
| 5 | # | ||
| 6 | # $Log: touch,v $ | ||
| 7 | # Revision 1.2 2004/08/05 14:17:44 cwest | ||
| 8 | # cleanup, new version number on website | ||
| 9 | # | ||
| 10 | # Revision 1.1 2004/07/23 20:10:18 cwest | ||
| 11 | # initial import | ||
| 12 | # | ||
| 13 | # Revision 1.1 1999/02/28 13:00:24 abigail | ||
| 14 | # Initial revision | ||
| 15 | # | ||
| 16 | # | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use Getopt::Std; | ||
| 20 | |||
| 21 | sub parse_time ($); | ||
| 22 | |||
| 23 | my ($VERSION) = '$Revision: 1.2 $' =~ /([.\d]+)/; | ||
| 24 | |||
| 25 | my $warnings = 0; | ||
| 26 | |||
| 27 | # Print a usuage message on a unknown option. | ||
| 28 | # Requires my patch to Getopt::Std of 25 Feb 1999. | ||
| 29 | $SIG {__WARN__} = sub { | ||
| 30 | require File::Basename; | ||
| 31 | $0 = File::Basename::basename ($0); | ||
| 32 | if (substr ($_ [0], 0, 14) eq "Unknown option") { | ||
| 33 | warn <<EOF; | ||
| 34 | $0 (Perl bin utils) $VERSION | ||
| 35 | $0 [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file [files ...] | ||
| 36 | EOF | ||
| 37 | exit; | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | $warnings = 1; | ||
| 41 | warn "$0: @_"; | ||
| 42 | } | ||
| 43 | }; | ||
| 44 | |||
| 45 | $SIG {__DIE__} = sub { | ||
| 46 | require File::Basename; | ||
| 47 | $0 = File::Basename::basename ($0); | ||
| 48 | die "$0: @_"; | ||
| 49 | }; | ||
| 50 | |||
| 51 | # Get the options. | ||
| 52 | getopts ('acmfr:t:', \my %options); | ||
| 53 | |||
| 54 | warn "Unknown option" unless @ARGV; | ||
| 55 | |||
| 56 | my $access_time = exists $options {a} || !exists $options {m}; | ||
| 57 | my $modification_time = exists $options {m} || !exists $options {a}; | ||
| 58 | my $no_create = exists $options {c}; | ||
| 59 | |||
| 60 | my ($atime, $mtime, $special_time); | ||
| 61 | if ($options {r}) { | ||
| 62 | ($atime, $mtime) = (stat $options {r}) [8, 9] or die "$options{r}: $!\n"; | ||
| 63 | $special_time = 1; | ||
| 64 | } | ||
| 65 | elsif ($options {t}) { | ||
| 66 | $atime = $mtime = parse_time $options {t}; | ||
| 67 | die "-t $options{t}: Time out of range!\n" if $atime < 0; | ||
| 68 | $special_time = 1; | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | $atime = $mtime = time; | ||
| 72 | } | ||
| 73 | |||
| 74 | foreach my $file (@ARGV) { | ||
| 75 | |||
| 76 | # Check if the file exists. If not, create it. | ||
| 77 | unless (-f $file) { | ||
| 78 | next if $no_create; | ||
| 79 | local *FILE; | ||
| 80 | require Fcntl; # Import | ||
| 81 | sysopen FILE, $file, Fcntl::O_CREAT () or do { | ||
| 82 | warn "$file: $!\n"; | ||
| 83 | next; | ||
| 84 | }; | ||
| 85 | close FILE; | ||
| 86 | |||
| 87 | # Nothing to be done, unless time different than now. | ||
| 88 | next unless $special_time; | ||
| 89 | } | ||
| 90 | |||
| 91 | my ($aorig, $morig) = (stat $file) [8, 9] or do { | ||
| 92 | warn "$file: $!\n"; | ||
| 93 | next; | ||
| 94 | }; | ||
| 95 | |||
| 96 | my $aset = $access_time ? $atime : $aorig; | ||
| 97 | my $mset = $modification_time ? $mtime : $morig; | ||
| 98 | |||
| 99 | utime $aset, $mset, $file or do { | ||
| 100 | warn "$file: $!\n"; | ||
| 101 | next; | ||
| 102 | }; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | exit $warnings; | ||
| 107 | |||
| 108 | sub parse_time ($) { | ||
| 109 | my $time = shift; | ||
| 110 | |||
| 111 | my ($first, $seconds) = split /\./ => $time, 2; | ||
| 112 | my $year; | ||
| 113 | if ($first =~ /\D/) {die "$time: Illegal time format\n"} | ||
| 114 | elsif (12 == length $first) { | ||
| 115 | $year = substr $first, 0, 4, ''; | ||
| 116 | } | ||
| 117 | elsif (10 == length $first) { | ||
| 118 | $year = substr $first, 0, 2, ''; | ||
| 119 | $year += 100 if $year < 70; | ||
| 120 | } | ||
| 121 | elsif ( 8 == length $first) { | ||
| 122 | $year = (localtime) [5]; | ||
| 123 | } | ||
| 124 | else {die "$time: Illegal time format\n"} | ||
| 125 | |||
| 126 | if (defined $seconds && | ||
| 127 | ($seconds =~ /\D/ || $seconds > 59 || 2 != length $seconds)) { | ||
| 128 | die "-t $time: Illegal time format\n" | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | $seconds = 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | my ($mon, $day, $hours, $minutes) = $first =~ /(..)(..)(..)(..)/; | ||
| 135 | |||
| 136 | require Time::Local; | ||
| 137 | |||
| 138 | Time::Local::timelocal ($seconds, $minutes, $hours, $day, $mon - 1, $year); | ||
| 139 | } | ||
| 140 | |||
| 141 | __END__ |