From 5a81dcea78a779824179c7e626261a4903241139 Mon Sep 17 00:00:00 2001 From: millert Date: Wed, 18 Jul 2018 17:20:54 +0000 Subject: [PATCH] Convert from fgetln(3) to getline(3). Based on a diff from Lauri Tirkkonen. With a tweak and OK from schwarze@ --- usr.bin/join/join.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/usr.bin/join/join.c b/usr.bin/join/join.c index 3049a423196..d3745f167a0 100644 --- a/usr.bin/join/join.c +++ b/usr.bin/join/join.c @@ -1,4 +1,4 @@ -/* $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 @@ -298,9 +298,10 @@ void 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 @@ -308,6 +309,8 @@ slurpit(INPUT *F) */ 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 @@ -343,13 +346,17 @@ slurpit(INPUT *F) 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 + @@ -360,17 +367,13 @@ slurpit(INPUT *F) 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; @@ -393,6 +396,7 @@ slurpit(INPUT *F) break; } } + free(line); } int -- 2.20.1