From 8324cbe227d932ca76f878bbd943d7b552571b31 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 6 Aug 2019 08:07:49 -0400 Subject: simplify inner loop --- head.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/head.c b/head.c index 1642186..58ee02a 100644 --- a/head.c +++ b/head.c @@ -22,7 +22,7 @@ * SOFTWARE. */ -#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 2 #include #include #include @@ -41,13 +41,12 @@ static int head(const char *path, int nlines) return 1; } - while (!feof(f) && nlines > 0) { - char *line = NULL; - size_t n = 0; - - getline(&line, &n, f); - fputs(line, stdout); - nlines--; + int c; + while ((c = fgetc(f)) != EOF && nlines > 0) { + putchar(c); + if (c == '\n') { + nlines--; + } } if (f != stdin) { -- cgit v1.2.1