commit f57338c9b4040d9105f3e3619141c09871e1279a
parent f60617e5c329abe4f55984c7de1ded620d746b99
Author: phoebos <ben@bvnf.space>
Date: Sat, 26 Mar 2022 19:46:55 +0000
head: free buf
Diffstat:
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/head.c b/head.c
@@ -9,13 +9,9 @@
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;
- }
+ char *buf = NULL;
+ size_t len = 0;
for (i = 0; i < n; i++) {
bytes = getline(&buf, &len, f);
if (bytes == -1) {
@@ -31,6 +27,7 @@ head(FILE *f, int n) {
return 1;
}
}
+ free(buf);
return 0;
}