1 /* vi: set sw=4 ts=4: */
3 * Mini tail implementation for busybox
6 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/types.h>
33 static const struct suffix_mult tail_suffixes[] = {
40 static const int BYTES = 0;
41 static const int LINES = 1;
47 static void tailbuf_append(char *buf, int len)
49 tailbuf = xrealloc(tailbuf, taillen + len);
50 memcpy(tailbuf + taillen, buf, len);
54 static void tailbuf_trunc()
57 s = memchr(tailbuf, '\n', taillen);
58 memmove(tailbuf, s + 1, taillen - ((s + 1) - tailbuf));
59 taillen -= (s + 1) - tailbuf;
63 int tail_main(int argc, char **argv)
65 int from_top = 0, units = LINES, count = 10, sleep_period = 1;
66 int show_headers = 0, hide_headers = 0, follow = 0;
67 int *fds, nfiles = 0, status = EXIT_SUCCESS, nread, nwrite, seen = 0;
68 char *s, *start, *end, buf[BUFSIZ];
71 while ((opt = getopt(argc, argv, "c:fhn:q:s:v")) > 0) {
76 #ifdef BB_FEATURE_FANCY_TAIL
82 count = parse_number(optarg, tail_suffixes);
88 #ifdef BB_FEATURE_FANCY_TAIL
93 sleep_period = parse_number(optarg, 0);
104 /* open all the files */
105 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
106 if (argc == optind) {
107 fds[nfiles++] = STDIN_FILENO;
108 argv[optind] = "standard input";
110 for (i = optind; i < argc; i++) {
111 if (strcmp(argv[i], "-") == 0) {
112 fds[nfiles++] = STDIN_FILENO;
113 argv[i] = "standard input";
114 } else if ((fds[nfiles++] = open(argv[i], O_RDONLY)) < 0) {
115 perror_msg("%s", argv[i]);
116 status = EXIT_FAILURE;
121 #ifdef BB_FEATURE_FANCY_TAIL
123 if (!from_top && units == BYTES)
124 tailbuf = xmalloc(count);
127 for (i = 0; i < nfiles; i++) {
131 lseek(fds[i], 0, SEEK_END);
135 if (show_headers || (!hide_headers && nfiles > 1))
136 printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]);
137 while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
139 #ifdef BB_FEATURE_FANCY_TAIL
140 if (units == BYTES) {
141 if (count - 1 <= seen)
143 else if (count - 1 <= seen + nread)
144 nwrite = nread + seen - (count - 1);
152 if (count - 1 <= seen)
156 for (s = memchr(buf, '\n', nread); s != NULL;
157 s = memchr(s+1, '\n', nread - (s + 1 - buf))) {
158 if (count - 1 <= ++seen) {
159 nwrite = nread - (s + 1 - buf);
165 if (full_write(STDOUT_FILENO, buf + nread - nwrite,
168 status = EXIT_FAILURE;
172 #ifdef BB_FEATURE_FANCY_TAIL
173 if (units == BYTES) {
175 memmove(tailbuf, tailbuf + nread, count - nread);
176 memcpy(tailbuf + count - nread, buf, nread);
178 memcpy(tailbuf, buf + nread - count, count);
185 for (start = buf, end = memchr(buf, '\n', nread);
186 end != NULL; start = end+1,
187 end = memchr(start, '\n', nread - (start - buf))) {
188 if (newline && count <= seen)
190 tailbuf_append(start, end - start + 1);
194 if (newline && count <= seen && nread - (start - buf) > 0)
196 tailbuf_append(start, nread - (start - buf));
203 status = EXIT_FAILURE;
206 #ifdef BB_FEATURE_FANCY_TAIL
207 if (!from_top && units == BYTES) {
210 if (full_write(STDOUT_FILENO, tailbuf + count - seen, seen) < 0) {
212 status = EXIT_FAILURE;
217 if (!from_top && units == LINES) {
218 if (full_write(STDOUT_FILENO, tailbuf, taillen) < 0) {
220 status = EXIT_FAILURE;
230 for (i = 0; i < nfiles; i++) {
234 if ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
235 if (show_headers || (!hide_headers && nfiles > 1))
236 printf("\n==> %s <==\n", argv[optind + i]);
239 full_write(STDOUT_FILENO, buf, nread);
240 } while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0);
245 status = EXIT_FAILURE;