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-chm-ai-";
32 $ignore .= "ucs2-ucs4-";
35 $ignore .= "class-so-dll-exe-obj-lib-a-o-lo-slo-sl-dylib-";
37 # Some build env files
38 $ignore .= "mcp-xdc-ncb-opt-pdb-ilk-exp-res-pch-idb-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 return if ($File::Find::dir =~ m|^(.+/)?.svn(/.+)?$|);
106 @ostat = stat($oname);
107 $srcfl = new IO::File $oname, "r" or die;
108 $dstfl = new IO::File $tname, "w" or die;
115 if (s/(\r*)\n$/\n/) {
120 if (!$forceending && (($n != $t) || m/\r/)) {
121 print "mismatch in " .$oname. ":" .$n. " expected " .$t. "\n";
125 elsif ($notnative > 0) {
131 if (defined $t && (tell $srcfl == tell $dstfl)) {
137 unlink $oname or die;
138 rename $tname, $oname or die;
141 utime $ostat[9], $ostat[9], @anames;
143 chmod $ostat[2] & 07777, @anames;
144 chown $ostat[5], $ostat[6], @anames;
145 print "Converted file " . $oname . " to text in " . $File::Find::dir . "\n";
148 unlink $tname or die;