=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/hexdump/parse.c,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- src/usr.bin/hexdump/parse.c 2004/11/21 19:57:16 1.14 +++ src/usr.bin/hexdump/parse.c 2006/08/23 03:13:09 1.15 @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.c,v 1.14 2004/11/21 19:57:16 otto Exp $ */ +/* $OpenBSD: parse.c,v 1.15 2006/08/23 03:13:09 ray Exp $ */ /* $NetBSD: parse.c,v 1.12 2001/12/07 13:37:39 bjh21 Exp $ */ /* @@ -32,7 +32,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)parse.c 5.6 (Berkeley) 3/9/91";*/ -static char rcsid[] = "$OpenBSD: parse.c,v 1.14 2004/11/21 19:57:16 otto Exp $"; +static char rcsid[] = "$OpenBSD: parse.c,v 1.15 2006/08/23 03:13:09 ray Exp $"; #endif /* not lint */ #include @@ -53,25 +53,31 @@ void addfile(char *name) { - char *p; FILE *fp; - int ch; - char buf[2048 + 1]; + size_t len; + char *buf, *lbuf, *p; if ((fp = fopen(name, "r")) == NULL) err(1, "fopen %s", name); - while (fgets(buf, sizeof(buf), fp)) { - if (!(p = strchr(buf, '\n'))) { - warnx("line too long."); - while ((ch = getchar()) != '\n' && ch != EOF); - continue; + + lbuf = NULL; + while ((buf = fgetln(fp, &len))) { + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + else { + /* EOF without EOL, copy and add the NUL */ + if ((lbuf = malloc(len + 1)) == NULL) + err(1, NULL); + memcpy(lbuf, buf, len); + lbuf[len] = '\0'; + buf = lbuf; } - *p = '\0'; for (p = buf; *p && isspace((unsigned char)*p); ++p); if (!*p || *p == '#') continue; add(p); } + free(lbuf); (void)fclose(fp); }