diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-08-06 08:07:49 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-08-06 08:07:49 -0400 |
commit | 8324cbe227d932ca76f878bbd943d7b552571b31 (patch) | |
tree | cfc84df6059fbaf1d53dd0702a2c4740354ebb8c /head.c | |
parent | b678a1801836b49f61f4993c18fc6bc4861b2e08 (diff) |
simplify inner loop
Diffstat (limited to 'head.c')
-rw-r--r-- | head.c | 15 |
1 files changed, 7 insertions, 8 deletions
@@ -22,7 +22,7 @@ * SOFTWARE. */ -#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 2 #include <errno.h> #include <stdio.h> #include <string.h> @@ -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) { |