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.
This commit is contained in:
FRIGN 2015-05-20 17:49:56 +02:00 committed by sin
parent fbd128b564
commit e8a4f37884

42
tail.c
View File

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