hexdump: rewrite addfile() to use getline()

Signed-off-by: Ondrej Oprala <ooprala@redhat.com>
This commit is contained in:
Ondrej Oprala 2013-09-23 15:39:19 +02:00 committed by Karel Zak
parent d6e5614e5d
commit 96ea3d3200
1 changed files with 8 additions and 9 deletions

View File

@ -57,25 +57,24 @@ void addfile(char *name)
{
char *p;
FILE *fp;
int ch;
char buf[2048 + 1];
size_t n;
char *buf = NULL;
if ((fp = fopen(name, "r")) == NULL)
err(EXIT_FAILURE, _("can't read %s"), name);
while (fgets(buf, sizeof(buf), fp)) {
if ((p = strchr(buf, '\n')) == NULL) {
warnx(_("line too long"));
while ((ch = getchar()) != '\n' && ch != EOF)
;
continue;
}
while (getline(&buf, &n, fp) != -1) {
p = buf;
while (*p && isspace(*p))
++p;
if (!*p || *p == '#')
continue;
add(p);
}
free(buf);
fclose(fp);
}