From 90cc206f6d279fa933b3e183eb209af425bcaddc Mon Sep 17 00:00:00 2001 From: Lauri Tirkkonen Date: Tue, 17 Jul 2018 17:46:50 +0300 Subject: [PATCH] join(1): use getline() instead of fgetln() --- bin/join/join.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bin/join/join.c b/bin/join/join.c index 3049a42319..ac62cf83cd 100644 --- a/bin/join/join.c +++ b/bin/join/join.c @@ -298,9 +298,9 @@ void slurpit(INPUT *F) { LINE *lp, *lastlp, tmp; - size_t len; + size_t len, 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 +308,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,8 +345,10 @@ slurpit(INPUT *F) F->pushbool = 0; continue; } - if ((bp = fgetln(F->fp, &len)) == NULL) + if ((len = getline(&line, &linesize, F->fp)) == -1) { + free(line); return; + } /* * we depend on knowing on what field we are, one safe way is * the file position. @@ -360,17 +364,15 @@ slurpit(INPUT *F) lp->linealloc = newsize; } F->setusedc++; - memmove(lp->line, bp, len); + memmove(lp->line, line, len); lp->fpos = fpos; /* Replace trailing newline, if it exists. */ - if (bp[len - 1] == '\n') + if (lp->line[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 +395,7 @@ slurpit(INPUT *F) break; } } + free(line); } int -- 2.11.4.GIT