summaryrefslogtreecommitdiff
blob: 11998be1fddfebb72bc47fe5bbe3d62e12401341 (plain)
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
84
85
86
87
88
* Fix http://bugzilla.redhat.com/57508 (make dos2unix not modify Mac
  files unless in mac2unix mode)
* Make mac2unix mode not create duplicate Unix line delimiters when
  run on a DOS file. (mschwendt@users.sf.net)

diff -Nur dos2unix-3.1-orig/dos2unix.c dos2unix-3.1/dos2unix.c
--- dos2unix-3.1-orig/dos2unix.c	1998-11-19 13:19:25.000000000 +0100
+++ dos2unix-3.1/dos2unix.c	2004-09-26 20:57:41.606587616 +0200
@@ -153,6 +153,24 @@
 }
 
 
+void StripDelimiter(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, int CurChar)
+{
+  int TempNextChar;
+  /* Don't modify Mac files when in dos2unix mode. */
+  if ( (TempNextChar = getc(ipInF)) != EOF) {
+    ungetc( TempNextChar, ipInF );  /* put back peek char */
+    if ( TempNextChar != '\x0a' ) {
+      putc( CurChar, ipOutF );  /* Mac line, put back CR */
+    }
+  }
+  else if ( CurChar == '\x0d' ) {  /* EOF: last Mac line delimiter (CR)? */
+    putc( CurChar, ipOutF );
+  }
+  if (ipFlag->NewLine) {  /* add additional LF? */
+    putc('\n', ipOutF);
+  }
+}
+
 /* converts stream ipInF to UNIX format text and write to stream ipOutF
  * RetVal: 0  if success
  *         -1  otherwise
@@ -161,6 +179,7 @@
 {
     int RetVal = 0;
     int TempChar;
+    int TempNextChar;
     
     if ( macmode )
       ipFlag->ConvMode = 3;
@@ -177,9 +196,7 @@
 		break;
 	      } 
 	    } else {
-	      if (ipFlag->NewLine) {
-		putc('\n', ipOutF);
-	      }
+	      StripDelimiter( ipInF, ipOutF, ipFlag, TempChar );
 	    }
 	  }
 	  break;
@@ -193,9 +210,7 @@
 		break;
 	      }
 	    } else {
-	      if (ipFlag->NewLine) {
-		putc('\n', ipOutF);
-	      }
+	      StripDelimiter( ipInF, ipOutF, ipFlag, TempChar );
 	    }
 	  }
 	  break;
@@ -209,9 +224,7 @@
 		break;
 	      }
 	    } else {
-	      if (ipFlag->NewLine) {
-		putc('\n', ipOutF);
-	      }
+	      StripDelimiter( ipInF, ipOutF, ipFlag, TempChar );
 	    }
 	  }
 	  break;
@@ -227,6 +240,13 @@
 		}
 	      }
 	    else{
+	      if ( (TempNextChar = getc(ipInF)) != EOF) {
+		ungetc( TempNextChar, ipInF );  /* put back peek char */
+		/* Don't touch this delimiter if it's a CR,LF pair. */
+		if ( TempNextChar == '\x0a' ) {
+		  continue;
+		}
+	      }
 	      if (putc('\x0a', ipOutF) == EOF)
 		{
 		  RetVal = -1;