use agetline instead of agets

also use agetline where fgets with a static buffer was used previously.

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
This commit is contained in:
Hiltjo Posthuma
2014-06-01 15:04:32 +02:00
committed by sin
parent eac0f658cf
commit fab4b384e7
9 changed files with 35 additions and 27 deletions

12
head.c
View File

@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "text.h"
#include "util.h"
static void head(FILE *, const char *, long);
@@ -49,14 +50,17 @@ main(int argc, char *argv[])
static void
head(FILE *fp, const char *str, long n)
{
char buf[BUFSIZ];
long i = 0;
char *buf = NULL;
size_t size = 0;
ssize_t len;
unsigned long i = 0;
while(i < n && fgets(buf, sizeof buf, fp)) {
while(i < n && ((len = agetline(&buf, &size, fp)) != -1)) {
fputs(buf, stdout);
if(buf[strlen(buf)-1] == '\n')
if(buf[len - 1] == '\n')
i++;
}
free(buf);
if(ferror(fp))
eprintf("%s: read error:", str);
}