sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 9144d51594d8b35f01da352bed0282afbbc39a24
parent a516338581fca00d06c0d2669d72519b8b808017
Author: FRIGN <dev@frign.de>
Date:   Fri, 27 Mar 2015 14:49:48 +0100

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.

Diffstat:
Mcomm.c | 2+-
Mcron.c | 2+-
Mcut.c | 2+-
Mfold.c | 2+-
Mgrep.c | 4++--
Mhead.c | 2+-
Mlibutil/crypt.c | 2+-
Mlibutil/getlines.c | 2+-
Mlogger.c | 2+-
Mnl.c | 2+-
Msed.c | 2+-
Msort.c | 6+++---
Mtail.c | 6+++---
Muniq.c | 2+-
Muudecode.c | 5+++--
15 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/comm.c b/comm.c @@ -62,7 +62,7 @@ main(int argc, char *argv[]) for (i = 0; i < 2; i++) { if (diff && i == (diff < 0)) continue; - if (getline(&line[i], &linelen[i], fp[i]) >= 0) + if (getline(&line[i], &linelen[i], fp[i]) > 0) continue; if (ferror(fp[i])) eprintf("getline %s:", argv[i]); diff --git a/cron.c b/cron.c @@ -304,7 +304,7 @@ loadentries(void) return -1; } - for (y = 0; (len = getline(&line, &size, fp)) != -1; y++) { + for (y = 0; (len = getline(&line, &size, fp)) > 0; y++) { p = line; if (line[0] == '#' || line[0] == '\n' || line[0] == '\0') continue; diff --git a/cut.c b/cut.c @@ -114,7 +114,7 @@ cut(FILE *fp, char *fname) ssize_t len; Range *r; - while ((len = getline(&buf, &size, fp)) >= 0) { + while ((len = getline(&buf, &size, fp)) > 0) { if (len && buf[len - 1] == '\n') buf[len - 1] = '\0'; if (mode == 'f' && !utfutf(buf, delim)) { diff --git a/fold.c b/fold.c @@ -55,7 +55,7 @@ fold(FILE *fp, const char *fname) char *buf = NULL; size_t size = 0; - while (getline(&buf, &size, fp) >= 0) + while (getline(&buf, &size, fp) > 0) foldline(buf); if (ferror(fp)) eprintf("getline %s:", fname); diff --git a/grep.c b/grep.c @@ -87,7 +87,7 @@ addpatternfile(FILE *fp) static size_t size = 0; ssize_t len = 0; - while ((len = getline(&buf, &size, fp)) != -1) { + while ((len = getline(&buf, &size, fp)) > 0) { if (len > 0 && buf[len - 1] == '\n') buf[len - 1] = '\0'; addpattern(buf); @@ -106,7 +106,7 @@ grep(FILE *fp, const char *str) struct pattern *pnode; int match = NoMatch; - for (n = 1; (len = getline(&buf, &size, fp)) != -1; n++) { + for (n = 1; (len = getline(&buf, &size, fp)) > 0; n++) { /* Remove the trailing newline if one is present. */ if (len && buf[len - 1] == '\n') buf[len - 1] = '\0'; diff --git a/head.c b/head.c @@ -12,7 +12,7 @@ head(FILE *fp, const char *fname, size_t n) size_t i = 0, size = 0; ssize_t len; - while (i < n && (len = getline(&buf, &size, fp)) >= 0) { + while (i < n && (len = getline(&buf, &size, fp)) > 0) { fputs(buf, stdout); i += (len && (buf[len - 1] == '\n')); } diff --git a/libutil/crypt.c b/libutil/crypt.c @@ -45,7 +45,7 @@ mdchecklist(FILE *listfp, struct crypt_ops *ops, uint8_t *md, size_t sz, int r; char *line = NULL, *file, *p; - while (getline(&line, &bufsiz, listfp) != -1) { + while (getline(&line, &bufsiz, listfp) > 0) { if (!(file = strstr(line, " "))) { (*formatsucks)++; continue; diff --git a/libutil/getlines.c b/libutil/getlines.c @@ -13,7 +13,7 @@ getlines(FILE *fp, struct linebuf *b) size_t size = 0, linelen; ssize_t len; - while ((len = getline(&line, &size, fp)) != -1) { + while ((len = getline(&line, &size, fp)) > 0) { if (++b->nlines > b->capacity) { b->capacity += 512; nline = erealloc(b->lines, b->capacity * sizeof(*b->lines)); diff --git a/logger.c b/logger.c @@ -70,7 +70,7 @@ main(int argc, char *argv[]) openlog(tag ? tag : getlogin(), logflags, 0); if (!argc) { - while (getline(&buf, &sz, stdin) >= 0) + while (getline(&buf, &sz, stdin) > 0) syslog(priority, "%s", buf); if (ferror(stdin)) eprintf("getline %s:", "<stdin>"); diff --git a/nl.c b/nl.c @@ -50,7 +50,7 @@ nl(const char *fname, FILE *fp) int donumber, oldsection, section = 1, bl = 1; char *buf = NULL; - while (getline(&buf, &size, fp) >= 0) { + while (getline(&buf, &size, fp) > 0) { donumber = 0; oldsection = section; diff --git a/sed.c b/sed.c @@ -451,7 +451,7 @@ read_line(FILE *f, String *s) if ((len = getline(&s->str, &s->cap, f)) < 0) { if (ferror(f)) - eprintf("getline failed\n"); + eprintf("getline:"); return EOF; } if (s->str[--len] == '\n') diff --git a/sort.c b/sort.c @@ -63,9 +63,9 @@ check(FILE *fp) { static struct { char *buf; size_t size; } prev, cur, tmp; - if (!prev.buf) - getline(&prev.buf, &prev.size, fp); - while (getline(&cur.buf, &cur.size, fp) != -1) { + if (!prev.buf && getline(&prev.buf, &prev.size, fp) < 0) + eprintf("getline:"); + while (getline(&cur.buf, &cur.size, fp) > 0) { if (uflag > linecmp((const char **) &cur.buf, (const char **) &prev.buf)) { if (!Cflag) weprintf("disorder: %s", cur.buf); diff --git a/tail.c b/tail.c @@ -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); } diff --git a/uniq.c b/uniq.c @@ -76,7 +76,7 @@ uniq(FILE *fp, FILE *ofp) size_t size = 0; ssize_t len; - while ((len = getline(&buf, &size, fp)) >= 0) + while ((len = getline(&buf, &size, fp)) > 0) uniqline(ofp, buf, (size_t)len); } diff --git a/uudecode.c b/uudecode.c @@ -166,7 +166,7 @@ uudecode(FILE *fp, FILE *outfp) #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) #define OUT_OF_RANGE(c) eprintf("character %c out of range: [%d-%d]\n", (c), 1 + ' ', 077 + ' ' + 1) - while ((len = getline(&bufb, &n, fp)) != -1) { + while ((len = getline(&bufb, &n, fp)) > 0) { p = bufb; /* trim newlines */ if (!len || bufb[len - 1] != '\n') @@ -210,7 +210,8 @@ uudecode(FILE *fp, FILE *outfp) eprintf("read error:"); } /* check for end or fail */ - len = getline(&bufb, &n, fp); + if ((len = getline(&bufb, &n, fp)) < 0) + eprintf("getline:"); if (len < 3 || strncmp(bufb, "end", 3) || bufb[3] != '\n') eprintf("invalid uudecode footer \"end\" not found\n"); free(bufb);