#!/usr/local/bin/perl # recursive flip to unix, pc, or mac EOL's # internal codes: 1 2 3 (respectively) $outmode = 1; $dirsok = 0; @modestring = ("","\012","\015\012","\015"); if ($#ARGV < 0) {print STDERR "Usage: flip.pl [ -u | -p | -m ] file [, file...] Flips the EOL convention of a text file to UNIX (-u), PC (-p), or Mac (-m) Routine will query if OK to recurse when a directory is first found in the argument list. File access and modification times are preserved.\n"; exit;} while ($rg = shift(@ARGV)) { if ($rg =~ m/^-u/) {$outmode = 1;} elsif ($rg =~ m/^-U/) {$outmode = 1;} elsif ($rg =~ m/^-p/) {$outmode = 2;} elsif ($rg =~ m/^-P/) {$outmode = 2;} elsif ($rg =~ m/^-m/) {$outmode = 3;} elsif ($rg =~ m/^-M/) {$outmode = 3;} elsif (-f $rg) {&flipme($rg,$outmode);} elsif (-d $rg) { if ($dirsok == 0) { print STDERR "\nOK to recurse into directories (e.g., $rg)? [y/n] "; $lin = ; $lin =~ tr/A-Z/a-z/; if ($lin =~ m/^y/) {$dirsok = 1;} } if ($dirsok == 1) {&dodir($rg,$outmode);} } else {print STDERR "\nUnknown argument $rg";} } print STDERR "\n"; sub dodir { local ($dirin,$outmode) = @_; local ($file,$glob); print STDERR "/"; $glob = "$dirin/*"; foreach $file (<${glob}>) { if (-d $file) {&dodir($file,$outmode);} if (-f $file) {&flipme($file,$outmode);} } } sub flipme { local ($filename,$outmode) = @_; print STDERR "."; undef($/); chmod 0777, $filename; open(IN,"<$filename"); binmode(IN); while () { $contents = $_; } close(IN); $cntm = ($contents =~ tr/\015/\015/); $cntj = ($contents =~ tr/\012/\012/); $cntx = ($contents =~ tr/\030/\030/); if ($cntx > 0) { print STDERR "\nSkipping $filename: binary file (contains \\030 chars)"; return 0; } if ($cntm > 0 && $cntj == 0) {$inmode = 3;} elsif ($cntj > 0 && $cntm == 0) {$inmode = 1;} elsif ($cntj > 0 && $cntm == $cntj) {$inmode = 2;} else { print STDERR "\nSkipping $filename: binary file (not pure text EOLs)"; return 0; } if ($inmode == $outmode) {return 0;} $instr = $modestring[$inmode]; $outstr = $modestring[$outmode]; $contents =~ s/$instr/\030/g; $contents =~ s/\030/$outstr/g; ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, $ctime,$blksize,$blocks) = stat($filename); open(OUT, ">$filename"); binmode(OUT); print OUT "$contents"; close(OUT); utime($atime, $mtime, $filename); }