- minor shrinkage

text    data     bss     dec     hex filename
   1431       0       4    1435     59b tail.o.orig
   1396       0       0    1396     574 tail.o
This commit is contained in:
Bernhard Reutner-Fischer 2007-04-04 20:29:15 +00:00
parent 10aed96f14
commit d9c2d5fe4f

View File

@ -33,7 +33,10 @@ static const struct suffix_mult tail_suffixes[] = {
{ NULL, 0 } { NULL, 0 }
}; };
static int status; struct globals {
bool status;
};
#define G (*(struct globals*)&bb_common_bufsiz1)
static void tail_xprint_header(const char *fmt, const char *filename) static void tail_xprint_header(const char *fmt, const char *filename)
{ {
@ -54,7 +57,7 @@ static ssize_t tail_read(int fd, char *buf, size_t count)
r = safe_read(fd, buf, count); r = safe_read(fd, buf, count);
if (r < 0) { if (r < 0) {
bb_perror_msg(bb_msg_read_error); bb_perror_msg(bb_msg_read_error);
status = EXIT_FAILURE; G.status = EXIT_FAILURE;
} }
return r; return r;
@ -64,7 +67,7 @@ static const char header_fmt[] = "\n==> %s <==\n";
static unsigned eat_num(const char *p) { static unsigned eat_num(const char *p) {
if (*p == '-') p++; if (*p == '-') p++;
else if (*p == '+') { p++; status = 1; } else if (*p == '+') { p++; G.status = EXIT_FAILURE; }
return xatou_sfx(p, tail_suffixes); return xatou_sfx(p, tail_suffixes);
} }
@ -111,11 +114,12 @@ int tail_main(int argc, char **argv)
#endif #endif
argc -= optind; argc -= optind;
argv += optind; argv += optind;
from_top = status; from_top = G.status;
/* open all the files */ /* open all the files */
fds = xmalloc(sizeof(int) * (argc + 1)); fds = xmalloc(sizeof(int) * (argc + 1));
status = nfiles = i = 0; nfiles = i = 0;
G.status = EXIT_SUCCESS;
if (argc == 0) { if (argc == 0) {
struct stat statbuf; struct stat statbuf;
@ -123,23 +127,15 @@ int tail_main(int argc, char **argv)
opt &= ~1; /* clear FOLLOW */ opt &= ~1; /* clear FOLLOW */
} }
*argv = (char *) bb_msg_standard_input; *argv = (char *) bb_msg_standard_input;
goto DO_STDIN;
} }
do { do {
if (NOT_LONE_DASH(argv[i])) { FILE* fil = fopen_or_warn_stdin(argv[i]);
fds[nfiles] = open(argv[i], O_RDONLY); if (!fil) {
if (fds[nfiles] < 0) { G.status = EXIT_FAILURE;
bb_perror_msg("%s", argv[i]); continue;
status = EXIT_FAILURE;
continue;
}
} else {
DO_STDIN: /* "-" */
fds[nfiles] = STDIN_FILENO;
} }
argv[nfiles] = argv[i]; fds[nfiles] = fileno(fil);
++nfiles; argv[nfiles++] = argv[i];
} while (++i < argc); } while (++i < argc);
if (!nfiles) if (!nfiles)
@ -217,13 +213,11 @@ int tail_main(int argc, char **argv)
if (newline + nbuf < count) { if (newline + nbuf < count) {
newline += nbuf; newline += nbuf;
taillen += nread; taillen += nread;
} else { } else {
int extra = 0; int extra = 0;
if (buf[nread-1] != '\n') {
extra = 1;
}
if (buf[nread-1] != '\n')
extra = 1;
k = newline + nbuf + extra - count; k = newline + nbuf + extra - count;
s = tailbuf; s = tailbuf;
while (k) { while (k) {
@ -232,7 +226,6 @@ int tail_main(int argc, char **argv)
} }
++s; ++s;
} }
taillen += nread - (s - tailbuf); taillen += nread - (s - tailbuf);
memmove(tailbuf, s, taillen); memmove(tailbuf, s, taillen);
newline = count - extra; newline = count - extra;
@ -273,6 +266,8 @@ int tail_main(int argc, char **argv)
} }
} while (++i < nfiles); } while (++i < nfiles);
} }
if (ENABLE_FEATURE_CLEAN_UP) {
return status; free(fds);
}
return G.status;
} }