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; char *buf = NULL;
size_t size = 0; size_t size = 0;
while(afgets(&buf, &size, fp)) while(agetline(&buf, &size, fp) != -1)
foldline(buf, width); foldline(buf, width);
free(buf); free(buf);
} }

12
head.c
View File

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

2
nl.c
View File

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

15
tail.c
View File

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

2
tr.c
View File

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

2
uniq.c
View File

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

View File

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

View File

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

View File

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