Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Because different operating systems use different EOL conventions, when moving text files from one system to another, you must perform an EOL conversion. This script shows you one way of doing this.
1 use strict;
2 use warnings;
3
4 sub usage()
5 {
6 print STDERR "Usage $0 <unix|linux|dos|mac|apple>\n";
7 exit(8);
8 }
9
10 binmode(STDIN);
11 binmode(STDOUT);
12
13 my $eol = "\n";
14
15 if ($#ARGV != 0) {
16 usage();
17 }
18 if ($ARGV[0] eq "linux") {
19 $eol = "\n";
20 } elsif ($ARGV[0] eq "unix") {
21 $eol = "\n";
22 } elsif ($ARGV[0] eq "dos") {
23 $eol = "\r\n";
24 } elsif ($ARGV[0] eq "apple") {
25 $eol = "\r";
26 } elsif ($ARGV[0] eq "mac") {
27 $eol = "\r";
28 } else {
29 usage();
30 }
31
32 while (1) {
33 my $ch; # Character from the input
34
35 # Read a character
36 my $status = sysread(STDIN, $ch, 1);
37 if ($status <= 0) {
38 last;
39 }
40
41 if ($ch eq "\n") {
42 syswrite(STDOUT, $eol);
43 next;
44 }
45
46 if ($ch eq "\r") {
47 my $next_ch; # Check for \r\n
48 $status = sysread(STDIN, $next_ch, 1);
49 if ($status <= 0) {
50 syswrite(STDOUT, $eol);
51 last;
52 }
53
54 # Check for \r\n
55 if ($next_ch eq "\n") {
56 syswrite(STDOUT, $eol);
57 next;
58 }
59
60 syswrite(STDOUT, $eol);
61 $ch = $next_ch;
62 }
63 syswrite(STDOUT, $ch);
64 }