156f6368b3943f6ee0df55c0ab7b1fbf00ecf6d0
[oweals/busybox.git] / coreutils / tail.c
1 /* vi: set sw=4 ts=4: */
2 /* 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,
24    and generally busyboxed, Erik Andersen <andersen@lineo.com>
25
26    Removed superfluous options and associated code ("-c", "-n", "-q").
27    Removed "tail -f" support for multiple files.
28    Both changes by Friedrich Vedder <fwv@myrtle.lahn.de>.
29
30    Compleate Rewrite to correctly support "-NUM", "+NUM", and "-s" by
31    E.Allen Soard (esp@espsw.net).
32
33  */
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <getopt.h>
42 #include "internal.h"
43
44 #define STDIN "standard input"
45 #define LINES 0
46 #define BYTES 1
47
48 static int n_files = 0;
49 static char **files = NULL;
50 static char * buffer;
51 static ssize_t bytes_read=0;
52 static ssize_t bs;
53 static ssize_t filelocation=0;
54 static char pip;
55
56 #ifdef BB_FEATURE_SIMPLE_TAIL
57 static const char unit_type=LINES;
58 #else
59 static char unit_type=LINES;
60 static char verbose = 0;
61 #endif
62
63 static off_t units=0;
64
65 int tail_stream(int fd)
66 {
67         ssize_t startpoint;
68         ssize_t endpoint=0;
69         ssize_t count=0;
70         ssize_t filesize=0;
71         char direction=1;
72
73         filelocation=0;
74         startpoint=bs=BUFSIZ;
75
76         filesize=lseek(fd, -1, SEEK_END)+1;
77         pip=(filesize<=0);
78
79         if(units>=0)
80                 lseek(fd,0,SEEK_SET);
81         else {
82                 direction=-1;
83                 count=1;
84         }
85         while(units != 0) {
86                 if (pip) {
87                         char * line;
88                         ssize_t f_size=0;
89
90                         bs=BUFSIZ;
91                         line=malloc(bs);
92                         while(1) {
93                                 bytes_read=read(fd,line,bs);
94                                 if(bytes_read<=0)
95                                         break;
96                                 buffer=realloc(buffer,f_size+bytes_read);
97                                 memcpy(&buffer[f_size],line,bytes_read);
98                                 filelocation=f_size+=bytes_read;
99                         }
100                         bs=f_size;
101                         if(direction<0)
102                                 bs--;
103                         if (line)
104                                 free(line);
105                 } else {
106                         filelocation = lseek(fd, 0, SEEK_CUR);
107                         if(direction<0) {
108                                 if(filelocation<bs)
109                                         bs=filelocation;
110                                 filelocation = lseek(fd, -bs, SEEK_CUR);
111                         }
112                         bytes_read = read(fd, buffer, bs);
113                         if (bytes_read <= 0)
114                                 break;
115                         bs=bytes_read;
116                 }
117                 startpoint=bs;
118                 if(direction>0) {
119                         endpoint=startpoint;
120                         startpoint=0;
121                 }
122                 for(;startpoint!=endpoint;startpoint+=direction) {
123 #ifndef BB_FEATURE_SIMPLE_TAIL
124                         if(unit_type==BYTES)
125                                 count++;
126                         else
127 #endif
128                                 if(buffer[startpoint-1]=='\n')
129                                         count++;
130                         if (!pip)
131                                 filelocation=lseek(fd,0,SEEK_CUR);
132                         if(count==units*direction)
133                                 break;
134                 }
135                 if((count==units*direction) | pip)
136                         break;
137                 if(direction<0){
138                         filelocation = lseek(fd, -bytes_read, SEEK_CUR);
139                         if(filelocation==0)
140                                 break;
141                 }
142         }
143         if(pip && (direction<0))
144                 bs++;
145         bytes_read=bs-startpoint;
146         memcpy(&buffer[0],&buffer[startpoint],bytes_read);
147
148         return 0;
149 }
150
151 void add_file(char *name)
152 {
153         ++n_files;
154         files = realloc(files, n_files);
155         files[n_files - 1] = (char *) malloc(strlen(name) + 1);
156         strcpy(files[n_files - 1], name);
157 }
158
159
160 int tail_main(int argc, char **argv)
161 {
162         int show_headers = 1;
163         int test;
164         int opt;
165         int optc=0;
166         char **optv=NULL;
167         char follow=0;
168         int sleep_int=1;
169         int *fd;
170
171         opterr = 0;
172         
173         for(opt=0;opt<argc;opt++){
174                 test=atoi(argv[opt]);
175                 if(test){
176                         units=test;
177                         if(units<0)
178                                 units=units-1;
179                 }else{
180                         optc++;
181                         optv = realloc(optv, optc);
182                         optv[optc - 1] = (char *) malloc(strlen(argv[opt]) + 1);
183                         strcpy(optv[optc - 1], argv[opt]);
184                 }
185         }
186         while ((opt=getopt(optc,optv,"c:fhn:s:q:v")) >0) {
187
188                 switch (opt) {
189
190 #ifndef BB_FEATURE_SIMPLE_TAIL
191
192                 case 'c':
193                         unit_type = BYTES;
194                         test = atoi(optarg);
195                         if(test==0)
196                                 usage(tail_usage);
197                         if(optarg[strlen(optarg)-1]>'9') {
198                                 switch (optarg[strlen(optarg)-1]) {
199                                 case 'b':
200                                         test *= 512;
201                                         break;
202                                 case 'k':
203                                         test *= 1024;
204                                         break;
205                                 case 'm':
206                                         test *= (1024 * 1024);
207                                         break;
208                                 default:
209                                         fprintf(stderr,"Size must be b,k, or m.");
210                                         usage(tail_usage);
211                                 }
212                         }
213                         if(optarg[0]=='+')
214                                 units=test+1;
215                         else
216                                 units=-(test+1);
217                         break;
218                 case 'q':
219                         show_headers = 0;
220                         break;
221                 case 's':
222                         sleep_int = atoi(optarg);
223                         if(sleep_int<1)
224                                 sleep_int=1;
225                         break;
226                 case 'v':
227                         verbose = 1;
228                         break;
229 #endif
230                 case 'f':
231                         follow = 1;
232                         break;
233                 case 'h':
234                         usage(tail_usage);
235                         break;
236                 case 'n':
237                         test = atoi(optarg);
238                         if (test) {
239                                 if (optarg[0] == '+')
240                                         units = test;
241                                 else
242                                         units = -(test+1);
243                         } else
244                                 usage(tail_usage);
245                         break;
246                 default:
247                         errorMsg("\nUnknown arg: %c.\n\n",optopt);
248                         usage(tail_usage);
249                 }
250         }
251         while (optind <= optc) {
252                 if(optind==optc) {
253                         if (n_files==0)
254                                 add_file(STDIN);
255                         else
256                                 break;
257                 }else {
258                         if (!strcmp(optv[optind], "-"))
259                                 add_file(STDIN);
260                         else
261                                 add_file(optv[optind]);
262                         optind++;
263                 }
264         }
265         if(units==0)
266                 units=-11;
267         if(units>0)
268                 units--;
269         fd=malloc(sizeof(int)*n_files);
270         if (n_files == 1)
271 #ifndef BB_FEATURE_SIMPLE_TAIL
272                 if (!verbose)
273 #endif
274                         show_headers = 0;
275         buffer=malloc(BUFSIZ);
276         for (test = 0; test < n_files; test++) {
277                 if (show_headers)
278                         printf("==> %s <==\n", files[test]);
279                 if (!strcmp(files[test], STDIN))
280                         fd[test] = 0;
281                 else
282                         fd[test] = open(files[test], O_RDONLY);
283                 if (fd[test] == -1)
284                         fatalError("Unable to open file %s.\n", files[test]);
285                 tail_stream(fd[test]);
286
287                 bs=BUFSIZ;
288                 while (1) {
289                         if((filelocation>0 || pip)){
290                                 write(1,buffer,bytes_read);
291                         }
292                         bytes_read = read(fd[test], buffer, bs);
293                         filelocation+=bytes_read;
294                         if (bytes_read <= 0) {
295                                 break;
296                         }
297                         usleep(sleep_int * 1000);
298                 }
299                 if(n_files>1)
300                         printf("\n");
301         }
302         while(1){
303                 for (test = 0; test < n_files; test++) {
304                         if(!follow){
305                                 close(fd[test]);
306                                 continue;
307                         } else {
308                                 sleep(sleep_int);
309                                 bytes_read = read(fd[test], buffer, bs);
310                                 if(bytes_read>0) {
311                                         if (show_headers)
312                                                 printf("==> %s <==\n", files[test]);
313                                         write(1,buffer,bytes_read);
314                                         if(n_files>1)
315                                                 printf("\n");
316                                 }
317                         }
318                 }
319                 if(!follow)
320                         break;
321         }
322         if (fd)
323                 free(fd);
324         if (buffer)
325                 free(buffer);
326         if(files)
327                 free(files);
328         if(optv)
329                 free(optv);
330         return 0;
331 }
332
333 /*
334 Local Variables:
335 c-file-style: "linux"
336 c-basic-offset: 4
337 tab-width: 4
338 End:
339 */