[hackers] [sbase] Small code cleanup in tail(1) || FRIGN

From: <git_AT_suckless.org>
Date: Mon, 9 Feb 2015 19:10:27 +0100 (CET)

commit 4dbe9e46a7ed1e624f3b80596bfb9a7c26d1b0a5
Author: FRIGN <dev_AT_frign.de>
Date: Mon Feb 9 18:46:47 2015 +0100

    Small code cleanup in tail(1)

diff --git a/tail.c b/tail.c
index 9893aad..9063656 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -12,10 +12,12 @@
 #include "utf.h"
 #include "util.h"
 
-static int fflag = 0;
+static int fflag = 0;
+static size_t num = 10;
+static char mode = 'n';
 
 static void
-dropinit(FILE *fp, const char *str, size_t n, char mode)
+dropinit(FILE *fp, const char *str)
 {
         Rune r;
         char *buf = NULL;
_AT_@ -23,11 +25,11 @@ dropinit(FILE *fp, const char *str, size_t n, char mode)
         ssize_t len;
 
         if (mode == 'n') {
- while (i < n && (len = getline(&buf, &size, fp)) != -1)
+ while (i < num && (len = getline(&buf, &size, fp)) != -1)
                         if (len > 0 && buf[len - 1] == '\n')
                                 i++;
         } else {
- while (i < n && (len = readrune(str, fp, &r)))
+ while (i < num && (len = readrune(str, fp, &r)))
                         i++;
         }
         free(buf);
_AT_@ -35,23 +37,23 @@ dropinit(FILE *fp, const char *str, size_t n, char mode)
 }
 
 static void
-taketail(FILE *fp, const char *str, size_t n, char mode)
+taketail(FILE *fp, const char *str)
 {
         Rune *r = NULL;
         char **ring = NULL;
         size_t i, j, *size = NULL;
 
         if (mode == 'n') {
- ring = ecalloc(n, sizeof *ring);
- size = ecalloc(n, sizeof *size);
+ ring = ecalloc(num, sizeof *ring);
+ size = ecalloc(num, sizeof *size);
 
                 for (i = j = 0; getline(&ring[i], &size[i], fp) != -1; )
- i = j = (i + 1) % n;
+ i = j = (i + 1) % num;
         } else {
- r = ecalloc(n, sizeof *r);
+ r = ecalloc(num, sizeof *r);
 
                 for (i = j = 0; readrune(str, fp, &r[i]); )
- i = j = (i + 1) % n;
+ i = j = (i + 1) % num;
         }
         if (ferror(fp))
                 eprintf("%s: read error:", str);
_AT_@ -60,20 +62,20 @@ taketail(FILE *fp, const char *str, size_t n, char mode)
                 if (ring && ring[j]) {
                         fputs(ring[j], stdout);
                         free(ring[j]);
- }
- if (r) {
+ } else if (r) {
                         writerune("<stdout>", stdout, &r[j]);
                 }
- } while ((j = (j + 1) % n) != i);
+ } while ((j = (j + 1) % num) != i);
 
         free(ring);
         free(size);
+ free(r);
 }
 
 static void
 usage(void)
 {
- eprintf("usage: %s [-f] [-n lines] [file ...]\n", argv0);
+ eprintf("usage: %s [-f] [-c num | -n num] [file ...]\n", argv0);
 }
 
 int
_AT_@ -81,10 +83,10 @@ main(int argc, char *argv[])
 {
         struct stat st1, st2;
         FILE *fp;
- size_t num = 10, tmpsize;
+ size_t tmpsize;
         int ret = 0, newline, many;
- char mode = 'n', *numstr, *tmp;
- void (*tail)(FILE *, const char *, size_t, char) = taketail;
+ char *numstr, *tmp;
+ void (*tail)(FILE *, const char *) = taketail;
 
         ARGBEGIN {
         case 'f':
_AT_@ -106,7 +108,7 @@ main(int argc, char *argv[])
         } ARGEND;
 
         if (argc == 0)
- tail(stdin, "<stdin>", num, mode);
+ tail(stdin, "<stdin>");
         else {
                 if ((many = argc > 1) && fflag)
                         usage();
_AT_@ -124,7 +126,7 @@ main(int argc, char *argv[])
                         if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
                                 fflag = 0;
                         newline = 1;
- tail(fp, argv[0], num, mode);
+ tail(fp, argv[0]);
 
                         if (fflag && argc == 1) {
                                 tmp = NULL;
Received on Mon Feb 09 2015 - 19:10:27 CET

This archive was generated by hypermail 2.3.0 : Mon Feb 09 2015 - 19:12:09 CET