-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.pl
executable file
·156 lines (144 loc) · 5.17 KB
/
clean.pl
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
#!/usr/bin/perl
######################################################################
# @(#)clean.pl 1.5.4 ([email protected]) 05/20/96
#
# AUTHOR: Leif Hedstrom <[email protected]>
#
# SYNOPSIS:
# clean [ -adefmnpruvFT] [-i [yn]] [-s string] [names ...]
#
# HISTORY:
# 21-Jul-1991 Leif Initial version
# 23-Mar-1992 Leif Bugs, makeregexp, framemaker, -b option, ver 1.1
# 5-Apr-1992 Leif Added -a and -n option, ver 1.2
# 26-Sep-1993 Leif Added -u and cleaned man page, ver 1.3
# 25-Jan-1994 Leif Fixed NFS mount points traversal, fixed a bug in
# answer, added -T, cleaned up, ver 1.4
# 5-Feb-1994 Leif Added -m for deleted MH files, performance boost,
# ignore symbolic links, ver. 1.5
# 22-Oct-1994 Leif Fixed %-bug, ver. 1.5.1
# 16-Jan-1995 Leif Works with Perl ver. 5.000 now.
# 22-Jan-1995 Leif Added `-c' option, and changed `%' matching.
# 20-May-1996 Leif Added `-e' switch, to remove emacs .save files. I
# also moved the other emacs regexp's to $emexp.
# 14-Mar-2012 Leif Wow, I've used this tool for 21 years... It finally
# broke, sort of, with the deprecation of getopts.pl.
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use Getopt::Std;
#
# Global variables.
#
$USAGE = "clean [ -acdefmnprtvuFT] [-i [yn]] [-s string] [names ...]";
$stdexp = '[^%]*\%$|^core$';
$emexp = '^\.saves-[0-9]+|.*\~$|^\#.*\#$';
$texexp = '.*\.(cp|fn|ky|pg|tp|vr|aux|log|toc|fns)$';
$sysexp = '^\.nfs.*';
$framexp = '.*\.backup$|.*\.lck';
$mhexp = '^[,#][0-9]+$';
%anslist = ('^[yY]([eE][sS])?$', 1, '^[Nn]([oO])?$', 0);
#
# Subroutines.
#
sub answer
{ # answer(STR prompt, STR default)
local ($regexp); # STR: Holds a reg. exp.
local ($retval); # INT: Return value of reg. exp.
local ($value) = -1; # INT: Calculated return value
print $_[0];
chop($_ = <>);
$_ = $_[1] if /^$/o; # Default choice
while (($regexp, $retval) = each %anslist) {
$value = $retval if $_ =~ /$regexp/;
}
return $value;
}
sub makeregexp
{ # makeregexp(STR regexp)
$_ = "^core$|" if $opt_c;
$_ = "$stdexp|" if $opt_d || $opt_a;
$_ .= "$texexp|" if $opt_t || $opt_a;
$_ .= "$framexp|" if $opt_f || $opt_a;
$_ .= "$sysexp|" if $opt_u || $opt_a;
$_ .= "$mhexp|" if $opt_m || $opt_a;
$_ .= "$emexp|" if $opt_e || $opt_a;
$_ .= $_[0] if $_[0];
chop if !$_[0];
$_ = $stdexp if $_ eq "";
s/ //g; # Don't allow space in filenames
$stdexp = $_;
print "Searchstring is: $stdexp\n" if $opt_v;
}
sub handledir
{ # handledir(STR directory)
local (@files); # ARR[STR]: Files in directory
if (opendir(thedir, $_[0]) == 0) {
print STDERR "WARNING:can't access directory $_[0]\n";
return 0;
}
print "Now processing directory \'$_[0]\'\n" if $opt_v;
@files = readdir(thedir); # No close, will be done on next open!
grep(substr($_, 0, 0) = "$_[0]/", @files);
foreach (@files) {
next if /\/\.$/ || /\/\.\.$/;
if (-d) {
next if (-l && !$opt_F);
next if (!$opt_T && ($org_dev != (stat(_))[0]));
&handledir($_) if $opt_r;
} else {
&handlefile($_);
}
}
}
sub handlefile
{ # handlefile(STR file)
$file = substr($_[0], rindex($_[0], "/") + 1);
if ($file =~ /$stdexp/o) {
print "$_[0]\n" if (!$opt_i && ($opt_p || $opt_v || $opt_n));
return if $opt_n;
if ($opt_i) { # Interactive mode
$key = -1;
$key = &answer("Delete $_[0] [$opt_i]? ", "$opt_i") while $key < 0;
return if ($key != 1); # Don't remove anything!
}
unlink $_[0] || print STDERR "Could not delete $_[0]\n";
}
}
#
# Here starts the main program.
#
unshift(@ARGV, split(/ /, $ENV{"CLEAN_OPTIONS"})); # Env. options
&getopts('acdefmnprtuvFTi:s:') || die "Usage: $USAGE\n";
$opt_i = 'n' if (defined($opt_i) && $opt_i eq "");
&makeregexp($opt_s);
if ($#ARGV < $[) {
($org_dev) = stat(".");
&handledir(".");
} else {
foreach (@ARGV) {
if (-f) {
($org_dev) = stat(".");
&handlefile($_);
} elsif (-d) { # Recursive
($org_dev) = stat(_);
&handledir($_);
} else {
print STDERR "$_: file not found!\n";
}
}
}