mirror of
https://github.com/sheumann/hush.git
synced 2025-01-24 16:30:54 +00:00
tail: optimize "tail -c HUGENUM REGULAR_FILE". Closes 3763.
function old new delta tail_main 1541 1547 +6 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
24b71fd894
commit
9e933d9bfc
@ -59,7 +59,8 @@ static const struct suffix_mult tail_suffixes[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct globals {
|
struct globals {
|
||||||
bool status;
|
bool from_top;
|
||||||
|
bool exitcode;
|
||||||
} FIX_ALIASING;
|
} FIX_ALIASING;
|
||||||
#define G (*(struct globals*)&bb_common_bufsiz1)
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ static ssize_t tail_read(int fd, char *buf, size_t count)
|
|||||||
r = full_read(fd, buf, count);
|
r = full_read(fd, buf, count);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
bb_perror_msg(bb_msg_read_error);
|
bb_perror_msg(bb_msg_read_error);
|
||||||
G.status = EXIT_FAILURE;
|
G.exitcode = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
@ -99,7 +100,7 @@ static unsigned eat_num(const char *p)
|
|||||||
p++;
|
p++;
|
||||||
else if (*p == '+') {
|
else if (*p == '+') {
|
||||||
p++;
|
p++;
|
||||||
G.status = 1; /* mark that we saw "+" */
|
G.from_top = 1;
|
||||||
}
|
}
|
||||||
return xatou_sfx(p, tail_suffixes);
|
return xatou_sfx(p, tail_suffixes);
|
||||||
}
|
}
|
||||||
@ -109,7 +110,6 @@ int tail_main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
unsigned count = 10;
|
unsigned count = 10;
|
||||||
unsigned sleep_period = 1;
|
unsigned sleep_period = 1;
|
||||||
bool from_top;
|
|
||||||
const char *str_c, *str_n;
|
const char *str_c, *str_n;
|
||||||
|
|
||||||
char *tailbuf;
|
char *tailbuf;
|
||||||
@ -152,8 +152,6 @@ int tail_main(int argc, char **argv)
|
|||||||
#endif
|
#endif
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
from_top = G.status; /* 1 if there was "-c +N" or "-n +N" */
|
|
||||||
G.status = EXIT_SUCCESS;
|
|
||||||
|
|
||||||
/* open all the files */
|
/* open all the files */
|
||||||
fds = xmalloc(sizeof(fds[0]) * (argc + 1));
|
fds = xmalloc(sizeof(fds[0]) * (argc + 1));
|
||||||
@ -171,7 +169,7 @@ int tail_main(int argc, char **argv)
|
|||||||
do {
|
do {
|
||||||
int fd = open_or_warn_stdin(argv[i]);
|
int fd = open_or_warn_stdin(argv[i]);
|
||||||
if (fd < 0 && !FOLLOW_RETRY) {
|
if (fd < 0 && !FOLLOW_RETRY) {
|
||||||
G.status = EXIT_FAILURE;
|
G.exitcode = EXIT_FAILURE;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fds[nfiles] = fd;
|
fds[nfiles] = fd;
|
||||||
@ -183,15 +181,19 @@ int tail_main(int argc, char **argv)
|
|||||||
|
|
||||||
/* prepare the buffer */
|
/* prepare the buffer */
|
||||||
tailbufsize = BUFSIZ;
|
tailbufsize = BUFSIZ;
|
||||||
if (!from_top && COUNT_BYTES) {
|
if (!G.from_top && COUNT_BYTES) {
|
||||||
if (tailbufsize < count + BUFSIZ) {
|
if (tailbufsize < count + BUFSIZ) {
|
||||||
tailbufsize = count + BUFSIZ;
|
tailbufsize = count + BUFSIZ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tailbuf = xmalloc(tailbufsize);
|
/* tail -c1024m REGULAR_FILE doesn't really need 1G mem block.
|
||||||
|
* (In fact, it doesn't need ANY memory). So delay allocation.
|
||||||
|
*/
|
||||||
|
tailbuf = NULL;
|
||||||
|
|
||||||
/* tail the files */
|
/* tail the files */
|
||||||
fmt = header_fmt_str + 1; /* skip header leading newline on first output */
|
|
||||||
|
fmt = header_fmt_str + 1; /* skip leading newline in the header on the first output */
|
||||||
i = 0;
|
i = 0;
|
||||||
do {
|
do {
|
||||||
char *buf;
|
char *buf;
|
||||||
@ -209,7 +211,7 @@ int tail_main(int argc, char **argv)
|
|||||||
fmt = header_fmt_str;
|
fmt = header_fmt_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!from_top) {
|
if (!G.from_top) {
|
||||||
off_t current = lseek(fd, 0, SEEK_END);
|
off_t current = lseek(fd, 0, SEEK_END);
|
||||||
if (current > 0) {
|
if (current > 0) {
|
||||||
unsigned off;
|
unsigned off;
|
||||||
@ -242,6 +244,9 @@ int tail_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!tailbuf)
|
||||||
|
tailbuf = xmalloc(tailbufsize);
|
||||||
|
|
||||||
buf = tailbuf;
|
buf = tailbuf;
|
||||||
taillen = 0;
|
taillen = 0;
|
||||||
/* "We saw 1st line/byte".
|
/* "We saw 1st line/byte".
|
||||||
@ -249,7 +254,7 @@ int tail_main(int argc, char **argv)
|
|||||||
seen = 1;
|
seen = 1;
|
||||||
newlines_seen = 0;
|
newlines_seen = 0;
|
||||||
while ((nread = tail_read(fd, buf, tailbufsize-taillen)) > 0) {
|
while ((nread = tail_read(fd, buf, tailbufsize-taillen)) > 0) {
|
||||||
if (from_top) {
|
if (G.from_top) {
|
||||||
int nwrite = nread;
|
int nwrite = nread;
|
||||||
if (seen < count) {
|
if (seen < count) {
|
||||||
/* We need to skip a few more bytes/lines */
|
/* We need to skip a few more bytes/lines */
|
||||||
@ -313,7 +318,7 @@ int tail_main(int argc, char **argv)
|
|||||||
buf = tailbuf + taillen;
|
buf = tailbuf + taillen;
|
||||||
}
|
}
|
||||||
} /* while (tail_read() > 0) */
|
} /* while (tail_read() > 0) */
|
||||||
if (!from_top) {
|
if (!G.from_top) {
|
||||||
xwrite(STDOUT_FILENO, tailbuf, taillen);
|
xwrite(STDOUT_FILENO, tailbuf, taillen);
|
||||||
}
|
}
|
||||||
} while (++i < nfiles);
|
} while (++i < nfiles);
|
||||||
@ -368,10 +373,11 @@ int tail_main(int argc, char **argv)
|
|||||||
xwrite(STDOUT_FILENO, tailbuf, nread);
|
xwrite(STDOUT_FILENO, tailbuf, nread);
|
||||||
}
|
}
|
||||||
} while (++i < nfiles);
|
} while (++i < nfiles);
|
||||||
}
|
} /* while (1) */
|
||||||
|
|
||||||
if (ENABLE_FEATURE_CLEAN_UP) {
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||||
free(fds);
|
free(fds);
|
||||||
free(tailbuf);
|
free(tailbuf);
|
||||||
}
|
}
|
||||||
return G.status;
|
return G.exitcode;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user