Modify our 'patch' utility to recognize the

\ No newline at end of file
line that some versions of diff print out if the last line of the two files
are different, and one of the two files does not have a newline character
on that last line.

This change is still somewhat under discussion in -arch and -standards, but I
want to commit it to -current today so I'd have the chance to MFC it to -stable
before the code freeze for 4.6-release (which would be May 1st).

Note: the related change to 'diff' (so it might *generate* that line) is NOT
expected to be included in 4.6-release.  We can debate that change later.

Obtained from:	NetBSD (1.13 of basesrc/usr.bin/patch/pch.c, by kristerw)
MFC after:	4 days
This commit is contained in:
gad 2002-04-26 19:01:19 +00:00
parent 8ad1c2807b
commit ad53a7dcd4

View File

@ -434,6 +434,30 @@ malformed ()
/* 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, -1, SEEK_CUR);
return FALSE;
}
/* True if there is more of the current diff listing to process. */
bool
@ -641,6 +665,15 @@ another_hunk()
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 &&
@ -768,6 +801,14 @@ another_hunk()
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);
@ -865,6 +906,12 @@ another_hunk()
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 = ' ';
@ -900,6 +947,12 @@ another_hunk()
p_Char[filldst] = ch;
p_line[filldst] = s;
p_len[filldst++] = strlen(s);
if (fillsrc > p_ptrn_lines) {
if (remove_special_line()) {
p_len[filldst - 1] -= 1;
s[p_len[filldst - 1]] = 0;
}
}
break;
default:
p_end = filldst;
@ -975,6 +1028,12 @@ another_hunk()
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++;
@ -1007,6 +1066,11 @@ another_hunk()
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? */
if (!pch_swap())