Small code cleanup in tail(1)
This commit is contained in:
parent
006c9739b3
commit
9292ae2e16
38
tail.c
38
tail.c
|
@ -13,9 +13,11 @@
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static int fflag = 0;
|
static int fflag = 0;
|
||||||
|
static size_t num = 10;
|
||||||
|
static char mode = 'n';
|
||||||
|
|
||||||
static void
|
static void
|
||||||
dropinit(FILE *fp, const char *str, size_t n, char mode)
|
dropinit(FILE *fp, const char *str)
|
||||||
{
|
{
|
||||||
Rune r;
|
Rune r;
|
||||||
char *buf = NULL;
|
char *buf = NULL;
|
||||||
|
@ -23,11 +25,11 @@ dropinit(FILE *fp, const char *str, size_t n, char mode)
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
|
|
||||||
if (mode == 'n') {
|
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')
|
if (len > 0 && buf[len - 1] == '\n')
|
||||||
i++;
|
i++;
|
||||||
} else {
|
} else {
|
||||||
while (i < n && (len = readrune(str, fp, &r)))
|
while (i < num && (len = readrune(str, fp, &r)))
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
|
@ -35,23 +37,23 @@ dropinit(FILE *fp, const char *str, size_t n, char mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
taketail(FILE *fp, const char *str, size_t n, char mode)
|
taketail(FILE *fp, const char *str)
|
||||||
{
|
{
|
||||||
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 (mode == 'n') {
|
if (mode == 'n') {
|
||||||
ring = ecalloc(n, sizeof *ring);
|
ring = ecalloc(num, sizeof *ring);
|
||||||
size = ecalloc(n, sizeof *size);
|
size = ecalloc(num, sizeof *size);
|
||||||
|
|
||||||
for (i = j = 0; getline(&ring[i], &size[i], fp) != -1; )
|
for (i = j = 0; getline(&ring[i], &size[i], fp) != -1; )
|
||||||
i = j = (i + 1) % n;
|
i = j = (i + 1) % num;
|
||||||
} else {
|
} else {
|
||||||
r = ecalloc(n, sizeof *r);
|
r = ecalloc(num, sizeof *r);
|
||||||
|
|
||||||
for (i = j = 0; readrune(str, fp, &r[i]); )
|
for (i = j = 0; readrune(str, fp, &r[i]); )
|
||||||
i = j = (i + 1) % n;
|
i = j = (i + 1) % num;
|
||||||
}
|
}
|
||||||
if (ferror(fp))
|
if (ferror(fp))
|
||||||
eprintf("%s: read error:", str);
|
eprintf("%s: read error:", str);
|
||||||
|
@ -60,20 +62,20 @@ taketail(FILE *fp, const char *str, size_t n, char mode)
|
||||||
if (ring && ring[j]) {
|
if (ring && ring[j]) {
|
||||||
fputs(ring[j], stdout);
|
fputs(ring[j], stdout);
|
||||||
free(ring[j]);
|
free(ring[j]);
|
||||||
}
|
} else if (r) {
|
||||||
if (r) {
|
|
||||||
writerune("<stdout>", stdout, &r[j]);
|
writerune("<stdout>", stdout, &r[j]);
|
||||||
}
|
}
|
||||||
} while ((j = (j + 1) % n) != i);
|
} while ((j = (j + 1) % num) != i);
|
||||||
|
|
||||||
free(ring);
|
free(ring);
|
||||||
free(size);
|
free(size);
|
||||||
|
free(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-f] [-n lines] [file ...]\n", argv0);
|
eprintf("usage: %s [-f] [-c num | -n num] [file ...]\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -81,10 +83,10 @@ main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct stat st1, st2;
|
struct stat st1, st2;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
size_t num = 10, tmpsize;
|
size_t tmpsize;
|
||||||
int ret = 0, newline, many;
|
int ret = 0, newline, many;
|
||||||
char mode = 'n', *numstr, *tmp;
|
char *numstr, *tmp;
|
||||||
void (*tail)(FILE *, const char *, size_t, char) = taketail;
|
void (*tail)(FILE *, const char *) = taketail;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'f':
|
case 'f':
|
||||||
|
@ -106,7 +108,7 @@ main(int argc, char *argv[])
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
if (argc == 0)
|
if (argc == 0)
|
||||||
tail(stdin, "<stdin>", num, mode);
|
tail(stdin, "<stdin>");
|
||||||
else {
|
else {
|
||||||
if ((many = argc > 1) && fflag)
|
if ((many = argc > 1) && fflag)
|
||||||
usage();
|
usage();
|
||||||
|
@ -124,7 +126,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[0], num, mode);
|
tail(fp, argv[0]);
|
||||||
|
|
||||||
if (fflag && argc == 1) {
|
if (fflag && argc == 1) {
|
||||||
tmp = NULL;
|
tmp = NULL;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user