[hackers] [sbase] Audit tail(1) || FRIGN
commit 6372a8f227317c3919fcbdf96dbd9ec4267b2336
Author: FRIGN <dev_AT_frign.de>
Date: Tue Mar 17 23:24:43 2015 +0100
Audit tail(1)
1) Specify default in manpage under flag.
2) Boolean and return value style fixes.
3) argv-argc-centric loop.
4) No need to check for argc == 1 before the fflag-subroutine.
5) Remove indentation.
6) Empty line before return.
diff --git a/README b/README
index 66443fd..2ddf7a6 100644
--- a/README
+++ b/README
_AT_@ -71,7 +71,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=*| sponge non-posix none
#*| strings yes none
=*| sync non-posix none
-=* tail yes none
+=*| tail yes none
=* tar non-posix none
=*| tee yes none
=* test yes none
diff --git a/tail.1 b/tail.1
index c7d46cb..89f2ad3 100644
--- a/tail.1
+++ b/tail.1
_AT_@ -1,4 +1,4 @@
-.Dd March 5, 2015
+.Dd March 17, 2015
.Dt TAIL 1
.Os sbase
.Sh NAME
_AT_@ -31,7 +31,7 @@ it is an offset from the beginning of each
.Ar file .
If
.Ar num
-begins with '-' it is as if no sign was given.
+begins with '-' it is as if no sign was given. The default is 10 lines.
.It Fl f
If one
.Ar file
diff --git a/tail.c b/tail.c
index 2711cc9..2e64ce5 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -24,7 +24,7 @@ dropinit(FILE *fp, const char *str)
ssize_t len;
if (mode == 'n') {
- while (i < num && (len = getline(&buf, &size, fp)) != -1)
+ while (i < num && (len = getline(&buf, &size, fp)) >= 0)
if (len > 0 && buf[len - 1] == '\n')
i++;
} else {
_AT_@ -106,52 +106,51 @@ main(int argc, char *argv[])
usage();
} ARGEND;
- if (argc == 0)
+ if (!argc)
tail(stdin, "<stdin>");
else {
if ((many = argc > 1) && fflag)
usage();
- for (newline = 0; argc > 0; argc--, argv++) {
- if (!(fp = fopen(argv[0], "r"))) {
- weprintf("fopen %s:", argv[0]);
+ for (newline = 0; *argv; argc--, argv++) {
+ if (!(fp = fopen(*argv, "r"))) {
+ weprintf("fopen %s:", *argv);
ret = 1;
continue;
}
if (many)
- printf("%s==> %s <==\n",
- newline ? "\n" : "", argv[0]);
- if (stat(argv[0], &st1) < 0)
- eprintf("stat %s:", argv[0]);
+ printf("%s==> %s <==\n", newline ? "\n" : "", *argv);
+ if (stat(*argv, &st1) < 0)
+ eprintf("stat %s:", *argv);
if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
fflag = 0;
newline = 1;
- tail(fp, argv[0]);
-
- if (fflag && argc == 1) {
- tmp = NULL;
- tmpsize = 0;
- for (;;) {
- while (getline(&tmp, &tmpsize, fp) != -1) {
- fputs(tmp, stdout);
- fflush(stdout);
- }
- if (ferror(fp))
- eprintf("readline %s:", argv[0]);
- clearerr(fp);
- /* ignore error in case file was removed, we continue
- * tracking the existing open file descriptor */
- if (!stat(argv[0], &st2)) {
- if (st2.st_size < st1.st_size) {
- fprintf(stderr, "%s: file truncated\n", argv[0]);
- rewind(fp);
- }
- st1 = st2;
+ tail(fp, *argv);
+
+ if (!fflag) {
+ fclose(fp);
+ continue;
+ }
+ for (tmp = NULL, tmpsize = 0;;) {
+ while (getline(&tmp, &tmpsize, fp) >= 0) {
+ fputs(tmp, stdout);
+ fflush(stdout);
+ }
+ if (ferror(fp))
+ eprintf("readline %s:", *argv);
+ clearerr(fp);
+ /* ignore error in case file was removed, we continue
+ * tracking the existing open file descriptor */
+ if (!stat(*argv, &st2)) {
+ if (st2.st_size < st1.st_size) {
+ fprintf(stderr, "%s: file truncated\n", *argv);
+ rewind(fp);
}
- sleep(1);
+ st1 = st2;
}
+ sleep(1);
}
- fclose(fp);
}
}
+
return ret;
}
Received on Tue Mar 24 2015 - 23:54:18 CET
This archive was generated by hypermail 2.3.0
: Wed Mar 25 2015 - 00:11:52 CET