diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2018-07-18 17:20:55 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2018-07-18 17:20:55 +0000 |
commit | 84945bc33662625970ce17b9021d44ad269ce6b6 (patch) | |
tree | 8535dc60fa059704cf2a2c5e9dfc95b05095da90 /usr.bin/join | |
parent | 2969ebebbf18e998a546b4d29953f92e877010f4 (diff) |
Convert from fgetln(3) to getline(3). Based on a diff from Lauri Tirkkonen.
With a tweak and OK from schwarze@
Diffstat (limited to 'usr.bin/join')
-rw-r--r-- | usr.bin/join/join.c | 28 |
1 files 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 |