top: tweak 'Inspect' code for isolated edge situations

If the 'Inspect' feature was used to view a file which
contained binary (unprintable) data, and when the last
line for such a file was purely unprintable, then that
line length would be overstated by the terminal width.

This was also the occasion where valgrind might object
over potential reference to some unitialized value(s).
It was a harmless situation and somewhat rare to begin
with. Anyway this commit will eliminate the potential.

Additionally, supporting readfile logic was simplified
and the 'status line' bytes read is now more accurate.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2013-03-22 00:00:00 -05:00 committed by Jaromir Capik
parent 10936365de
commit a6c5e31022

View File

@ -1223,15 +1223,13 @@ static int readfile (FILE *fp, char **baddr, size_t *bsize, size_t *bread) {
*bsize = READMINSZ;
*baddr = alloc_c(READMINSZ);
if (fp) {
while (0 < (num = fread(chunk, 1, sizeof(chunk) -1, fp))) {
if (feof(fp) && chunk[num -1]) chunk[num++] = '\0';
while (0 < (num = fread(chunk, 1, sizeof(chunk), fp))) {
*baddr = alloc_r(*baddr, num + *bsize);
memcpy(*baddr + *bread, chunk, num);
*bread += num;
*bsize += num;
};
// adjust for the null terminator, which was counted above
if (*bread) --(*bread);
*(*baddr + *bread) = '\0';
return ferror(fp);
}
return ENOENT;
@ -2888,6 +2886,7 @@ static inline void insp_make_row (int col, int row) {
} else { mkFND // a big show (he,he)
ofs = 0;
}
if (col + fr >= INSP_RLEN(row)) break;
}
capNO;
putp(Cap_clr_eol);