--- edit.c	2002-08-21 16:41:21.000000000 +0200
+++ /usr/src/mc-4.6.0-pre1a/edit/edit.c	2002-08-22 14:49:34.000000000 +0200
@@ -175,6 +175,25 @@ int init_dynamic_edit_buffers (WEdit * e
 {
     long buf;
     int j, file = -1, buf2;
+    size_t block_count;		/* Number of blocks, each of EDIT_BUF_SIZE size,
+				   this file contains. */
+    char *begp, *endp, *curp;	/* Start, end and current possition in the
+				   current edit buffer. */
+    char *putp, *getp;		/* Current put/get position. */
+    char *crlf_readahead;	/* Pointer used to determine if there is more
+				   than one CR/LF pair following one after the
+				   other. */
+    size_t avail = 0;		/* How many bytes are free from the
+				   CR/LF -> LF translation. */
+    int need_lf;
+    int crlf_maybe = FALSE;	/* See below. */
+    size_t nbytes;		/* Number of bytes - used as argument to memmove,
+				   memcpy (). */
+    int nread = EDIT_BUF_SIZE;	/* Used to check the return value of
+				   mc_read (). */
+    size_t nblkout = 0;		/* The number of output blockd
+				   resulting from the conversion. */
+    
 
     for (j = 0; j <= MAXBUFF; j++) {
 	edit->buffers1[j] = NULL;
@@ -182,34 +201,197 @@ int init_dynamic_edit_buffers (WEdit * e
     }
 
     if (filename)
-	if ((file = mc_open (filename, O_RDONLY)) == -1) {
+	if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
 /* The file-name is printed after the ':' */
 	    edit_error_dialog (_ (" Error "), get_sys_error (catstrs (_ (" Failed trying to open file for reading: "), filename, " ", 0)));
 	    return 1;
 	}
-    edit->curs2 = edit->last_byte;
 
-    buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
-
-    edit->buffers2[buf2] = CMalloc (EDIT_BUF_SIZE);
+    edit->curs2 = edit->last_byte;
 
-    if (filename) {
-	mc_read (file, (char *) edit->buffers2[buf2] + EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE), edit->curs2 & M_EDIT_BUF_SIZE);
-    } else {
-	memcpy (edit->buffers2[buf2] + EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE), text, edit->curs2 & M_EDIT_BUF_SIZE);
-	text += edit->curs2 & M_EDIT_BUF_SIZE;
-    }
+    /* Read all data in a single loop then further distrubute it throughout the
+       buffers .*/
+    block_count = (edit->curs2 >> S_EDIT_BUF_SIZE) + 1;
+    
+    /* To seamlessly integrate the CR/LF code I had to made the following
+       changes:
 
-    for (buf = buf2 - 1; buf >= 0; buf--) {
-	edit->buffers2[buf] = CMalloc (EDIT_BUF_SIZE);
-	if (filename) {
-	    mc_read (file, (char *) edit->buffers2[buf], EDIT_BUF_SIZE);
+        1) Reorganize the old code so it fills the pointers in 'edit->buffers2'
+	   in reverse order i.e. first read block is stored in the first
+	   element, second in the second and so on.. This is required since
+	   there is no way to find out how much space the translated data will
+	   take.
+
+	2) Reading a block and translate it on. If there is space left in
+	   buffer (i.e. less then EDIT_BUF_SIZE), read another block from
+	   the file, store it the next available buffer and start translating 
+	   it - we fill first the missing data in the previous buffer.
+
+	3) Having the data loaded and translated, rearrange the buffers so
+	   that they look as expected.
+       */
+    for (j = 0; j < block_count; j++) {
+	edit->buffers2[j] = CMalloc (EDIT_BUF_SIZE);
+	if ((j + 1) == block_count)
+	    nread = edit->curs2 & M_EDIT_BUF_SIZE;
+	
+        if (filename) {
+	    if (mc_read (file, (char *) edit->buffers2[j], nread) !=
+			nread) {
+		/* TODO: Add some message here - I dunno how :( */
+		return 1;
+	    }
 	} else {
-	    memcpy (edit->buffers2[buf], text, EDIT_BUF_SIZE);
-	    text += EDIT_BUF_SIZE;
+	    memcpy (edit->buffers2[j], text, nread);
+	    text += nread;
 	}
+	
+	getp = curp = begp = edit->buffers2[j];
+	/* End is pointing to the last byte, NOT the byte afte the last byte.
+	   You must have that in mind when inspecting the code below. */
+	endp = begp + nread - 1;
+
+	/* Handle a case where a '\r' can be found at the last byte in
+	   the previous buffer and the '\n' in the first byte of the
+	   current buffer. */
+	if (crlf_maybe == TRUE) {
+	    *(putp - 1) = '\n';
+	    curp++;
+	    crlf_maybe = FALSE;
+	}
+
+	if (avail == 0) putp = NULL;
+
+	/* Do CR/LF -> LF translation */
+	/* TODO: Move to a separate function ?! */
+	/* TODO: Control this feature through some kind of flag which
+		 detects if the file is with DOS line endings. */
+	for (; curp < endp; curp++) {
+	    if (*curp == '\r' || need_lf == TRUE) {
+	    if (*(curp + 1) == '\n') {
+		if (putp != NULL) {
+		    nbytes = curp - getp;
+		    /* Fill the unused space from the previous block first! */
+		    if (avail > 0) {
+			nbytes = min (nbytes, avail);
+			/* We can use memcpy () here. */
+			memcpy (putp, getp, nbytes);
+			putp += nbytes;
+			avail -= nbytes;
+			{
+			/* Ugly! */
+			size_t nbytes_sav = nbytes;
+			nbytes = (curp - getp) - nbytes;
+			getp += nbytes_sav;
+			}
+			/* The previous block is full - swtich to the current
+			   one. */
+			if (nbytes > 0) {
+			    putp = begp;
+			    nblkout++;
+			}
+		    }
+		    memmove (putp, getp, nbytes);
+		    putp += nbytes;
+		} else {
+		    putp = curp;
+		}
+		/* Are there any other CR/LF pairs following this one ? */
+		/* TODO: This is supposed to speed up things - if you think
+		   that is ugly - remove it! :) */
+		crlf_readahead = curp + 2;
+		while ((crlf_readahead < endp) &&
+			*crlf_readahead == '\r' && *++crlf_readahead == '\n')
+		    crlf_readahead++;
+		    
+		if ((nbytes = ((crlf_readahead - curp) / 2)) > 0) {
+		    /* Handle multiple subsequent CR/LF pairs. */
+		    memset (putp, '\n', nbytes);
+		    putp += nbytes;
+		    getp = curp = crlf_readahead;
+		    if (avail > 0) {
+			if ((avail -= nbytes) == 0) {
+			    putp = begp;
+			    nblkout++;
+			}
+		    }
+		} else {
+		    /* This is the normal case - one CR/LF a line. */
+		    *putp++ = '\n';
+		    getp = curp + 2;
+		    if (avail > 0) {
+			if (--avail == 0) {
+			    putp = begp;
+			    nblkout++;
+			}
+		    }
+		}
+		curp++;
+		need_lf = FALSE;
+	    } else if (*(curp + 1) == '\r') {
+		need_lf = TRUE;
+		curp++;
+	    } else if (need_lf == TRUE) {
+		curp++;
+		need_lf = FALSE;
+	    }
+	    }
+		
+	}
+	
+	if (putp != NULL && getp < endp) {
+	    /* (endp+1) - getp is used here because the loop may end either
+	       with curp == or curp > endp. So a safe bet is just to use endp+1
+	       here - it is always the same. */
+	    /* FIXME: Do we have to care about 'avail' ?!
+	       It seems so :) */
+	    nbytes = (endp + 1) - getp;
+	    memmove (putp, getp, nbytes);
+	    putp += nbytes;
+	}
+
+	avail = (endp + 1) - (putp != NULL ? putp : begp);
+
+	/* Set a mark tha we have to check for a starting LF in the next
+	   buffer. */
+	if (*curp == '\r')
+	    crlf_maybe = TRUE;
+	
+	/* End of CR/LF -> LF translation */
     }
-
+    
+    /* Rearrange the data now */
+    if  (nblkout > 0) {
+	/* TODO: Use a more useful proccessing than that - this is ugly*/
+	if (avail > 0) {
+	    char *first_chunk = CMalloc (nread - avail);
+	    char *tmpbuf = first_chunk;
+	    nblkout++; /* Add one for the partial buffer */
+	    for (j = 0; j < nblkout; j++) {
+		memcpy (tmpbuf, edit->buffers2[j], nread - avail);
+		if ((j + 1) == nblkout) {
+		    tmpbuf = NULL;
+		    memset (edit->buffers2[j], 0x00, EDIT_BUF_SIZE);
+		} else {
+		    memmove (edit->buffers2[j], edit->buffers2[j] + nread - avail,
+			EDIT_BUF_SIZE - nread + avail);
+		    tmpbuf = edit->buffers2[j] + (EDIT_BUF_SIZE - nread + avail);
+		}
+	    }
+	    memcpy (edit->buffers2[nblkout-1] + EDIT_BUF_SIZE-nread + avail,
+		first_chunk, nread - avail);
+	    free (first_chunk);
+	    for (j = 0; j < (nblkout-1) / 2; j++) {
+		tmpbuf = edit->buffers2[j];
+		edit->buffers2[j] = edit->buffers2[(nblkout-1)-j-1];
+		edit->buffers2[(nblkout-1)-j-1] = tmpbuf;
+	    }
+	}
+    }
+		
+    edit->curs2 = edit->last_byte = (nblkout - 1) * EDIT_BUF_SIZE +
+					    (nread - avail);
+    
     edit->curs1 = 0;
     if (file != -1)
 	mc_close (file);
@@ -456,14 +638,15 @@ int edit_open_file (WEdit * edit, const 
 	if (r)
 	    return 1;
 	edit->stat1 = st;
-#ifndef CR_LF_TRANSLATION
+//#ifndef CR_LF_TRANSLATION
+//	edit->last_byte = st.st_size;
+//#else
+///* going to read the file into the buffer later byte by byte */
+//	edit->last_byte = 0;
+//	filename = 0;
+//	text = "";
+//#endif
 	edit->last_byte = st.st_size;
-#else
-/* going to read the file into the buffer later byte by byte */
-	edit->last_byte = 0;
-	filename = 0;
-	text = "";
-#endif
     }
     return init_dynamic_edit_buffers (edit, filename, text);
 }
