tail: Don't print garbage when input contains no newlines.
getline(3) expects newline-terminated input. While glibc's implementation seems to catch unterminated input and zero the buffer, other versions (notably musl's) do not. This is a workaround. Garbage will still be read, but not printed.
This commit is contained in:
parent
1fa942a0ee
commit
438d2542e4
5
tail.c
5
tail.c
@ -39,6 +39,7 @@ taketail(FILE *fp, const char *str, size_t n)
|
|||||||
Rune *r = NULL;
|
Rune *r = NULL;
|
||||||
char **ring = NULL;
|
char **ring = NULL;
|
||||||
size_t i, j, *size = NULL;
|
size_t i, j, *size = NULL;
|
||||||
|
int seenln = 0;
|
||||||
|
|
||||||
if (!n)
|
if (!n)
|
||||||
return;
|
return;
|
||||||
@ -47,7 +48,7 @@ taketail(FILE *fp, const char *str, size_t n)
|
|||||||
ring = ecalloc(n, sizeof(*ring));
|
ring = ecalloc(n, sizeof(*ring));
|
||||||
size = ecalloc(n, sizeof(*size));
|
size = ecalloc(n, sizeof(*size));
|
||||||
|
|
||||||
for (i = j = 0; getline(ring + i, size + i, fp) > 0; )
|
for (i = j = 0; getline(ring + i, size + i, fp) > 0; seenln = 1)
|
||||||
i = j = (i + 1) % n;
|
i = j = (i + 1) % n;
|
||||||
} else {
|
} else {
|
||||||
r = ecalloc(n, sizeof(*r));
|
r = ecalloc(n, sizeof(*r));
|
||||||
@ -59,7 +60,7 @@ taketail(FILE *fp, const char *str, size_t n)
|
|||||||
eprintf("%s: read error:", str);
|
eprintf("%s: read error:", str);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (ring && ring[j]) {
|
if (seenln && ring && ring[j]) {
|
||||||
fputs(ring[j], stdout);
|
fputs(ring[j], stdout);
|
||||||
free(ring[j]);
|
free(ring[j]);
|
||||||
} else if (r) {
|
} else if (r) {
|
||||||
|
Loading…
Reference in New Issue
Block a user