summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep/grep.c
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2019-01-31 01:30:46 +0000
committertedu <tedu@openbsd.org>2019-01-31 01:30:46 +0000
commit6ec3986fe53b63fb788c4a7e61e2732b64782cbc (patch)
tree0900f5c6e63151168e630dbca815a2dbe9d821a7 /usr.bin/grep/grep.c
parentmissed in previous revert, function not called anymore (diff)
downloadwireguard-openbsd-6ec3986fe53b63fb788c4a7e61e2732b64782cbc.tar.xz
wireguard-openbsd-6ec3986fe53b63fb788c4a7e61e2732b64782cbc.zip
convert fgetln to getline. this improves portability and sets a good
better example for other code to follow. in the common case, grep uses mmap anyway (so no functional change). despite fgetln doing sneaky things with stdio internals, preliminary analysis by lauri suggests this may actually reduce the number of allocations. from Lauri Tirkkonen.
Diffstat (limited to 'usr.bin/grep/grep.c')
-rw-r--r--usr.bin/grep/grep.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c
index fc0b7dc01c4..8b7d2b932b7 100644
--- a/usr.bin/grep/grep.c
+++ b/usr.bin/grep/grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.58 2019/01/23 23:00:54 tedu Exp $ */
+/* $OpenBSD: grep.c,v 1.59 2019/01/31 01:30:46 tedu Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -222,15 +222,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