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
31 #include <sys/types.h>
34 static const struct suffix_mult tail_suffixes[] = {
41 static const int BYTES = 0;
42 static const int LINES = 1;
48 static void tailbuf_append(char *buf, int len)
50 tailbuf = xrealloc(tailbuf, taillen + len);
51 memcpy(tailbuf + taillen, buf, len);
55 static void tailbuf_trunc(void)
58 s = memchr(tailbuf, '\n', taillen);
59 memmove(tailbuf, s + 1, taillen - ((s + 1) - tailbuf));
60 taillen -= (s + 1) - tailbuf;
64 int tail_main(int argc, char **argv)
66 int from_top = 0, units = LINES, count = 10, sleep_period = 1;
67 int show_headers = 0, hide_headers = 0, follow = 0;
68 int *fds, nfiles = 0, status = EXIT_SUCCESS, nread, nwrite, seen = 0;
69 char *s, *start, *end, buf[BUFSIZ];
79 line_num = atoi(&argv[1][1]);
88 #ifdef CONFIG_FEATURE_FANCY_TAIL
89 while ((opt = getopt(argc, argv, "c:fn:q:s:v")) > 0) {
91 while ((opt = getopt(argc, argv, "fn:")) > 0) {
97 #ifdef CONFIG_FEATURE_FANCY_TAIL
103 count = parse_number(optarg, tail_suffixes);
106 if (optarg[0] == '+')
109 #ifdef CONFIG_FEATURE_FANCY_TAIL
114 sleep_period = parse_number(optarg, 0);
125 /* open all the files */
126 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
127 if (argc == optind) {
128 fds[nfiles++] = STDIN_FILENO;
129 argv[optind] = "standard input";
131 for (i = optind; i < argc; i++) {
132 if (strcmp(argv[i], "-") == 0) {
133 fds[nfiles++] = STDIN_FILENO;
134 argv[i] = "standard input";
135 } else if ((fds[nfiles++] = open(argv[i], O_RDONLY)) < 0) {
136 perror_msg("%s", argv[i]);
137 status = EXIT_FAILURE;
142 #ifdef CONFIG_FEATURE_FANCY_TAIL
144 if (!from_top && units == BYTES)
145 tailbuf = xmalloc(count);
148 for (i = 0; i < nfiles; i++) {
152 lseek(fds[i], 0, SEEK_END);
156 if (show_headers || (!hide_headers && nfiles > 1))
157 printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]);
158 while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
160 #ifdef CONFIG_FEATURE_FANCY_TAIL
161 if (units == BYTES) {
162 if (count - 1 <= seen)
164 else if (count - 1 <= seen + nread)
165 nwrite = nread + seen - (count - 1);
173 if (count - 1 <= seen)
177 for (s = memchr(buf, '\n', nread); s != NULL;
178 s = memchr(s+1, '\n', nread - (s + 1 - buf))) {
179 if (count - 1 <= ++seen) {
180 nwrite = nread - (s + 1 - buf);
186 if (full_write(STDOUT_FILENO, buf + nread - nwrite,
189 status = EXIT_FAILURE;
193 #ifdef CONFIG_FEATURE_FANCY_TAIL
194 if (units == BYTES) {
196 memmove(tailbuf, tailbuf + nread, count - nread);
197 memcpy(tailbuf + count - nread, buf, nread);
199 memcpy(tailbuf, buf + nread - count, count);
206 for (start = buf, end = memchr(buf, '\n', nread);
207 end != NULL; start = end+1,
208 end = memchr(start, '\n', nread - (start - buf))) {
209 if (newline && count <= seen)
211 tailbuf_append(start, end - start + 1);
215 if (newline && count <= seen && nread - (start - buf) > 0)
217 tailbuf_append(start, nread - (start - buf));
224 status = EXIT_FAILURE;
227 #ifdef CONFIG_FEATURE_FANCY_TAIL
228 if (!from_top && units == BYTES) {
231 if (full_write(STDOUT_FILENO, tailbuf + count - seen, seen) < 0) {
233 status = EXIT_FAILURE;
238 if (!from_top && units == LINES) {
239 if (full_write(STDOUT_FILENO, tailbuf, taillen) < 0) {
241 status = EXIT_FAILURE;
251 for (i = 0; i < nfiles; i++) {
255 if ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
256 if (show_headers || (!hide_headers && nfiles > 1))
257 printf("\n==> %s <==\n", argv[optind + i]);
260 full_write(STDOUT_FILENO, buf, nread);
261 } while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0);
266 status = EXIT_FAILURE;