blob: 778c2d18b87396f5553eb6ea9b476b3a0b4d1539 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Matt Kraai55bccf32001-01-05 02:57:53 +00002/*
3 * Mini tail implementation for busybox
4 *
5 *
6 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 *
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.
12 *
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.
17 *
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
21 *
Erik Andersen3fe39dc2000-01-25 18:13:53 +000022 */
Matt Kraai55bccf32001-01-05 02:57:53 +000023
Eric Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000025
Matt Kraai55bccf32001-01-05 02:57:53 +000026#include <fcntl.h>
Eric Andersened3ef502001-01-27 08:24:39 +000027#include <getopt.h>
28#include <string.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <sys/types.h>
Eric Andersenabc0f4f1999-12-08 23:19:36 +000032
Matt Kraaia164c642001-02-05 17:50:03 +000033static const struct suffix_mult tail_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000034 { "b", 512 },
35 { "k", 1024 },
36 { "m", 1048576 },
37 { NULL, 0 }
38};
39
Mark Whitleyeb60d8b2001-01-05 18:19:30 +000040static const int BYTES = 0;
41static const int LINES = 1;
Matt Kraai55bccf32001-01-05 02:57:53 +000042
43static char *tailbuf;
44static int taillen;
45static int newline;
46
47void tailbuf_append(char *buf, int len)
48{
49 tailbuf = xrealloc(tailbuf, taillen + len);
50 memcpy(tailbuf + taillen, buf, len);
51 taillen += len;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000052}
53
Matt Kraai55bccf32001-01-05 02:57:53 +000054void tailbuf_trunc()
Eric Andersenabc0f4f1999-12-08 23:19:36 +000055{
Matt Kraai55bccf32001-01-05 02:57:53 +000056 char *s;
57 s = memchr(tailbuf, '\n', taillen);
58 memmove(tailbuf, s + 1, taillen - ((s + 1) - tailbuf));
59 taillen -= (s + 1) - tailbuf;
60 newline = 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000061}
62
Eric Andersen98bbd682000-07-31 17:05:58 +000063int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000064{
Matt Kraai55bccf32001-01-05 02:57:53 +000065 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];
69 int i, opt;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000070
Matt Kraai55bccf32001-01-05 02:57:53 +000071 while ((opt = getopt(argc, argv, "c:fhn:q:s:v")) > 0) {
Eric Andersend5fa3e32000-08-02 16:42:58 +000072 switch (opt) {
Matt Kraai55bccf32001-01-05 02:57:53 +000073 case 'f':
74 follow = 1;
75 break;
Eric Andersen98bbd682000-07-31 17:05:58 +000076#ifndef BB_FEATURE_SIMPLE_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +000077 case 'c':
78 units = BYTES;
79 /* FALLS THROUGH */
Eric Andersen98bbd682000-07-31 17:05:58 +000080#endif
Matt Kraai55bccf32001-01-05 02:57:53 +000081 case 'n':
82 count = parse_number(optarg, tail_suffixes);
83 if (count < 0)
84 count = -count;
Eric Andersen98bbd682000-07-31 17:05:58 +000085 if (optarg[0] == '+')
Matt Kraai55bccf32001-01-05 02:57:53 +000086 from_top = 1;
Eric Andersend5fa3e32000-08-02 16:42:58 +000087 break;
Eric Andersen98bbd682000-07-31 17:05:58 +000088#ifndef BB_FEATURE_SIMPLE_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +000089 case 'q':
90 hide_headers = 1;
Eric Andersend5fa3e32000-08-02 16:42:58 +000091 break;
Matt Kraai55bccf32001-01-05 02:57:53 +000092 case 's':
Eric Andersen963791a2001-02-18 20:13:18 +000093 sleep_period = parse_number(optarg, 0);
Matt Kraai55bccf32001-01-05 02:57:53 +000094 break;
95 case 'v':
96 show_headers = 1;
97 break;
98#endif
99 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000100 show_usage();
Eric Andersend5fa3e32000-08-02 16:42:58 +0000101 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 }
Matt Kraai55bccf32001-01-05 02:57:53 +0000103
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";
109 } else {
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;
117 }
118 }
119 }
120
121#ifndef BB_FEATURE_SIMPLE_TAIL
122 /* tail the files */
123 if (!from_top && units == BYTES)
124 tailbuf = xmalloc(count);
125#endif
126
127 for (i = 0; i < nfiles; i++) {
128 if (fds[i] == -1)
129 continue;
130 seen = 0;
131 if (show_headers || (!hide_headers && nfiles > 1))
132 printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]);
133 while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
134 if (from_top) {
135#ifndef BB_FEATURE_SIMPLE_TAIL
136 if (units == BYTES) {
137 if (count - 1 <= seen)
138 nwrite = nread;
139 else if (count - 1 <= seen + nread)
140 nwrite = nread + seen - (count - 1);
141 else
142 nwrite = 0;
143 seen += nread;
144 } else {
145#else
146 {
147#endif
148 if (count - 1 <= seen)
149 nwrite = nread;
150 else {
151 nwrite = 0;
152 for (s = memchr(buf, '\n', nread); s != NULL;
153 s = memchr(s+1, '\n', nread - (s + 1 - buf))) {
154 if (count - 1 <= ++seen) {
155 nwrite = nread - (s + 1 - buf);
156 break;
157 }
158 }
159 }
160 }
161 if (full_write(STDOUT_FILENO, buf + nread - nwrite,
162 nwrite) < 0) {
163 perror_msg("write");
164 status = EXIT_FAILURE;
165 break;
166 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000167 } else {
Matt Kraai55bccf32001-01-05 02:57:53 +0000168#ifndef BB_FEATURE_SIMPLE_TAIL
169 if (units == BYTES) {
170 if (nread < count) {
171 memmove(tailbuf, tailbuf + nread, count - nread);
172 memcpy(tailbuf + count - nread, buf, nread);
173 } else {
174 memcpy(tailbuf, buf + nread - count, count);
175 }
176 seen += nread;
177 } else {
178#else
179 {
180#endif
181 for (start = buf, end = memchr(buf, '\n', nread);
182 end != NULL; start = end+1,
183 end = memchr(start, '\n', nread - (start - buf))) {
184 if (newline && count <= seen)
185 tailbuf_trunc();
186 tailbuf_append(start, end - start + 1);
187 seen++;
188 newline = 1;
189 }
190 if (newline && count <= seen && nread - (start - buf) > 0)
191 tailbuf_trunc();
192 tailbuf_append(start, nread - (start - buf));
Eric Andersend5fa3e32000-08-02 16:42:58 +0000193 }
194 }
195 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000196
Matt Kraai55bccf32001-01-05 02:57:53 +0000197 if (nread < 0) {
198 perror_msg("read");
199 status = EXIT_FAILURE;
200 }
201
202#ifndef BB_FEATURE_SIMPLE_TAIL
203 if (!from_top && units == BYTES) {
204 if (count < seen)
205 seen = count;
206 if (full_write(STDOUT_FILENO, tailbuf + count - seen, seen) < 0) {
207 perror_msg("write");
208 status = EXIT_FAILURE;
209 }
210 }
211#endif
212
213 if (!from_top && units == LINES) {
214 if (full_write(STDOUT_FILENO, tailbuf, taillen) < 0) {
215 perror_msg("write");
216 status = EXIT_FAILURE;
217 }
218 }
219
220 taillen = 0;
221 }
222
223 while (follow) {
224 sleep(sleep_period);
225
226 for (i = 0; i < nfiles; i++) {
227 if (fds[i] == -1)
228 continue;
229
230 if ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
231 if (show_headers || (!hide_headers && nfiles > 1))
232 printf("\n==> %s <==\n", argv[optind + i]);
233
234 do {
235 full_write(STDOUT_FILENO, buf, nread);
236 } while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0);
237 }
238
239 if (nread < 0) {
240 perror_msg("read");
241 status = EXIT_FAILURE;
242 }
243 }
244 }
245
246 return status;
247}