changeset 412:2b521c791e4e

Patch shouldn't be bothered by DOS newlines.
author Rob Landley <rob@landley.net>
date Sun, 22 Jan 2012 22:28:04 -0600
parents 7da386057101
children 12add511705e
files toys/patch.c
diffstat 1 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/toys/patch.c	Sun Jan 22 20:33:15 2012 -0600
+++ b/toys/patch.c	Sun Jan 22 22:28:04 2012 -0600
@@ -238,14 +238,15 @@
 
 void patch_main(void)
 {
-	int reverse = toys.optflags & FLAG_REVERSE, state = 0;
+	int reverse = toys.optflags&FLAG_REVERSE, state = 0, patchlinenum = 0,
+		strip = 0;
 	char *oldname = NULL, *newname = NULL;
 
 	if (TT.infile) TT.filepatch = xopen(TT.infile, O_RDONLY);
 	TT.filein = TT.fileout = -1;
 
 	// Loop through the lines in the patch
-	for(;;) {
+	for (;;) {
 		char *patchline;
 
 		patchline = get_line(TT.filepatch);
@@ -253,6 +254,14 @@
 
 		// Other versions of patch accept damaged patches,
 		// so we need to also.
+		if (strip || !patchlinenum++) {
+			int len = strlen(patchline);
+			if (patchline[len-1] == '\r') {
+				if (!strip) fdprintf(2, "Removing DOS newlines\n");
+				strip = 1;
+				patchline[len-1]=0;
+			}
+		}
 		if (!*patchline) {
 			free(patchline);
 			patchline = xstrdup(" ");