Check getline()-return-values properly

It's not useful when 0 is returned anyway, so be sure that we have a
string with length > 0, this also solves some indexing-gotchas like
"len - 1" and so on.
Also, add checked getline()'s whenever it has been forgotten and
clean up the error-messages.
This commit is contained in:
FRIGN
2015-03-27 14:49:48 +01:00
parent a516338581
commit 9144d51594
15 changed files with 22 additions and 21 deletions

6
tail.c
View File

@@ -24,7 +24,7 @@ dropinit(FILE *fp, const char *str)
ssize_t len;
if (mode == 'n') {
while (i < num && (len = getline(&buf, &size, fp)) >= 0)
while (i < num && (len = getline(&buf, &size, fp)) > 0)
if (len > 0 && buf[len - 1] == '\n')
i++;
} else {
@@ -46,7 +46,7 @@ taketail(FILE *fp, const char *str)
ring = ecalloc(num, sizeof *ring);
size = ecalloc(num, sizeof *size);
for (i = j = 0; getline(&ring[i], &size[i], fp) != -1; )
for (i = j = 0; getline(&ring[i], &size[i], fp) > 0; )
i = j = (i + 1) % num;
} else {
r = ecalloc(num, sizeof *r);
@@ -131,7 +131,7 @@ main(int argc, char *argv[])
continue;
}
for (tmp = NULL, tmpsize = 0;;) {
while (getline(&tmp, &tmpsize, fp) >= 0) {
while (getline(&tmp, &tmpsize, fp) > 0) {
fputs(tmp, stdout);
fflush(stdout);
}