diff options
author | Moritz Jodeit <moritz@cvs.openbsd.org> | 2007-05-17 10:55:17 +0000 |
---|---|---|
committer | Moritz Jodeit <moritz@cvs.openbsd.org> | 2007-05-17 10:55:17 +0000 |
commit | 309b513364b7d30f4e31c1541dff2818d7227440 (patch) | |
tree | 7e68570b055aeac8d57915b46cb4a683a60f999e | |
parent | a13c76090ffdc67c35591e08d50e08d814eca74e (diff) |
Fix fgetln(3) handling of lines without trailing newline.
From Tobias Stoeckmann.
ok ray@ millert@
-rw-r--r-- | usr.bin/asa/asa.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/usr.bin/asa/asa.c b/usr.bin/asa/asa.c index 4901251bd34..096aa528479 100644 --- a/usr.bin/asa/asa.c +++ b/usr.bin/asa/asa.c @@ -1,4 +1,4 @@ -/* $OpenBSD: asa.c,v 1.6 2006/03/19 19:06:40 robert Exp $ */ +/* $OpenBSD: asa.c,v 1.7 2007/05/17 10:55:16 moritz Exp $ */ /* $NetBSD: asa.c,v 1.10 1995/04/21 03:01:41 cgd Exp $ */ /* @@ -32,11 +32,12 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: asa.c,v 1.6 2006/03/19 19:06:40 robert Exp $"; +static char rcsid[] = "$OpenBSD: asa.c,v 1.7 2007/05/17 10:55:16 moritz Exp $"; #endif #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <err.h> #include <unistd.h> @@ -79,11 +80,19 @@ main(int argc, char *argv[]) static void asa(FILE *f) { - char *buf; + char *buf, *lbuf = NULL; size_t len; if ((buf = fgetln (f, &len)) != NULL) { - buf[len - 1] = '\0'; + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + else { + if ((lbuf = malloc(len + 1)) == NULL) + err(1, NULL); + memcpy(lbuf, buf, len); + lbuf[len] = '\0'; + buf = lbuf; + } /* special case the first line */ switch (buf[0]) { case '0': @@ -99,7 +108,15 @@ asa(FILE *f) } while ((buf = fgetln(f, &len)) != NULL) { - buf[len - 1] = '\0'; + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + else { + if ((lbuf = malloc(len + 1)) == NULL) + err(1, NULL); + memcpy(lbuf, buf, len); + lbuf[len] = '\0'; + buf = lbuf; + } switch (buf[0]) { default: case ' ': @@ -121,6 +138,7 @@ asa(FILE *f) fputs (&buf[1], stdout); } } + free(lbuf); putchar ('\n'); } |