-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdos2unix.pl
84 lines (74 loc) · 2.07 KB
/
dos2unix.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
#!/usr/bin/perl -w
# $Id: dos2unix.pl,v 1.2 2007/04/02 20:29:20 gumpu Exp $
#
# Convert all files to the unix-line-end convention.
# This ensures that all files in the ROBODoc package
# follow the same convention.
#
use strict;
use IO::File;
use IO::Dir;
sub fix
{
my $path = shift;
my $mode = shift;
my $file;
if ( -T $path ) {
if ( !( $file = IO::File->new($path, "r") ) ) {
print "Can't open $path to read : $!\n";
} else {
# Open in binmode otherwise Perl will do the cr/lf for
# us while reading.
binmode( $file );
my @file_data = <$file>;
$file->close();
if ( !( grep { /\r/ } @file_data ) ) {
print "$path is OK\n";
# File is OK
} else {
print "$path contains CR/LF\n";
if ( $mode eq "test" ) {
print "$path contains CR/LF\n";
} else {
print "Fixing: $path\n";
map { s/\r//g; } (@file_data);
$file = IO::File->new("> $path") or die "Can't open $path to write : $!";
binmode( $file );
print $file @file_data;
$file->close();
}
}
}
}
}
sub scan_directory
{
my $dirname = shift;
my $mode = shift;
my $path;
my $dir = IO::Dir->new($dirname) or die "Can't open $dirname : $!";
my @files = $dir->read();
$dir->close();
foreach my $filename ( sort @files ) {
$path = "$dirname/$filename";
if ( -f $path ) {
fix( $path, $mode );
}
}
# Also fix any subdirectories.
foreach my $subdirname ( sort @files ) {
$path = "$dirname/$subdirname";
if ( -d $path ) {
unless ( $subdirname =~ m/^\.+$/ ) {
scan_directory( $path, $mode );
}
}
}
}
sub main
{
my $out = IO::File->new(">fl.txt") or die "can't open fl.txt : $!";
scan_directory( ".", "test" );
$out->close();
}
main;