=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/patch/pch.c,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- src/usr.bin/patch/pch.c 2003/04/08 00:18:31 1.13 +++ src/usr.bin/patch/pch.c 2003/07/16 18:09:20 1.14 @@ -1,7 +1,7 @@ -/* $OpenBSD: pch.c,v 1.13 2003/04/08 00:18:31 deraadt Exp $ */ +/* $OpenBSD: pch.c,v 1.14 2003/07/16 18:09:20 otto Exp $ */ #ifndef lint -static char rcsid[] = "$OpenBSD: pch.c,v 1.13 2003/04/08 00:18:31 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: pch.c,v 1.14 2003/07/16 18:09:20 otto Exp $"; #endif /* not lint */ #include "EXTERN.h" @@ -404,6 +404,30 @@ /* about as informative as "Syntax error" in C */ } +/* + * True if the line has been discarded (i.e. it is a line saying + * "\ No newline at end of file".) + */ +static bool +remove_special_line(void) +{ + int c; + + c = fgetc(pfp); + if (c == '\\') { + do { + c = fgetc(pfp); + } while (c != EOF && c != '\n'); + + return TRUE; + } + + if (c != EOF) + fseek(pfp, -1L, SEEK_CUR); + + return FALSE; +} + /* True if there is more of the current diff listing to process. */ bool @@ -612,6 +636,14 @@ p_end--; return FALSE; } + if (p_end == p_ptrn_lines) { + if (remove_special_line()) { + int len; + + len = strlen(p_line[p_end]) - 1; + (p_line[p_end])[len] = 0; + } + } break; case '\t': case '\n': /* assume the 2 spaces got eaten */ if (repl_beginning && repl_could_be_missing && @@ -739,6 +771,12 @@ assert(fillsrc==p_end+1 || fillsrc==repl_beginning); assert(filldst==p_end+1 || filldst==repl_beginning); } + if (p_line[p_end] != NULL) { + if (remove_special_line()) { + p_len[p_end] -= 1; + (p_line[p_end])[p_len[p_end]] = 0; + } + } } else if (diff_type == UNI_DIFF) { long line_beginning = ftell(pfp); @@ -839,6 +877,12 @@ p_char[fillsrc] = ch; p_line[fillsrc] = s; p_len[fillsrc++] = strlen(s); + if (fillsrc > p_ptrn_lines) { + if (remove_special_line()) { + p_len[fillsrc - 1] -= 1; + s[p_len[fillsrc - 1]] = 0; + } + } break; case '=': ch = ' '; @@ -874,6 +918,12 @@ p_char[filldst] = ch; p_line[filldst] = s; p_len[filldst++] = strlen(s); + if (fillsrc > p_ptrn_lines) { + if (remove_special_line()) { + p_len[fillsrc - 1] -= 1; + s[p_len[fillsrc - 1]] = 0; + } + } break; default: p_end = filldst; @@ -950,6 +1000,12 @@ p_len[i] = strlen(p_line[i]); p_char[i] = '-'; } + + if (remove_special_line()) { + p_len[i - 1] -= 1; + (p_line[i - 1])[p_len[i - 1]] = 0; + } + if (hunk_type == 'c') { ret = pgets(buf, sizeof buf, pfp); p_input_line++; @@ -981,6 +1037,11 @@ } p_len[i] = strlen(p_line[i]); p_char[i] = '+'; + } + + if (remove_special_line()) { + p_len[i - 1] -= 1; + (p_line[i - 1])[p_len[i - 1]] = 0; } } if (reverse) /* backwards patch? */