summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-06 08:07:49 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-06 08:07:49 -0400
commit8324cbe227d932ca76f878bbd943d7b552571b31 (patch)
treecfc84df6059fbaf1d53dd0702a2c4740354ebb8c
parentb678a1801836b49f61f4993c18fc6bc4861b2e08 (diff)
simplify inner loop
-rw-r--r--head.c15
1 files 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 <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) {