[hackers] [sbase] Fix tail(1) -n 0 handling and unglobalize some variables || FRIGN

From: <git_AT_suckless.org>
Date: Thu, 21 May 2015 16:43:43 +0200 (CEST)

commit e8a4f378847b5e8717c1a0c54179d91ce4b04265
Author: FRIGN <dev_AT_frign.de>
Date: Wed May 20 17:49:56 2015 +0200

    Fix tail(1) -n 0 handling and unglobalize some variables
    
    Don't terminate too early. If the given file doesn't exist,
    we need the error-message.
    
    Additionally, some variables were globals for no good reason.

diff --git a/tail.c b/tail.c
index a53dcd1..0667877 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -11,12 +11,10 @@
 #include "utf.h"
 #include "util.h"
 
-static int fflag = 0;
-static size_t num = 10;
-static char mode = 'n';
+static char mode = 'n';
 
 static void
-dropinit(FILE *fp, const char *str)
+dropinit(FILE *fp, const char *str, size_t n)
 {
         Rune r;
         char *buf = NULL;
_AT_@ -24,11 +22,11 @@ dropinit(FILE *fp, const char *str)
         ssize_t len;
 
         if (mode == 'n') {
- while (i < num && (len = getline(&buf, &size, fp)) > 0)
+ while (i < n && (len = getline(&buf, &size, fp)) > 0)
                         if (len > 0 && buf[len - 1] == '\n')
                                 i++;
         } else {
- while (i < num && (len = efgetrune(&r, fp, str)))
+ while (i < n && (len = efgetrune(&r, fp, str)))
                         i++;
         }
         free(buf);
_AT_@ -36,23 +34,26 @@ dropinit(FILE *fp, const char *str)
 }
 
 static void
-taketail(FILE *fp, const char *str)
+taketail(FILE *fp, const char *str, size_t n)
 {
         Rune *r = NULL;
         char **ring = NULL;
         size_t i, j, *size = NULL;
 
+ if (!n)
+ return;
+
         if (mode == 'n') {
- ring = ecalloc(num, sizeof *ring);
- size = ecalloc(num, sizeof *size);
+ ring = ecalloc(n, sizeof(*ring));
+ size = ecalloc(n, sizeof(*size));
 
                 for (i = j = 0; getline(&ring[i], &size[i], fp) > 0; )
- i = j = (i + 1) % num;
+ i = j = (i + 1) % n;
         } else {
- r = ecalloc(num, sizeof *r);
+ r = ecalloc(n, sizeof(*r));
 
                 for (i = j = 0; efgetrune(&r[i], fp, str); )
- i = j = (i + 1) % num;
+ i = j = (i + 1) % n;
         }
         if (ferror(fp))
                 eprintf("%s: read error:", str);
_AT_@ -64,7 +65,7 @@ taketail(FILE *fp, const char *str)
                 } else if (r) {
                         efputrune(&r[j], stdout, "<stdout>");
                 }
- } while ((j = (j + 1) % num) != i);
+ } while ((j = (j + 1) % n) != i);
 
         free(ring);
         free(size);
_AT_@ -82,10 +83,10 @@ main(int argc, char *argv[])
 {
         struct stat st1, st2;
         FILE *fp;
- size_t tmpsize;
- int ret = 0, newline = 0, many = 0;
+ size_t tmpsize, n = 10;
+ int fflag = 0, ret = 0, newline = 0, many = 0;
         char *numstr, *tmp;
- void (*tail)(FILE *, const char *) = taketail;
+ void (*tail)(FILE *, const char *, size_t) = taketail;
 
         ARGBEGIN {
         case 'f':
_AT_@ -95,22 +96,19 @@ main(int argc, char *argv[])
         case 'n':
                 mode = ARGC();
                 numstr = EARGF(usage());
- num = MIN(llabs(estrtonum(numstr, LLONG_MIN + 1, MIN(LLONG_MAX, SIZE_MAX))), SIZE_MAX);
+ n = MIN(llabs(estrtonum(numstr, LLONG_MIN + 1, MIN(LLONG_MAX, SIZE_MAX))), SIZE_MAX);
                 if (strchr(numstr, '+'))
                         tail = dropinit;
                 break;
         ARGNUM:
- num = ARGNUMF();
+ n = ARGNUMF();
                 break;
         default:
                 usage();
         } ARGEND;
 
- if (!num)
- return 0;
-
         if (!argc)
- tail(stdin, "<stdin>");
+ tail(stdin, "<stdin>", n);
         else {
                 if ((many = argc > 1) && fflag)
                         usage();
_AT_@ -130,7 +128,7 @@ main(int argc, char *argv[])
                         if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
                                 fflag = 0;
                         newline = 1;
- tail(fp, *argv);
+ tail(fp, *argv, n);
 
                         if (!fflag) {
                                 if (fp != stdin && fshut(fp, *argv))
Received on Thu May 21 2015 - 16:43:43 CEST

This archive was generated by hypermail 2.3.0 : Thu May 21 2015 - 16:48:16 CEST