sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit fab4b384e7eb22613174f8adee6510ea3b78db6b
parent eac0f658cfce9aa70492127fbc980aca9b0f8f7b
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun,  1 Jun 2014 15:04:32 +0200

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>

Diffstat:
Mfold.c | 2+-
Mhead.c | 12++++++++----
Mnl.c | 2+-
Mtail.c | 15+++++++++------
Mtr.c | 2+-
Muniq.c | 2+-
Mutil/crypt.c | 2+-
Mutil/getlines.c | 10+++++-----
Muudecode.c | 15++++++++-------
9 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/fold.c b/fold.c @@ -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); } diff --git a/head.c b/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); } diff --git a/nl.c b/nl.c @@ -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)) diff --git a/tail.c b/tail.c @@ -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); diff --git a/tr.c b/tr.c @@ -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)) diff --git a/uniq.c b/uniq.c @@ -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); } diff --git a/util/crypt.c b/util/crypt.c @@ -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; diff --git a/util/getlines.c b/util/getlines.c @@ -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); } - diff --git a/uudecode.c b/uudecode.c @@ -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); }