blob: a9da95462efc2743cbdffa3b59cd648f3683719b [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
Matt Kraai69229a62000-10-19 21:28:32 +000064static int 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 Andersen98bbd682000-07-31 17:05:58 +0000158int tail_main(int argc, char **argv)
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000159{
Eric Andersen98bbd682000-07-31 17:05:58 +0000160 int show_headers = 1;
161 int test;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000162 int opt;
Eric Andersend5fa3e32000-08-02 16:42:58 +0000163 char follow=0;
164 int sleep_int=1;
165 int *fd;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000166
Eric Andersen98bbd682000-07-31 17:05:58 +0000167 opterr = 0;
168
Matt Kraai69229a62000-10-19 21:28:32 +0000169 while ((opt=getopt(argc,argv,"c:fhn:s:q:v")) >0) {
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000170
Eric Andersend5fa3e32000-08-02 16:42:58 +0000171 switch (opt) {
Eric Andersen98bbd682000-07-31 17:05:58 +0000172#ifndef BB_FEATURE_SIMPLE_TAIL
Eric Andersen98bbd682000-07-31 17:05:58 +0000173 case 'c':
174 unit_type = BYTES;
175 test = atoi(optarg);
176 if(test==0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 usage(tail_usage);
Eric Andersen98bbd682000-07-31 17:05:58 +0000178 if(optarg[strlen(optarg)-1]>'9') {
179 switch (optarg[strlen(optarg)-1]) {
180 case 'b':
181 test *= 512;
182 break;
183 case 'k':
184 test *= 1024;
185 break;
186 case 'm':
187 test *= (1024 * 1024);
188 break;
189 default:
190 fprintf(stderr,"Size must be b,k, or m.");
191 usage(tail_usage);
192 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 }
Eric Andersen98bbd682000-07-31 17:05:58 +0000194 if(optarg[0]=='+')
195 units=test+1;
196 else
197 units=-(test+1);
198 break;
199 case 'q':
200 show_headers = 0;
201 break;
202 case 's':
203 sleep_int = atoi(optarg);
204 if(sleep_int<1)
205 sleep_int=1;
206 break;
207 case 'v':
208 verbose = 1;
209 break;
210#endif
211 case 'f':
212 follow = 1;
213 break;
214 case 'h':
215 usage(tail_usage);
216 break;
217 case 'n':
218 test = atoi(optarg);
219 if (test) {
220 if (optarg[0] == '+')
221 units = test;
222 else
223 units = -(test+1);
224 } else
225 usage(tail_usage);
226 break;
227 default:
Eric Andersend5fa3e32000-08-02 16:42:58 +0000228 errorMsg("\nUnknown arg: %c.\n\n",optopt);
Eric Andersen98bbd682000-07-31 17:05:58 +0000229 usage(tail_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 }
Eric Andersen1792f8c1999-12-09 06:11:36 +0000231 }
Eric Andersen46ade972000-08-02 19:57:18 +0000232 while (optind <= argc) {
233 if(optind==argc) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000234 if (n_files==0)
235 add_file(STDIN);
236 else
237 break;
238 }else {
Matt Kraai69229a62000-10-19 21:28:32 +0000239 if (!strcmp(argv[optind], "-")) {
Eric Andersend5fa3e32000-08-02 16:42:58 +0000240 add_file(STDIN);
Eric Andersen46ade972000-08-02 19:57:18 +0000241 } else {
242 add_file(argv[optind]);
243 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000244 optind++;
245 }
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000246 }
Eric Andersen98bbd682000-07-31 17:05:58 +0000247 if(units==0)
248 units=-11;
249 if(units>0)
250 units--;
Matt Kraai322ae932000-09-13 02:46:14 +0000251 fd=xmalloc(sizeof(int)*n_files);
Eric Andersen98bbd682000-07-31 17:05:58 +0000252 if (n_files == 1)
253#ifndef BB_FEATURE_SIMPLE_TAIL
254 if (!verbose)
255#endif
256 show_headers = 0;
Matt Kraai322ae932000-09-13 02:46:14 +0000257 buffer=xmalloc(BUFSIZ);
Eric Andersen98bbd682000-07-31 17:05:58 +0000258 for (test = 0; test < n_files; test++) {
259 if (show_headers)
260 printf("==> %s <==\n", files[test]);
Eric Andersend5fa3e32000-08-02 16:42:58 +0000261 if (!strcmp(files[test], STDIN))
262 fd[test] = 0;
263 else
264 fd[test] = open(files[test], O_RDONLY);
265 if (fd[test] == -1)
266 fatalError("Unable to open file %s.\n", files[test]);
267 tail_stream(fd[test]);
268
269 bs=BUFSIZ;
270 while (1) {
271 if((filelocation>0 || pip)){
272 write(1,buffer,bytes_read);
273 }
274 bytes_read = read(fd[test], buffer, bs);
275 filelocation+=bytes_read;
276 if (bytes_read <= 0) {
277 break;
278 }
279 usleep(sleep_int * 1000);
280 }
281 if(n_files>1)
282 printf("\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 }
Eric Andersend5fa3e32000-08-02 16:42:58 +0000284 while(1){
285 for (test = 0; test < n_files; test++) {
286 if(!follow){
287 close(fd[test]);
288 continue;
289 } else {
290 sleep(sleep_int);
291 bytes_read = read(fd[test], buffer, bs);
292 if(bytes_read>0) {
293 if (show_headers)
294 printf("==> %s <==\n", files[test]);
295 write(1,buffer,bytes_read);
296 if(n_files>1)
297 printf("\n");
298 }
299 }
300 }
301 if(!follow)
302 break;
303 }
304 if (fd)
305 free(fd);
306 if (buffer)
307 free(buffer);
Eric Andersen98bbd682000-07-31 17:05:58 +0000308 if(files)
309 free(files);
Eric Andersen98bbd682000-07-31 17:05:58 +0000310 return 0;
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000311}
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000312
Eric Andersen98bbd682000-07-31 17:05:58 +0000313/*
314Local Variables:
315c-file-style: "linux"
316c-basic-offset: 4
317tab-width: 4
318End:
319*/