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:
12
head.c
12
head.c
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user