blob: 40511aa7bbf3663dff4a90b4864cee8181470f32 [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 Kraai24ac0172000-12-18 21:38:57 +000033static struct suffix_mult tail_suffixes[] = {
34 { "b", 512 },
35 { "k", 1024 },
36 { "m", 1048576 },
37 { NULL, 0 }
38};
39
Eric Andersen98bbd682000-07-31 17:05:58 +000040#ifndef BB_FEATURE_SIMPLE_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +000041static struct suffix_mult null_suffixes[] = {
42 { NULL, 0 }
43};
Eric Andersen98bbd682000-07-31 17:05:58 +000044#endif
Eric Andersenabc0f4f1999-12-08 23:19:36 +000045
Mark Whitleyeb60d8b2001-01-05 18:19:30 +000046static const int BYTES = 0;
47static const int LINES = 1;
Matt Kraai55bccf32001-01-05 02:57:53 +000048
49static char *tailbuf;
50static int taillen;
51static int newline;
52
53void tailbuf_append(char *buf, int len)
54{
55 tailbuf = xrealloc(tailbuf, taillen + len);
56 memcpy(tailbuf + taillen, buf, len);
57 taillen += len;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000058}
59
Matt Kraai55bccf32001-01-05 02:57:53 +000060void tailbuf_trunc()
Eric Andersenabc0f4f1999-12-08 23:19:36 +000061{
Matt Kraai55bccf32001-01-05 02:57:53 +000062 char *s;
63 s = memchr(tailbuf, '\n', taillen);
64 memmove(tailbuf, s + 1, taillen - ((s + 1) - tailbuf));
65 taillen -= (s + 1) - tailbuf;
66 newline = 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000067}
68
Eric Andersen98bbd682000-07-31 17:05:58 +000069int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000070{
Matt Kraai55bccf32001-01-05 02:57:53 +000071 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];
75 int i, opt;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000076
Matt Kraai55bccf32001-01-05 02:57:53 +000077 while ((opt = getopt(argc, argv, "c:fhn:q:s:v")) > 0) {
Eric Andersend5fa3e32000-08-02 16:42:58 +000078 switch (opt) {
Matt Kraai55bccf32001-01-05 02:57:53 +000079 case 'f':
80 follow = 1;
81 break;
Eric Andersen98bbd682000-07-31 17:05:58 +000082#ifndef BB_FEATURE_SIMPLE_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +000083 case 'c':
84 units = BYTES;
85 /* FALLS THROUGH */
Eric Andersen98bbd682000-07-31 17:05:58 +000086#endif
Matt Kraai55bccf32001-01-05 02:57:53 +000087 case 'n':
88 count = parse_number(optarg, tail_suffixes);
89 if (count < 0)
90 count = -count;
Eric Andersen98bbd682000-07-31 17:05:58 +000091 if (optarg[0] == '+')
Matt Kraai55bccf32001-01-05 02:57:53 +000092 from_top = 1;
Eric Andersend5fa3e32000-08-02 16:42:58 +000093 break;
Eric Andersen98bbd682000-07-31 17:05:58 +000094#ifndef BB_FEATURE_SIMPLE_TAIL
Matt Kraai55bccf32001-01-05 02:57:53 +000095 case 'q':
96 hide_headers = 1;
Eric Andersend5fa3e32000-08-02 16:42:58 +000097 break;
Matt Kraai55bccf32001-01-05 02:57:53 +000098 case 's':
99 sleep_period = parse_number(optarg, null_suffixes);
100 break;
101 case 'v':
102 show_headers = 1;
103 break;
104#endif
105 default:
106 usage(tail_usage);
Eric Andersend5fa3e32000-08-02 16:42:58 +0000107 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 }
Matt Kraai55bccf32001-01-05 02:57:53 +0000109
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";
115 } else {
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;
123 }
124 }
125 }
126
127#ifndef BB_FEATURE_SIMPLE_TAIL
128 /* tail the files */
129 if (!from_top && units == BYTES)
130 tailbuf = xmalloc(count);
131#endif
132
133 for (i = 0; i < nfiles; i++) {
134 if (fds[i] == -1)
135 continue;
136 seen = 0;
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) {
140 if (from_top) {
141#ifndef BB_FEATURE_SIMPLE_TAIL
142 if (units == BYTES) {
143 if (count - 1 <= seen)
144 nwrite = nread;
145 else if (count - 1 <= seen + nread)
146 nwrite = nread + seen - (count - 1);
147 else
148 nwrite = 0;
149 seen += nread;
150 } else {
151#else
152 {
153#endif
154 if (count - 1 <= seen)
155 nwrite = nread;
156 else {
157 nwrite = 0;
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);
162 break;
163 }
164 }
165 }
166 }
167 if (full_write(STDOUT_FILENO, buf + nread - nwrite,
168 nwrite) < 0) {
169 perror_msg("write");
170 status = EXIT_FAILURE;
171 break;
172 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000173 } else {
Matt Kraai55bccf32001-01-05 02:57:53 +0000174#ifndef BB_FEATURE_SIMPLE_TAIL
175 if (units == BYTES) {
176 if (nread < count) {
177 memmove(tailbuf, tailbuf + nread, count - nread);
178 memcpy(tailbuf + count - nread, buf, nread);
179 } else {
180 memcpy(tailbuf, buf + nread - count, count);
181 }
182 seen += nread;
183 } else {
184#else
185 {
186#endif
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)
191 tailbuf_trunc();
192 tailbuf_append(start, end - start + 1);
193 seen++;
194 newline = 1;
195 }
196 if (newline && count <= seen && nread - (start - buf) > 0)
197 tailbuf_trunc();
198 tailbuf_append(start, nread - (start - buf));
Eric Andersend5fa3e32000-08-02 16:42:58 +0000199 }
200 }
201 }
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000202
Matt Kraai55bccf32001-01-05 02:57:53 +0000203 if (nread < 0) {
204 perror_msg("read");
205 status = EXIT_FAILURE;
206 }
207
208#ifndef BB_FEATURE_SIMPLE_TAIL
209 if (!from_top && units == BYTES) {
210 if (count < seen)
211 seen = count;
212 if (full_write(STDOUT_FILENO, tailbuf + count - seen, seen) < 0) {
213 perror_msg("write");
214 status = EXIT_FAILURE;
215 }
216 }
217#endif
218
219 if (!from_top && units == LINES) {
220 if (full_write(STDOUT_FILENO, tailbuf, taillen) < 0) {
221 perror_msg("write");
222 status = EXIT_FAILURE;
223 }
224 }
225
226 taillen = 0;
227 }
228
229 while (follow) {
230 sleep(sleep_period);
231
232 for (i = 0; i < nfiles; i++) {
233 if (fds[i] == -1)
234 continue;
235
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]);
239
240 do {
241 full_write(STDOUT_FILENO, buf, nread);
242 } while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0);
243 }
244
245 if (nread < 0) {
246 perror_msg("read");
247 status = EXIT_FAILURE;
248 }
249 }
250 }
251
252 return status;
253}