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

2
fold.c
View File

@ -64,7 +64,7 @@ fold(FILE *fp, long width)
char *buf = NULL;
size_t size = 0;
while(afgets(&buf, &size, fp))
while(agetline(&buf, &size, fp) != -1)
foldline(buf, width);
free(buf);
}

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);
}

2
nl.c
View File

@ -68,7 +68,7 @@ nl(FILE *fp)
long n = 0;
size_t size = 0;
while(afgets(&buf, &size, fp)) {
while(agetline(&buf, &size, fp) != -1) {
if((mode == 'a')
|| (mode == 'p'
&& !regexec(&preg, buf, 0, NULL, 0))

15
tail.c
View File

@ -55,25 +55,28 @@ main(int argc, char *argv[])
static void
dropinit(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))
if(buf[strlen(buf)-1] == '\n')
while(i < n && ((len = agetline(&buf, &size, fp) != -1)))
if(len && buf[len - 1] == '\n')
i++;
free(buf);
concat(fp, str, stdout, "<stdout>");
}
static void
taketail(FILE *fp, const char *str, long n)
{
char **ring;
char **ring = NULL;
long i, j;
size_t *size = NULL;
if(!(ring = calloc(n, sizeof *ring)) || !(size = calloc(n, sizeof *size)))
eprintf("calloc:");
for(i = j = 0; afgets(&ring[i], &size[i], fp); i = j = (i+1)%n)
for(i = j = 0; agetline(&ring[i], &size[i], fp) != -1; i = j = (i + 1) % n)
;
if(ferror(fp))
eprintf("%s: read error:", str);

2
tr.c
View File

@ -160,7 +160,7 @@ main(int argc, char *argv[])
mapfunc = maptoset;
}
while(afgets(&buf, &size, stdin))
while(agetline(&buf, &size, stdin) != -1)
mapfunc(mappings, buf);
free(buf);
if(ferror(stdin))

2
uniq.c
View File

@ -93,7 +93,7 @@ uniq(FILE *fp, const char *str)
char *buf = NULL;
size_t size = 0;
while(afgets(&buf, &size, fp))
while(agetline(&buf, &size, fp) != -1)
uniqline(buf);
}

View File

@ -50,7 +50,7 @@ cryptcheck(char *sumfile, int argc, char *argv[],
else if(!(cfp = fopen(sumfile, "r")))
eprintf("fopen %s:", sumfile);
while(afgets(&line, &bufsiz, cfp)) {
while(agetline(&line, &bufsiz, cfp) != -1) {
if(!(file = strstr(line, " "))) {
formatsucks++;
continue;

View File

@ -10,10 +10,10 @@ void
getlines(FILE *fp, struct linebuf *b)
{
char *line = NULL, **nline;
size_t size = 0;
size_t linelen;
size_t size = 0, linelen;
ssize_t len;
while(afgets(&line, &size, fp)) {
while((len = agetline(&line, &size, fp)) != -1) {
if(++b->nlines > b->capacity) {
b->capacity += 512;
nline = realloc(b->lines, b->capacity * sizeof(*b->lines));
@ -21,10 +21,10 @@ getlines(FILE *fp, struct linebuf *b)
eprintf("realloc:");
b->lines = nline;
}
if(!(b->lines[b->nlines-1] = malloc((linelen = strlen(line)+1))))
linelen = len + 1;
if(!(b->lines[b->nlines-1] = malloc(linelen)))
eprintf("malloc:");
memcpy(b->lines[b->nlines-1], line, linelen);
}
free(line);
}

View File

@ -118,19 +118,20 @@ parseheader(FILE *fp, const char *s, const char *header, mode_t *mode, char **fn
static void
uudecode(FILE *fp, FILE *outfp)
{
char *bufb = NULL, *p, *nl;
size_t n=0;
char *bufb = NULL, *p;
size_t n = 0;
ssize_t len;
int ch, i;
#define DEC(c) (((c) - ' ') & 077) /* single character decode */
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
#define OUT_OF_RANGE(c) eprintf("character %c out of range: [%d-%d]", (c), 1 + ' ', 077 + ' ' + 1)
while (afgets(&bufb,&n,fp)) {
while((len = agetline(&bufb, &n, fp)) != -1) {
p = bufb;
/* trim newlines */
if ((nl = strchr(bufb, '\n')) != NULL)
*nl = '\0';
if(len && bufb[len - 1] != '\n')
bufb[len - 1] = '\0';
else
eprintf("no newline found, aborting\n");
/* check for last line */
@ -170,8 +171,8 @@ uudecode(FILE *fp, FILE *outfp)
eprintf("read error:");
}
/* check for end or fail */
afgets(&bufb, &n, fp);
if (strnlen(bufb, 3) < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
len = agetline(&bufb, &n, fp);
if(len < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
eprintf("invalid uudecode footer \"end\" not found\n");
free(bufb);
}