1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71#include "libbb.h"
72#include "common_bufsiz.h"
73
74struct globals {
75 bool from_top;
76 bool exitcode;
77} FIX_ALIASING;
78#define G (*(struct globals*)bb_common_bufsiz1)
79#define INIT_G() do { setup_common_bufsiz(); } while (0)
80
81static void tail_xprint_header(const char *fmt, const char *filename)
82{
83 if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
84 bb_perror_nomsg_and_die();
85}
86
87static ssize_t tail_read(int fd, char *buf, size_t count)
88{
89 ssize_t r;
90
91 r = full_read(fd, buf, count);
92 if (r < 0) {
93 bb_simple_perror_msg(bb_msg_read_error);
94 G.exitcode = EXIT_FAILURE;
95 }
96
97 return r;
98}
99
100#define header_fmt_str "\n==> %s <==\n"
101
102static unsigned eat_num(const char *p)
103{
104 if (*p == '-')
105 p++;
106 else if (*p == '+') {
107 p++;
108 G.from_top = 1;
109 }
110 return xatou_sfx(p, bkm_suffixes);
111}
112
113int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
114int tail_main(int argc, char **argv)
115{
116 unsigned count = 10;
117 unsigned sleep_period = 1;
118 const char *str_c, *str_n;
119
120 char *tailbuf;
121 size_t tailbufsize;
122 unsigned header_threshold = 1;
123 unsigned nfiles;
124 int i, opt;
125
126 int *fds;
127 const char *fmt;
128 int prev_fd;
129
130 INIT_G();
131
132#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
133
134 if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
135 && isdigit(argv[1][1])
136 ) {
137 count = eat_num(argv[1]);
138 argv++;
139 argc--;
140 }
141#endif
142
143
144 opt = getopt32(argv, IF_FEATURE_FANCY_TAIL("^")
145 "fc:n:"IF_FEATURE_FANCY_TAIL("qs:+vF")
146 IF_FEATURE_FANCY_TAIL("\0" "Ff"),
147 &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period)
148 );
149#define FOLLOW (opt & 0x1)
150#define COUNT_BYTES (opt & 0x2)
151
152 if (opt & 0x2) count = eat_num(str_c);
153 if (opt & 0x4) count = eat_num(str_n);
154#if ENABLE_FEATURE_FANCY_TAIL
155
156 if (opt & 0x8) header_threshold = UINT_MAX;
157
158 if (opt & 0x20) header_threshold = 0;
159# define FOLLOW_RETRY (opt & 0x40)
160#else
161# define FOLLOW_RETRY 0
162#endif
163 argc -= optind;
164 argv += optind;
165
166
167 fds = xmalloc(sizeof(fds[0]) * (argc + 1));
168 if (!argv[0]) {
169 struct stat statbuf;
170
171 if (fstat(STDIN_FILENO, &statbuf) == 0
172 && S_ISFIFO(statbuf.st_mode)
173 ) {
174 opt &= ~1;
175 }
176 argv[0] = (char *) bb_msg_standard_input;
177 }
178 nfiles = i = 0;
179 do {
180 int fd = open_or_warn_stdin(argv[i]);
181 if (fd < 0 && !FOLLOW_RETRY) {
182 G.exitcode = EXIT_FAILURE;
183 continue;
184 }
185 fds[nfiles] = fd;
186 argv[nfiles++] = argv[i];
187 } while (++i < argc);
188
189 if (!nfiles)
190 bb_simple_error_msg_and_die("no files");
191
192
193 tailbufsize = BUFSIZ;
194 if (!G.from_top && COUNT_BYTES) {
195 if (tailbufsize < count + BUFSIZ) {
196 tailbufsize = count + BUFSIZ;
197 }
198 }
199
200
201
202 tailbuf = NULL;
203
204
205
206 fmt = header_fmt_str + 1;
207 i = 0;
208 do {
209 char *buf;
210 int taillen;
211 int newlines_seen;
212 unsigned seen;
213 int nread;
214 int fd = fds[i];
215
216 if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
217 continue;
218
219 if (nfiles > header_threshold) {
220 tail_xprint_header(fmt, argv[i]);
221 fmt = header_fmt_str;
222 }
223
224 if (!G.from_top) {
225 off_t current = lseek(fd, 0, SEEK_END);
226 if (current > 0) {
227 unsigned off;
228 if (COUNT_BYTES) {
229
230
231
232 if (count == 0)
233 continue;
234 current -= count;
235 if (current < 0)
236 current = 0;
237 xlseek(fd, current, SEEK_SET);
238 bb_copyfd_size(fd, STDOUT_FILENO, count);
239 continue;
240 }
241#if 1
242
243
244
245
246 off = (count | 0xf);
247 if (off > (INT_MAX / (64*1024)))
248 off = (INT_MAX / (64*1024));
249 current -= off * (64*1024);
250 if (current < 0)
251 current = 0;
252 xlseek(fd, current, SEEK_SET);
253#endif
254 }
255 }
256
257 if (!tailbuf)
258 tailbuf = xmalloc(tailbufsize);
259
260 buf = tailbuf;
261 taillen = 0;
262
263
264 seen = 1;
265 newlines_seen = 0;
266 while ((nread = tail_read(fd, buf, tailbufsize - taillen)) > 0) {
267 if (G.from_top) {
268 int nwrite = nread;
269 if (seen < count) {
270
271 if (COUNT_BYTES) {
272 nwrite -= (count - seen);
273 seen += nread;
274 } else {
275 char *s = buf;
276 do {
277 --nwrite;
278 if (*s++ == '\n' && ++seen == count) {
279 break;
280 }
281 } while (nwrite);
282 }
283 }
284 if (nwrite > 0)
285 xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
286 } else if (count) {
287 if (COUNT_BYTES) {
288 taillen += nread;
289 if (taillen > (int)count) {
290 memmove(tailbuf, tailbuf + taillen - count, count);
291 taillen = count;
292 }
293 } else {
294 int k = nread;
295 int newlines_in_buf = 0;
296
297 do {
298 k--;
299 if (buf[k] == '\n') {
300 newlines_in_buf++;
301 }
302 } while (k);
303
304 if (newlines_seen + newlines_in_buf < (int)count) {
305 newlines_seen += newlines_in_buf;
306 taillen += nread;
307 } else {
308 int extra = (buf[nread-1] != '\n');
309 char *s;
310
311 k = newlines_seen + newlines_in_buf + extra - count;
312 s = tailbuf;
313 while (k) {
314 if (*s == '\n') {
315 k--;
316 }
317 s++;
318 }
319 taillen += nread - (s - tailbuf);
320 memmove(tailbuf, s, taillen);
321 newlines_seen = count - extra;
322 }
323 if (tailbufsize < (size_t)taillen + BUFSIZ) {
324 tailbufsize = taillen + BUFSIZ;
325 tailbuf = xrealloc(tailbuf, tailbufsize);
326 }
327 }
328 buf = tailbuf + taillen;
329 }
330 }
331 if (!G.from_top) {
332 xwrite(STDOUT_FILENO, tailbuf, taillen);
333 }
334 } while (++i < nfiles);
335 prev_fd = fds[i-1];
336
337 tailbuf = xrealloc(tailbuf, BUFSIZ);
338
339 fmt = NULL;
340
341 if (FOLLOW) while (1) {
342 sleep(sleep_period);
343
344 i = 0;
345 do {
346 int nread;
347 const char *filename = argv[i];
348 int fd = fds[i];
349 int new_fd = -1;
350 struct stat sbuf;
351
352 if (FOLLOW_RETRY) {
353 struct stat fsbuf;
354
355 if (fd < 0
356 || fstat(fd, &fsbuf) < 0
357 || stat(filename, &sbuf) < 0
358 || fsbuf.st_dev != sbuf.st_dev
359 || fsbuf.st_ino != sbuf.st_ino
360 ) {
361
362 new_fd = open(filename, O_RDONLY);
363 if (new_fd >= 0) {
364 bb_error_msg("%s has %s; following end of new file",
365 filename, (fd < 0) ? "appeared" : "been replaced"
366 );
367 if (fd < 0) {
368
369
370 fds[i] = fd = new_fd;
371 new_fd = -1;
372 }
373 } else if (fd >= 0) {
374 bb_perror_msg("%s has been renamed or deleted", filename);
375 }
376 }
377 }
378 if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
379 continue;
380 if (nfiles > header_threshold) {
381 fmt = header_fmt_str;
382 }
383 for (;;) {
384
385
386 if (fstat(fd, &sbuf) == 0
387
388 && sbuf.st_size > 0
389 ) {
390 off_t current = lseek(fd, 0, SEEK_CUR);
391 if (sbuf.st_size < current) {
392
393 xlseek(fd, 0, SEEK_SET);
394 }
395 }
396
397 nread = tail_read(fd, tailbuf, BUFSIZ);
398 if (nread <= 0) {
399 if (new_fd < 0)
400 break;
401
402 xmove_fd(new_fd, fd);
403 new_fd = -1;
404 continue;
405 }
406 if (fmt && (fd != prev_fd)) {
407 tail_xprint_header(fmt, filename);
408 fmt = NULL;
409 prev_fd = fd;
410 }
411 xwrite(STDOUT_FILENO, tailbuf, nread);
412 }
413 } while (++i < nfiles);
414 }
415
416 if (ENABLE_FEATURE_CLEAN_UP) {
417 free(fds);
418 free(tailbuf);
419 }
420 return G.exitcode;
421}
422