commit 0892f7dd27e583e08a2105bcd739cfff34524d78
parent 0d57993ee0e988c75bcefe8c8f3630932c12b366
Author: phoebos <ben@bvnf.space>
Date: Mon, 4 Oct 2021 19:59:36 +0100
head: write the correct bytes from buf
Diffstat:
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/head.c b/head.c
@@ -9,13 +9,15 @@ int
head(FILE *f, int n) {
int i;
size_t len = LINE_MAX;
+ ssize_t bytes;
char *buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "head: %s\n", strerror(errno));
return 1;
}
for (i = 0; i < n; i++) {
- if (getline(&buf, &len, f) == -1) {
+ bytes = getline(&buf, &len, f);
+ if (bytes == -1) {
if (ferror(f)) {
fprintf(stderr, "head: %s\n", strerror(errno));
return 1;
@@ -23,7 +25,10 @@ head(FILE *f, int n) {
if (feof(f))
break;
}
- printf(buf);
+ if (write(1, buf, bytes) == -1) {
+ fprintf(stderr, "head: %s\n", strerror(errno));
+ return 1;
+ }
}
return 0;