blob: 1091d28edb4ffde0829ac490df5dfa07b878e62f [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen3fe39dc2000-01-25 18:13:53 +00002/* tail -- output the last part of file(s)
3 Copyright (C) 89, 90, 91, 95, 1996 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
20 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
21 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>.
22
23 Rewrote the option parser, removed locales support,
Eric Andersen98bbd682000-07-31 17:05:58 +000024 and generally busyboxed, Erik Andersen <andersen@lineo.com>
Erik Andersen3fe39dc2000-01-25 18:13:53 +000025
26 Removed superfluous options and associated code ("-c", "-n", "-q").
Erik Andersenfac10d72000-02-07 05:29:42 +000027 Removed "tail -f" support for multiple files.
Erik Andersen3fe39dc2000-01-25 18:13:53 +000028 Both changes by Friedrich Vedder <fwv@myrtle.lahn.de>.
29
Eric Andersen98bbd682000-07-31 17:05:58 +000030 Compleate Rewrite to correctly support "-NUM", "+NUM", and "-s" by
31 E.Allen Soard (esp@espsw.net).
32
Erik Andersen3fe39dc2000-01-25 18:13:53 +000033 */
Erik Andersen3fe39dc2000-01-25 18:13:53 +000034#include <sys/types.h>
Erik Andersen3fe39dc2000-01-25 18:13:53 +000035#include <fcntl.h>
Eric Andersen98bbd682000-07-31 17:05:58 +000036#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39#include <string.h>
40#include <getopt.h>
Eric Andersen3570a342000-09-25 21:45:58 +000041#include "busybox.h"
Eric Andersenabc0f4f1999-12-08 23:19:36 +000042
Eric Andersen98bbd682000-07-31 17:05:58 +000043#define STDIN "standard input"
44#define LINES 0
45#define BYTES 1
Eric Andersen1792f8c1999-12-09 06:11:36 +000046
Eric Andersen98bbd682000-07-31 17:05:58 +000047static int n_files = 0;
48static char **files = NULL;
Eric Andersend5fa3e32000-08-02 16:42:58 +000049static char * buffer;
50static ssize_t bytes_read=0;
51static ssize_t bs;
52static ssize_t filelocation=0;
53static char pip;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000054
Eric Andersen98bbd682000-07-31 17:05:58 +000055#ifdef BB_FEATURE_SIMPLE_TAIL
56static const char unit_type=LINES;
Eric Andersen98bbd682000-07-31 17:05:58 +000057#else
58static char unit_type=LINES;
Eric Andersen98bbd682000-07-31 17:05:58 +000059static char verbose = 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000060#endif
61
Eric Andersen98bbd682000-07-31 17:05:58 +000062static off_t units=0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000063
Eric Andersend5fa3e32000-08-02 16:42:58 +000064int tail_stream(int fd)
Eric Andersenabc0f4f1999-12-08 23:19:36 +000065{
Eric Andersend5fa3e32000-08-02 16:42:58 +000066 ssize_t startpoint;
Eric Andersen98bbd682000-07-31 17:05:58 +000067 ssize_t endpoint=0;
68 ssize_t count=0;
69 ssize_t filesize=0;
Pavel Roskin8237add2000-08-22 15:38:16 +000070 int direction=1;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000071
Eric Andersend5fa3e32000-08-02 16:42:58 +000072 filelocation=0;
73 startpoint=bs=BUFSIZ;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000074
Eric Andersen98bbd682000-07-31 17:05:58 +000075 filesize=lseek(fd, -1, SEEK_END)+1;
Eric Andersend5fa3e32000-08-02 16:42:58 +000076 pip=(filesize<=0);
Eric Andersen98bbd682000-07-31 17:05:58 +000077
78 if(units>=0)
79 lseek(fd,0,SEEK_SET);
80 else {
81 direction=-1;
82 count=1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 }
Eric Andersen98bbd682000-07-31 17:05:58 +000084 while(units != 0) {
Eric Andersend5fa3e32000-08-02 16:42:58 +000085 if (pip) {
Eric Andersen98bbd682000-07-31 17:05:58 +000086 char * line;
87 ssize_t f_size=0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +000088
Eric Andersen98bbd682000-07-31 17:05:58 +000089 bs=BUFSIZ;
Matt Kraai322ae932000-09-13 02:46:14 +000090 line=xmalloc(bs);
Eric Andersen98bbd682000-07-31 17:05:58 +000091 while(1) {
92 bytes_read=read(fd,line,bs);
93 if(bytes_read<=0)
94 break;
Matt Kraai322ae932000-09-13 02:46:14 +000095 buffer=xrealloc(buffer,f_size+bytes_read);
Eric Andersen98bbd682000-07-31 17:05:58 +000096 memcpy(&buffer[f_size],line,bytes_read);
97 filelocation=f_size+=bytes_read;
98 }
99 bs=f_size;
100 if(direction<0)
101 bs--;
102 if (line)
103 free(line);
104 } else {
105 filelocation = lseek(fd, 0, SEEK_CUR);
106 if(direction<0) {
107 if(filelocation<bs)
108 bs=filelocation;
109 filelocation = lseek(fd, -bs, SEEK_CUR);
110 }
111 bytes_read = read(fd, buffer, bs);
112 if (bytes_read <= 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 break;
Eric Andersen98bbd682000-07-31 17:05:58 +0000114 bs=bytes_read;
115 }
116 startpoint=bs;
117 if(direction>0) {
118 endpoint=startpoint;
119 startpoint=0;
120 }
121 for(;startpoint!=endpoint;startpoint+=direction) {
122#ifndef BB_FEATURE_SIMPLE_TAIL
123 if(unit_type==BYTES)
124 count++;
125 else
126#endif
127 if(buffer[startpoint-1]=='\n')
128 count++;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000129 if (!pip)
Eric Andersen98bbd682000-07-31 17:05:58 +0000130 filelocation=lseek(fd,0,SEEK_CUR);
Eric Andersend5fa3e32000-08-02 16:42:58 +0000131 if(count==units*direction)
Eric Andersen98bbd682000-07-31 17:05:58 +0000132 break;
133 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000134 if((count==units*direction) | pip)
Eric Andersen98bbd682000-07-31 17:05:58 +0000135 break;
136 if(direction<0){
137 filelocation = lseek(fd, -bytes_read, SEEK_CUR);
138 if(filelocation==0)
139 break;
140 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000142 if(pip && (direction<0))
Eric Andersen98bbd682000-07-31 17:05:58 +0000143 bs++;
144 bytes_read=bs-startpoint;
145 memcpy(&buffer[0],&buffer[startpoint],bytes_read);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000146
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 return 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000148}
149
Eric Andersen98bbd682000-07-31 17:05:58 +0000150void add_file(char *name)
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000151{
Eric Andersen98bbd682000-07-31 17:05:58 +0000152 ++n_files;
Matt Kraai322ae932000-09-13 02:46:14 +0000153 files = xrealloc(files, n_files);
154 files[n_files - 1] = (char *) xmalloc(strlen(name) + 1);
Eric Andersen98bbd682000-07-31 17:05:58 +0000155 strcpy(files[n_files - 1], name);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000156}
157
Eric Andersen46ade972000-08-02 19:57:18 +0000158void checknumbers(const char* name)
159{
160 int test=atoi(name);
161 if(test){
162 units=test;
163 if(units<0)
164 units=units-1;
165 } else {
166 fatalError("Unrecognised number '%s'\n", name);
167 }
168}
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000169
Eric Andersen98bbd682000-07-31 17:05:58 +0000170int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000171{
Eric Andersen98bbd682000-07-31 17:05:58 +0000172 int show_headers = 1;
173 int test;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000174 int opt;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000175 char follow=0;
176 int sleep_int=1;
177 int *fd;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000178
Eric Andersen98bbd682000-07-31 17:05:58 +0000179 opterr = 0;
180
Eric Andersen46ade972000-08-02 19:57:18 +0000181 while ((opt=getopt(argc,argv,"c:fhn:s:q:v123456789+")) >0) {
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000182
Eric Andersend5fa3e32000-08-02 16:42:58 +0000183 switch (opt) {
Eric Andersen46ade972000-08-02 19:57:18 +0000184 case '1':case '2':case '3':case '4':case '5':
185 case '6':case '7':case '8':case '9':case '0':
186 checknumbers(argv[optind-1]);
187 break;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000188
Eric Andersen98bbd682000-07-31 17:05:58 +0000189#ifndef BB_FEATURE_SIMPLE_TAIL
Eric Andersen98bbd682000-07-31 17:05:58 +0000190 case 'c':
191 unit_type = BYTES;
192 test = atoi(optarg);
193 if(test==0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 usage(tail_usage);
Eric Andersen98bbd682000-07-31 17:05:58 +0000195 if(optarg[strlen(optarg)-1]>'9') {
196 switch (optarg[strlen(optarg)-1]) {
197 case 'b':
198 test *= 512;
199 break;
200 case 'k':
201 test *= 1024;
202 break;
203 case 'm':
204 test *= (1024 * 1024);
205 break;
206 default:
207 fprintf(stderr,"Size must be b,k, or m.");
208 usage(tail_usage);
209 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 }
Eric Andersen98bbd682000-07-31 17:05:58 +0000211 if(optarg[0]=='+')
212 units=test+1;
213 else
214 units=-(test+1);
215 break;
216 case 'q':
217 show_headers = 0;
218 break;
219 case 's':
220 sleep_int = atoi(optarg);
221 if(sleep_int<1)
222 sleep_int=1;
223 break;
224 case 'v':
225 verbose = 1;
226 break;
227#endif
228 case 'f':
229 follow = 1;
230 break;
231 case 'h':
232 usage(tail_usage);
233 break;
234 case 'n':
235 test = atoi(optarg);
236 if (test) {
237 if (optarg[0] == '+')
238 units = test;
239 else
240 units = -(test+1);
241 } else
242 usage(tail_usage);
243 break;
244 default:
Eric Andersend5fa3e32000-08-02 16:42:58 +0000245 errorMsg("\nUnknown arg: %c.\n\n",optopt);
Eric Andersen98bbd682000-07-31 17:05:58 +0000246 usage(tail_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 }
Eric Andersen1792f8c1999-12-09 06:11:36 +0000248 }
Eric Andersen46ade972000-08-02 19:57:18 +0000249 while (optind <= argc) {
250 if(optind==argc) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000251 if (n_files==0)
252 add_file(STDIN);
253 else
254 break;
255 }else {
Eric Andersen46ade972000-08-02 19:57:18 +0000256 if (*argv[optind] == '+') {
257 checknumbers(argv[optind]);
258 }
259 else if (!strcmp(argv[optind], "-")) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000260 add_file(STDIN);
Eric Andersen46ade972000-08-02 19:57:18 +0000261 } else {
262 add_file(argv[optind]);
263 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000264 optind++;
265 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000266 }
Eric Andersen98bbd682000-07-31 17:05:58 +0000267 if(units==0)
268 units=-11;
269 if(units>0)
270 units--;
Matt Kraai322ae932000-09-13 02:46:14 +0000271 fd=xmalloc(sizeof(int)*n_files);
Eric Andersen98bbd682000-07-31 17:05:58 +0000272 if (n_files == 1)
273#ifndef BB_FEATURE_SIMPLE_TAIL
274 if (!verbose)
275#endif
276 show_headers = 0;
Matt Kraai322ae932000-09-13 02:46:14 +0000277 buffer=xmalloc(BUFSIZ);
Eric Andersen98bbd682000-07-31 17:05:58 +0000278 for (test = 0; test < n_files; test++) {
279 if (show_headers)
280 printf("==> %s <==\n", files[test]);
Eric Andersend5fa3e32000-08-02 16:42:58 +0000281 if (!strcmp(files[test], STDIN))
282 fd[test] = 0;
283 else
284 fd[test] = open(files[test], O_RDONLY);
285 if (fd[test] == -1)
286 fatalError("Unable to open file %s.\n", files[test]);
287 tail_stream(fd[test]);
288
289 bs=BUFSIZ;
290 while (1) {
291 if((filelocation>0 || pip)){
292 write(1,buffer,bytes_read);
293 }
294 bytes_read = read(fd[test], buffer, bs);
295 filelocation+=bytes_read;
296 if (bytes_read <= 0) {
297 break;
298 }
299 usleep(sleep_int * 1000);
300 }
301 if(n_files>1)
302 printf("\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000304 while(1){
305 for (test = 0; test < n_files; test++) {
306 if(!follow){
307 close(fd[test]);
308 continue;
309 } else {
310 sleep(sleep_int);
311 bytes_read = read(fd[test], buffer, bs);
312 if(bytes_read>0) {
313 if (show_headers)
314 printf("==> %s <==\n", files[test]);
315 write(1,buffer,bytes_read);
316 if(n_files>1)
317 printf("\n");
318 }
319 }
320 }
321 if(!follow)
322 break;
323 }
324 if (fd)
325 free(fd);
326 if (buffer)
327 free(buffer);
Eric Andersen98bbd682000-07-31 17:05:58 +0000328 if(files)
329 free(files);
Eric Andersen98bbd682000-07-31 17:05:58 +0000330 return 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000331}
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000332
Eric Andersen98bbd682000-07-31 17:05:58 +0000333/*
334Local Variables:
335c-file-style: "linux"
336c-basic-offset: 4
337tab-width: 4
338End:
339*/