auto/share/dist/App-RegexFileUtils/ppt/rm.pl untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 169 CC 20 SCAR 33.7
CC
20
Cov
0%
removeFile
56.0
removeDirectory
30.0
overWriteFile
30.0
CRAP 29.1
1 #############################################################################
2 # A simple compatible version of rm( 1 ) written in Perl.
3 #############################################################################
4 #
5 # Copyright (c) Steve Kemp 1999, skx@tardis.ed.ac.uk
6 #
7 # To do:-
8 # Currently the interactive and force options are not handled
9 # totally correctly. According to the man pages for RM the
10 # option placement matters, so a -f will override an _earlier_
11 # -i, etc.
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #
27 ##############################################################################
28  
29 # Packages we use.
30 use strict;
31 use Getopt::Std;
32  
33 # Command line arguments, and other variables.
34 use vars qw( $opt_i $opt_f $opt_r $opt_R $opt_P );
35 my $arg = 0;
36  
37 # Get the options.
38 getOptions();
39  
40 #
41 # Process each file named on the command line.
42 foreach $arg ( @ARGV )
43 {
44 processFile( $arg );
45 }
46  
47 #
48 # Attempt to process each file / directory named on the command line.
49 sub processFile()
50 {
51 my ( $fileName )= @_;
52  
53 # See if the file is a directory.
54 if ( ( -d $fileName ) && ( $opt_r || $opt_R ))
55 {
56 # Remove a directory recursively.
57 removeDirectory( $fileName );
58 }
59 elsif ( ( -d $fileName ) && !( $opt_r || $opt_R ) && (!$opt_i ))
60 {
61 rmdir( $fileName );
62 }
63 elsif( -f $fileName )
64 {
65 removeFile( $fileName );
66 }
67 }
68  
69 #
70 # Recursively remove a directory
71 sub removeDirectory( )
72 {
73 my ( $dirName ) = @_;
74 my ( $path );
75  
76 unless (opendir(DIR, $dirName))
77 {
78 warn "Can't open $dirName\n";
79 closedir(DIR);
80 return;
81 }
82  
83 foreach (readdir(DIR))
84 {
85 next if $_ eq '.' || $_ eq '..';
86 $path = "$dirName/$_";
87  
88 if (-d $path)
89 {
90 &removeDirectory($path);
91 }
92 elsif (-f _)
93 {
94 removeFile( $path );
95 }
96 }
97 closedir(DIR);
98  
99 rmdir( $dirName );
100 }
101  
102 #
103 # Remove a file, asking for confirmation, etc, as
104 # necessary
105 sub removeFile( $fileName )
106 {
107 my ( $fileName ) = @_;
108 my $reply;
109  
110 # If its read only, and we're not forcing, and interactive prompt for deletion
111 #
112 if ( ( ! -w $fileName ) && ( !$opt_f ) && ( $opt_i ))
113 {
114 print "$fileName: Read-only ? ";
115 $reply = <STDIN>;
116 if ( $reply =~ /^[Nn]/ )
117 {
118 return;
119 }
120 }
121 elsif ( $opt_i )
122 {
123 print "$fileName: ? ";
124 $reply = <STDIN>;
125 if ( $reply =~ /^[Nn]/ )
126 {
127 return;
128 }
129 }
130  
131 # If we are forcing the delete first change the files mode to allow writes.
132 if ( $opt_f )
133 {
134 my ( $mode ) = "0777";
135 chmod $mode, $fileName;
136 }
137  
138 # Overwrite the file with rubbish before deleting.
139 if ( $opt_P )
140 {
141 overWriteFile( $fileName );
142 }
143  
144 # Delete the file.
145 unlink( $fileName );
146 }
147  
148 #
149 # Overwrite the file specified, first with x00, the xFF, then x00
150 sub overWriteFile( )
151 {
152 my ( $fileName ) = @_;
153 # Info returned from stat
154 my ( $dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size );
155 # Text we print to the file to overwrite its contents
156 my ( $text, $FILEHANDLE, $ff );
157  
158 # We only want the size
159 ( $dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size ) = stat $fileName;
160  
161 $ff = "\0xFF";
162  
163 # Change mode if the file is readonly.
164 if ( !-w $fileName )
165 {
166 my ( $mode ) = "0777";
167 chmod $mode, $fileName;
168 }
169  
170 ## First pass at overwrite
171 if ( open (FILEHANDLE, ">$fileName" ) )
172 {
173 $text = $ff x $size;
174 print FILEHANDLE $text;
175 close ( FILEHANDLE );
176 }
177  
178 ## Second pass at overwrite
179 if ( open (FILEHANDLE, ">$fileName" ) )
180 {
181 $text = "\0" x $size;
182 print $text;
183 print FILEHANDLE $text;
184 close ( FILEHANDLE );
185 }
186  
187 ## Third pass at overwrite
188 if ( open (FILEHANDLE, ">$fileName" ) )
189 {
190 $text = $ff x $size;
191 print FILEHANDLE $text;
192 close ( FILEHANDLE );
193 }
194 }
195  
196 #
197 # Read the options from the command line.
198 sub getOptions()
199 {
200 # Process options, if any.
201 # Make sure defaults are set before returning!
202 return unless @ARGV > 0;
203
204 if ( !getopts( 'ifPrR' ) )
205 {
206 showUsage();
207 }
208 }
209  
210 #
211 # Show the useage
212 sub showUsage()
213 {
214 print << "E-O-F";
215 Usage: rm [-fiPrR] file ...
216 The options are as follows:
217  
218 -f Attempt to remove the files without prompting for confirmation, re-
219 gardless of the file's permissions. If the file does not exist, do
220 not display a diagnostic message or modify the exit status to re-
221 flect an error. The -f option overrides any previous -i options.
222  
223 -i Request confirmation before attempting to remove each file, regard-
224 less of the file's permissions, or whether or not the standard in-
225 put device is a terminal. The -i option overrides any previous -f
226 options.
227  
228 -P Overwrite regular files before deleting them. Files are overwrit-
229 ten three times, first with the byte pattern 0xff, then 0x00, and
230 then 0xff again, before they are deleted.
231  
232 -R Attempt to remove the file hierarchy rooted in each file argument.
233 The -R option implies the -d option. If the -i option is speci-
234 fied, the user is prompted for confirmation before each directory's
235 contents are processed (as well as before the attempt is made to
236 remove the directory). If the user does not respond affirmatively,
237 the file hierarchy rooted in that directory is skipped.
238  
239 The rm utility removes symbolic links, not the files referenced by the
240 links.
241  
242 It is an error to attempt to remove the files ``.'' or ``..''.
243 E-O-F
244 exit;
245 }