3
# Heuristically converts line endings to the current OS's preferred format
5
# All existing line endings must be identical (e.g. lf's only, or even
6
# the accedental cr.cr.lf sequence.) If some lines end lf, and others as
7
# cr.lf, the file is presumed binary. If the cr character appears anywhere
8
# except prefixed to an lf, the file is presumed binary. If there is no
9
# change in the resulting file size, or the file is binary, the conversion
12
# Todo: Handle NULL stdin characters gracefully.
18
# The ignore list is '-' seperated, with this leading hyphen and
19
# trailing hyphens in ever concatinated list below.
23
$ignore .= "gif-jpg-jpeg-png-ico-bmp-";
26
$ignore .= "tar-gz-z-zip-jar-war-bz2-tgz-";
28
# Many document formats
29
$ignore .= "eps-psd-pdf-ai-";
32
$ignore .= "ucs2-ucs4-";
35
$ignore .= "class-so-dll-exe-obj-a-o-lo-slo-sl-dylib-";
37
# Some build env files
38
$ignore .= "mcp-xdc-ncb-opt-pdb-ilk-sbr-";
48
while (defined @ARGV[0]) {
49
if (@ARGV[0] eq '--touch') {
52
elsif (@ARGV[0] eq '--nocr') {
55
elsif (@ARGV[0] eq '--cr') {
58
elsif (@ARGV[0] eq '--force') {
61
elsif (@ARGV[0] eq '--FORCE') {
64
elsif (@ARGV[0] =~ m/^-/) {
65
die "What is " . @ARGV[0] . " supposed to mean?\n\n"
66
. "Syntax:\t$0 [option()s] [path(s)]\n\n" . <<'OUTCH'
67
Where: paths specifies the top level directory to convert (default of '.')
72
--touch the datestamp (default: keeps date/attribs)
73
--force mismatched corrections (unbalanced ^M's)
74
--FORCE all files regardless of file name!
79
find(\&totxt, @ARGV[0]);
80
print "scanned " . @ARGV[0] . "\n";
98
if ($forceending < 2) {
99
while ($#exts && ($ext = pop(@exts))) {
100
if ($ignore =~ m|-$ext-|i) {
105
@ostat = stat($oname);
106
$srcfl = new IO::File $oname, "r" or die;
107
$dstfl = new IO::File $tname, "w" or die;
114
if (s/(\r*)\n$/\n/) {
119
if (!$forceending && (($n != $t) || m/\r/)) {
120
print "mismatch in " .$oname. ":" .$n. " expected " .$t. "\n";
124
elsif ($notnative > 0) {
130
if (defined $t && (tell $srcfl == tell $dstfl)) {
136
unlink $oname or die;
137
rename $tname, $oname or die;
140
utime $ostat[9], $ostat[9], @anames;
142
chmod $ostat[2] & 07777, @anames;
143
chown $ostat[5], $ostat[6], @anames;
144
print "Converted file " . $oname . " to text in " . $File::Find::dir . "\n";
147
unlink $tname or die;