I would like to know if this does more malloc. I worry it is an additional level of malloc per line.
>On Wed, Jan 23 2019 18:01:24 -0500, Ted Unangst wrote: >> Lauri Tirkkonen wrote: >> > > > oh, interesting. that's sloppy. can you please fix that first, >> > > > separately? >> > > >> > > Sure, here it is. >> > >> > Could you please take a look at it? It's been a couple weeks. >> >> yup, sorry, slipped by. committed. > >Thanks. What about the fgetln->getline diff (same as I sent previously >applies cleanly; reattached below)? I know you said you're concerned >about the extra copy, but as I pointed out the getline code path will >only be taken if mmopen() fails, the file is not seekable, or if grep >was compiled -DSMALL. > >diff --git a/usr.bin/grep/file.c b/usr.bin/grep/file.c >index 4b3c689e4ab..87c49dd5cc0 100644 >--- a/usr.bin/grep/file.c >+++ b/usr.bin/grep/file.c >@@ -34,10 +34,8 @@ > #include "grep.h" > > static char fname[PATH_MAX]; >-#ifndef NOZ > static char *lnbuf; >-static size_t lnbuflen; >-#endif >+static size_t lnbufsize; > > #define FILE_STDIO 0 > #define FILE_MMAP 1 >@@ -73,9 +71,9 @@ gzfgetln(gzFile *f, size_t *len) > else > errx(2, "%s: %s", fname, gzerrstr); > } >- if (n >= lnbuflen) { >- lnbuflen *= 2; >- lnbuf = grep_realloc(lnbuf, ++lnbuflen); >+ if (n >= lnbufsize) { >+ lnbufsize *= 2; >+ lnbuf = grep_realloc(lnbuf, ++lnbufsize); > } > if (c == '\n') > break; >@@ -182,7 +180,13 @@ grep_fgetln(file_t *f, size_t *l) > { > switch (f->type) { > case FILE_STDIO: >- return fgetln(f->f, l); >+ if ((*l = getline(&lnbuf, &lnbufsize, f->f)) == -1) { >+ if (ferror(f->f)) >+ err(2, "%s: getline", fname); >+ else >+ return NULL; >+ } >+ return lnbuf; > #ifndef SMALL > case FILE_MMAP: > return mmfgetln(f->mmf, l); >diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c >index 913cc97a0f3..cfac24b12aa 100644 >--- a/usr.bin/grep/grep.c >+++ b/usr.bin/grep/grep.c >@@ -224,15 +224,19 @@ read_patterns(const char *fn) > { > FILE *f; > char *line; >- size_t len; >+ ssize_t len; >+ size_t linesize; > > if ((f = fopen(fn, "r")) == NULL) > err(2, "%s", fn); >- while ((line = fgetln(f, &len)) != NULL) >+ line = NULL; >+ linesize = 0; >+ while ((len = getline(&line, &linesize, f)) != -1) > add_pattern(line, *line == '\n' ? 0 : len); > if (ferror(f)) > err(2, "%s", fn); > fclose(f); >+ free(line); > } > > int >-- >Lauri Tirkkonen | lotheac @ IRCnet > >