File: undos.pl

package info (click to toggle)
acm 5.0-23.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,364 kB
  • ctags: 4,793
  • sloc: ansic: 42,444; makefile: 706; cpp: 293; perl: 280; sh: 198
file content (79 lines) | stat: -rwxr-xr-x 1,499 bytes parent folder | download | duplicates (9)
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
#  undos.pl -- ensure all text files are in UNIX text file format

require 5;

$tmpfile = "undos.tmp";

if ($ARGV[0] eq '-r') {
	shift @ARGV;
	$reverse = 1;
}

sub ProcessFile {
	local ($f) = @_;
	local ($path, $found, @dirlist);

	if ( -d $f ) {
		opendir (dir, $f);
		@dirlist = readdir (dir);
		closedir (dir);
		foreach (@dirlist) {
			next if ($_ eq '.' || $_ eq '..');

			$path = $f . "/" . $_;

			if ($_ =~ /\.[cC]$/ ||
				$_ =~ /\.[xX]$/ ||
				$_ =~ /\.[hH]$/ ||
				$_ =~ /\.[iI][nN]$/ ||
				$_ eq "README" ||
				$_ eq "inventory" ||
				$_ =~ /-scene$/ ||
				$_ =~ /\.[cC][pP][pP]$/ ||
				$_ =~ /\.[pP][lL]$/ ) {
				ProcessFile ( $path );
			}
			elsif ( -d $path ) {
				ProcessFile ( $path );
			}
		}
			
	}
	else {
		$found = 0;
		open (src, $f) || die "unable to open input: $f";
		binmode src;
		open (dst, ">$tmpfile") || die "unable to open temporary outfile";
		binmode dst;
		while (<src>) {
			$last = chop;
			$_ = $_ . $last if ($last ne "\n");
			if ($reverse) {
			if ( substr($_, length($_) - 1, 1) ne "\r" ) {
				++ $found;
				$_ = $_ . "\r";
			}
			}
			else {
			if ( substr($_, length($_) - 1, 1) eq "\r" ) {
				++ $found;
				chop;
			}
			}	
			print dst $_, "\n";
		}
		close (src);
		close (dst);
		if ($found != 0) {
			print "Converted $found line(s) in $f\n";
			rename ($f, $f . ".orig") || die "rename failed";
			rename ($tmpfile, $f) || die "rename failed";
#			unlink $tmpfile;
		}
		else {
			unlink $tmpfile;
		}
	}
}

ProcessFile ($ARGV[0])