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>
33 static struct suffix_mult tail_suffixes[] = {
40 #ifndef BB_FEATURE_SIMPLE_TAIL
41 static struct suffix_mult null_suffixes[] = {
46 static const int BYTES = 0;
47 static const int LINES = 1;
53 void tailbuf_append(char *buf, int len)
55 tailbuf = xrealloc(tailbuf, taillen + len);
56 memcpy(tailbuf + taillen, buf, len);
63 s = memchr(tailbuf, '\n', taillen);
64 memmove(tailbuf, s + 1, taillen - ((s + 1) - tailbuf));
65 taillen -= (s + 1) - tailbuf;
69 int tail_main(int argc, char **argv)
71 int from_top = 0, units = LINES, count = 10, sleep_period = 1;
72 int show_headers = 0, hide_headers = 0, follow = 0;
73 int *fds, nfiles = 0, status = EXIT_SUCCESS, nread, nwrite, seen = 0;
74 char *s, *start, *end, buf[BUFSIZ];
77 while ((opt = getopt(argc, argv, "c:fhn:q:s:v")) > 0) {
82 #ifndef BB_FEATURE_SIMPLE_TAIL
88 count = parse_number(optarg, tail_suffixes);
94 #ifndef BB_FEATURE_SIMPLE_TAIL
99 sleep_period = parse_number(optarg, null_suffixes);
110 /* open all the files */
111 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
112 if (argc == optind) {
113 fds[nfiles++] = STDIN_FILENO;
114 argv[optind] = "standard input";
116 for (i = optind; i < argc; i++) {
117 if (strcmp(argv[i], "-") == 0) {
118 fds[nfiles++] = STDIN_FILENO;
119 argv[i] = "standard input";
120 } else if ((fds[nfiles++] = open(argv[i], O_RDONLY)) < 0) {
121 perror_msg("%s", argv[i]);
122 status = EXIT_FAILURE;
127 #ifndef BB_FEATURE_SIMPLE_TAIL
129 if (!from_top && units == BYTES)
130 tailbuf = xmalloc(count);
133 for (i = 0; i < nfiles; i++) {
137 if (show_headers || (!hide_headers && nfiles > 1))
138 printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]);
139 while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
141 #ifndef BB_FEATURE_SIMPLE_TAIL
142 if (units == BYTES) {
143 if (count - 1 <= seen)
145 else if (count - 1 <= seen + nread)
146 nwrite = nread + seen - (count - 1);
154 if (count - 1 <= seen)
158 for (s = memchr(buf, '\n', nread); s != NULL;
159 s = memchr(s+1, '\n', nread - (s + 1 - buf))) {
160 if (count - 1 <= ++seen) {
161 nwrite = nread - (s + 1 - buf);
167 if (full_write(STDOUT_FILENO, buf + nread - nwrite,
170 status = EXIT_FAILURE;
174 #ifndef BB_FEATURE_SIMPLE_TAIL
175 if (units == BYTES) {
177 memmove(tailbuf, tailbuf + nread, count - nread);
178 memcpy(tailbuf + count - nread, buf, nread);
180 memcpy(tailbuf, buf + nread - count, count);
187 for (start = buf, end = memchr(buf, '\n', nread);
188 end != NULL; start = end+1,
189 end = memchr(start, '\n', nread - (start - buf))) {
190 if (newline && count <= seen)
192 tailbuf_append(start, end - start + 1);
196 if (newline && count <= seen && nread - (start - buf) > 0)
198 tailbuf_append(start, nread - (start - buf));
205 status = EXIT_FAILURE;
208 #ifndef BB_FEATURE_SIMPLE_TAIL
209 if (!from_top && units == BYTES) {
212 if (full_write(STDOUT_FILENO, tailbuf + count - seen, seen) < 0) {
214 status = EXIT_FAILURE;
219 if (!from_top && units == LINES) {
220 if (full_write(STDOUT_FILENO, tailbuf, taillen) < 0) {
222 status = EXIT_FAILURE;
232 for (i = 0; i < nfiles; i++) {
236 if ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
237 if (show_headers || (!hide_headers && nfiles > 1))
238 printf("\n==> %s <==\n", argv[optind + i]);
241 full_write(STDOUT_FILENO, buf, nread);
242 } while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0);
247 status = EXIT_FAILURE;