/
touch
executable file
·219 lines (152 loc) · 4.89 KB
/
touch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/perl
=begin metadata
Name: touch
Description: change access and modification times of files
Author: Abigail, perlpowertools@abigail.be
License: perl
=end metadata
=cut
use strict;
use File::Basename qw(basename);
use Getopt::Std qw(getopts);
use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;
my $Program = basename($0);
sub parse_time ($);
my ($VERSION) = '1.3';
my $rc = EX_SUCCESS;
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};
my $no_create = exists $options {c};
my ($atime, $mtime, $special_time);
if ($options {r}) {
($atime, $mtime) = (stat $options {r}) [8, 9] or die "$options{r}: $!\n";
$special_time = 1;
}
elsif ($options {t}) {
$atime = $mtime = parse_time $options {t};
die "-t $options{t}: Time out of range!\n" if $atime < 0;
$special_time = 1;
}
else {
$atime = $mtime = time;
}
foreach my $file (@ARGV) {
# Check if the file exists. If not, create it.
unless (-e $file) {
next if $no_create;
local *FILE;
require Fcntl; # Import
sysopen FILE, $file, Fcntl::O_CREAT () or do {
warn "$Program: $file: $!\n";
$rc = EX_FAILURE;
next;
};
close FILE;
# Nothing to be done, unless time different than now.
next unless $special_time;
}
my ($aorig, $morig) = (stat $file) [8, 9] or do {
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 "$Program: $file: $!\n";
$rc = EX_FAILURE;
next;
};
}
exit $rc;
sub usage {
warn "$Program (Perl bin utils) $VERSION\n";
warn "usage: $Program [-acm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file...\n";
exit EX_FAILURE;
}
sub parse_time ($) {
my $time = shift;
my ($first, $seconds) = split /\./ => $time, 2;
my $year;
if ($first =~ /\D/) {die "$time: Illegal time format\n"}
elsif (12 == length $first) {
$year = substr $first, 0, 4, '';
}
elsif (10 == length $first) {
$year = substr $first, 0, 2, '';
$year += 100 if $year < 70;
}
elsif ( 8 == length $first) {
$year = (localtime) [5];
}
else {die "$time: Illegal time format\n"}
if (defined $seconds &&
($seconds =~ /\D/ || $seconds > 59 || 2 != length $seconds)) {
die "-t $time: Illegal time format\n"
}
else {
$seconds = 0;
}
my ($mon, $day, $hours, $minutes) = $first =~ /(..)(..)(..)(..)/;
require Time::Local;
Time::Local::timelocal ($seconds, $minutes, $hours, $day, $mon - 1, $year);
}
__END__
=pod
=head1 NAME
touch - change access and modification times of files
=head1 SYNOPSIS
touch [-acm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file...
=head1 DESCRIPTION
I<touch> sets the access and modification timestamps of files. By default,
both the access and modification times are set to the current time.
If necessary, files will be created.
I<touch> exits successfully if and only if all timestamps could be set
successfully.
=head2 OPTIONS
I<touch> accepts the following options:
=over 4
=item -a
Set the access time of the specified files only, unless I<-m> is given as
well.
=item -c
Do not create non-existing files. No warning is generated, and it will
not influence the exit status.
=item -f
This option is ignored, and only recognized for compatability reasons.
=item -m
Set the modification time of the specified files only, unless I<-a> is
given as well.
=item -r file
Use the access and modification time of I<file> instead of the
current time.
=item -t [[CC]YY]MMDDhhmm[.SS]
Set the access and modification times to the specified time.
If B<YY> is present, but B<CC> is not, then B<CC> is assumed to
be 19 if B<YY> is larger than 69, and 20 otherwise. If B<YY>
is not present, the current year is assumed.
It should be noted that many systems cannot deal with timestamps
before Jan 1, 1970 or after Jan 19, 2038.
=back
=head1 ENVIRONMENT
The working of I<touch> is not influenced by any environment variables.
=head1 BUGS
I<touch> uses C<Time::Local> to translate the time format to epoch
seconds
=head1 STANDARDS
This I<touch> implementation is compatible with the B<OpenBSD> implementation.
=head1 AUTHOR
The Perl implementation of I<touch> was written by Abigail, I<perlpowertools@abigail.be>.
=head1 COPYRIGHT and LICENSE
This program is copyright by Abigail 1999.
This program is free and open software. You may use, copy, modify, distribute
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others to do the same.
=cut