-/* $OpenBSD: join.c,v 1.27 2015/10/09 01:37:07 deraadt Exp $ */
+/* $OpenBSD: join.c,v 1.28 2018/07/18 17:20:54 millert Exp $ */
/*-
* Copyright (c) 1991, 1993, 1994
slurpit(INPUT *F)
{
LINE *lp, *lastlp, tmp;
- size_t len;
+ ssize_t len;
+ size_t linesize;
u_long cnt;
- char *bp, *fieldp;
+ char *bp, *fieldp, *line;
long fpos;
/*
* Read all of the lines from an input file that have the same
*/
F->setcnt = 0;
+ line = NULL;
+ linesize = 0;
for (lastlp = NULL; ; ++F->setcnt, lastlp = lp) {
/*
* If we're out of space to hold line structures, allocate
F->pushbool = 0;
continue;
}
- if ((bp = fgetln(F->fp, &len)) == NULL)
- return;
+ if ((len = getline(&line, &linesize, F->fp)) == -1)
+ break;
/*
* we depend on knowing on what field we are, one safe way is
* the file position.
*/
fpos = ftell(F->fp) - len;
+
+ /* Remove trailing newline, if it exists, and copy line. */
+ if (line[len - 1] == '\n')
+ len--;
if (lp->linealloc <= len + 1) {
char *p;
u_long newsize = lp->linealloc +
lp->linealloc = newsize;
}
F->setusedc++;
- memmove(lp->line, bp, len);
+ memcpy(lp->line, line, len);
+ lp->line[len] = '\0';
lp->fpos = fpos;
- /* Replace trailing newline, if it exists. */
- if (bp[len - 1] == '\n')
- lp->line[len - 1] = '\0';
- else
- lp->line[len] = '\0';
- bp = lp->line;
/* Split the line into fields, allocate space as necessary. */
lp->fieldcnt = 0;
+ bp = lp->line;
while ((fieldp = strsep(&bp, tabchar)) != NULL) {
if (spans && *fieldp == '\0')
continue;
break;
}
}
+ free(line);
}
int