From 7f2185205dccb18444917d57641d6321b9f46576 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 27 Jan 2003 13:49:10 +0000 Subject: [PATCH] Added another version of bgets() which doesn't die on too long lines and reports an error instead. I wrote it originally for new http.c, but the required http.c changes were going to be too extensive, so I postponed the changes and this function is currently unused, but probably worth saving for the future. Also optimized the existing bgets functions a bit. --- lib/fastbuf.c | 31 +++++++++++++++++++++++++++---- lib/fastbuf.h | 1 + 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/fastbuf.c b/lib/fastbuf.c index b7be624b..50b869a8 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -244,11 +244,11 @@ bgets(struct fastbuf *f, byte *b, uns l) int k; k = bgetc(f); - if (k == EOF) + if (k < 0) return NULL; while (b < e) { - if (k == '\n' || k == EOF) + if (k == '\n' || k < 0) { *b = 0; return b; @@ -259,6 +259,29 @@ bgets(struct fastbuf *f, byte *b, uns l) die("%s: Line too long", f->name); } +int +bgets_nodie(struct fastbuf *f, byte *b, uns l) +{ + byte *start = b; + byte *e = b + l - 1; + int k; + + k = bgetc(f); + if (k < 0) + return 0; + while (b < e) + { + if (k == '\n' || k < 0) + { + *b++ = 0; + return b - start; + } + *b++ = k; + k = bgetc(f); + } + return -1; +} + byte * bgets0(struct fastbuf *f, byte *b, uns l) { @@ -266,11 +289,11 @@ bgets0(struct fastbuf *f, byte *b, uns l) int k; k = bgetc(f); - if (k == EOF) + if (k < 0) return NULL; while (b < e) { - if (!k || k == EOF) + if (k <= 0) { *b = 0; return b; diff --git a/lib/fastbuf.h b/lib/fastbuf.h index a370c45b..e08e16a1 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -268,6 +268,7 @@ static inline void bwrite(struct fastbuf *f, void *b, uns l) } byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */ +int bgets_nodie(struct fastbuf *f, byte *b, uns l); byte *bgets0(struct fastbuf *f, byte *b, uns l); static inline void -- 2.39.2