[hackers] [sbase] use agetline instead of agets || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Sun, 01 Jun 2014 19:03:14 +0200

commit fab4b384e7eb22613174f8adee6510ea3b78db6b
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Jun 1 15:04:32 2014 +0200

    use agetline instead of agets
    
    also use agetline where fgets with a static buffer was used previously.
    
    Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>

diff --git a/fold.c b/fold.c
index 84888e5..c307bb9 100644
--- a/fold.c
+++ b/fold.c
_AT_@ -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
index 93fd818..8313648 100644
--- a/head.c
+++ b/head.c
_AT_@ -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);
_AT_@ -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] == '
')
+ if(buf[len - 1] == '
')
                         i++;
         }
+ free(buf);
         if(ferror(fp))
                 eprintf("%s: read error:", str);
 }
diff --git a/nl.c b/nl.c
index 4e2df70..2d15391 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -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
index c02fc41..59e68ca 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -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] == '
')
+ while(i < n && ((len = agetline(&buf, &size, fp) != -1)))
+ if(len && buf[len - 1] == '
')
                         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
index 527c0e0..388d42f 100644
--- a/tr.c
+++ b/tr.c
_AT_@ -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
index ee76ee0..03fe72d 100644
--- a/uniq.c
+++ b/uniq.c
_AT_@ -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
index 589104b..cd9ff32 100644
--- a/util/crypt.c
+++ b/util/crypt.c
_AT_@ -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
index f1c3453..4d66acf 100644
--- a/util/getlines.c
+++ b/util/getlines.c
_AT_@ -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));
_AT_@ -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
index 1196661..b17a0f1 100644
--- a/uudecode.c
+++ b/uudecode.c
_AT_@ -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, '
')) != NULL)
- *nl = '
Received on Sun Jun 01 2014 - 19:03:14 CEST

This archive was generated by hypermail 2.3.0 : Sun Jun 01 2014 - 19:12:15 CEST