Patch for the debian-cvs package, ip* applets in /bin, patch from Bastian Blank
[oweals/busybox.git] / coreutils / tail.c
1 /* vi: set sw=4 ts=4: */
2 /*
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  *
22  */
23
24
25 #include <fcntl.h>
26 #include <getopt.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include "busybox.h"
33
34 static const struct suffix_mult tail_suffixes[] = {
35         { "b", 512 },
36         { "k", 1024 },
37         { "m", 1048576 },
38         { NULL, 0 }
39 };
40
41 static const int BYTES = 0;
42 static const int LINES = 1;
43
44 static char *tailbuf;
45 static int taillen;
46 static int newline;
47
48 static void tailbuf_append(char *buf, int len)
49 {
50         tailbuf = xrealloc(tailbuf, taillen + len);
51         memcpy(tailbuf + taillen, buf, len);
52         taillen += len;
53 }
54
55 static void tailbuf_trunc(void)
56 {
57         char *s;
58         s = memchr(tailbuf, '\n', taillen);
59         memmove(tailbuf, s + 1, taillen - ((s + 1) - tailbuf));
60         taillen -= (s + 1) - tailbuf;
61         newline = 0;
62 }
63
64 int tail_main(int argc, char **argv)
65 {
66         int from_top = 0, units = LINES, count = 10, sleep_period = 1;
67         int show_headers = 0, hide_headers = 0, follow = 0;
68         int *fds, nfiles = 0, status = EXIT_SUCCESS, nread, nwrite, seen = 0;
69         char *s, *start, *end, buf[BUFSIZ];
70         int i, opt;
71
72         if (argc >= 2) {
73                 int line_num;
74                 switch (argv[1][0]) {
75                         case '+':
76                                 from_top = 1;
77                                 /* FALLS THROUGH */
78                         case '-':
79                                 line_num = atoi(&argv[1][1]);
80                                 if (line_num != 0) {
81                                         optind = 2;
82                                         count = line_num;
83                                 }
84                                 break;
85                 }
86         }
87
88 #ifdef CONFIG_FEATURE_FANCY_TAIL
89         while ((opt = getopt(argc, argv, "c:fn:q:s:v")) > 0) {
90 #else
91         while ((opt = getopt(argc, argv, "fn:")) > 0) {
92 #endif
93                 switch (opt) {
94                         case 'f':
95                                 follow = 1;
96                                 break;
97 #ifdef CONFIG_FEATURE_FANCY_TAIL
98                         case 'c':
99                                 units = BYTES;
100                                 /* FALLS THROUGH */
101 #endif
102                         case 'n':
103                                 count = parse_number(optarg, tail_suffixes);
104                                 if (count < 0)
105                                         count = -count;
106                                 if (optarg[0] == '+')
107                                         from_top = 1;
108                                 break;
109 #ifdef CONFIG_FEATURE_FANCY_TAIL
110                         case 'q':
111                                 hide_headers = 1;
112                                 break;
113                         case 's':
114                                 sleep_period = parse_number(optarg, 0);
115                                 break;
116                         case 'v':
117                                 show_headers = 1;
118                                 break;
119 #endif
120                         default:
121                                 show_usage();
122                 }
123         }
124
125         /* open all the files */
126         fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
127         if (argc == optind) {
128                 fds[nfiles++] = STDIN_FILENO;
129                 argv[optind] = "standard input";
130         } else {
131                 for (i = optind; i < argc; i++) {
132                         if (strcmp(argv[i], "-") == 0) {
133                                 fds[nfiles++] = STDIN_FILENO;
134                                 argv[i] = "standard input";
135                         } else if ((fds[nfiles++] = open(argv[i], O_RDONLY)) < 0) {
136                                 perror_msg("%s", argv[i]);
137                                 status = EXIT_FAILURE;
138                         }
139                 }
140         }
141         
142 #ifdef CONFIG_FEATURE_FANCY_TAIL
143         /* tail the files */
144         if (!from_top && units == BYTES)
145                 tailbuf = xmalloc(count);
146 #endif
147
148         for (i = 0; i < nfiles; i++) {
149                 if (fds[i] == -1)
150                         continue;
151                 if (!count) {
152                         lseek(fds[i], 0, SEEK_END);
153                         continue;
154                 }
155                 seen = 0;
156                 if (show_headers || (!hide_headers && nfiles > 1))
157                         printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]);
158                 while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
159                         if (from_top) {
160 #ifdef CONFIG_FEATURE_FANCY_TAIL
161                                 if (units == BYTES) {
162                                         if (count - 1 <= seen)
163                                                 nwrite = nread;
164                                         else if (count - 1 <= seen + nread)
165                                                 nwrite = nread + seen - (count - 1);
166                                         else
167                                                 nwrite = 0;
168                                         seen += nread;
169                                 } else {
170 #else
171                                 {
172 #endif
173                                         if (count - 1 <= seen)
174                                                 nwrite = nread;
175                                         else {
176                                                 nwrite = 0;
177                                                 for (s = memchr(buf, '\n', nread); s != NULL;
178                                                                 s = memchr(s+1, '\n', nread - (s + 1 - buf))) {
179                                                         if (count - 1 <= ++seen) {
180                                                                 nwrite = nread - (s + 1 - buf);
181                                                                 break;
182                                                         }
183                                                 }
184                                         }
185                                 }
186                                 if (full_write(STDOUT_FILENO, buf + nread - nwrite,
187                                                         nwrite) < 0) {
188                                         perror_msg("write");
189                                         status = EXIT_FAILURE;
190                                         break;
191                                 }
192                         } else {
193 #ifdef CONFIG_FEATURE_FANCY_TAIL
194                                 if (units == BYTES) {
195                                         if (nread < count) {
196                                                 memmove(tailbuf, tailbuf + nread, count - nread);
197                                                 memcpy(tailbuf + count - nread, buf, nread);
198                                         } else {
199                                                 memcpy(tailbuf, buf + nread - count, count);
200                                         }
201                                         seen += nread;
202                                 } else {
203 #else
204                                 {
205 #endif
206                                         for (start = buf, end = memchr(buf, '\n', nread);
207                                                         end != NULL; start = end+1,
208                                                         end = memchr(start, '\n', nread - (start - buf))) {
209                                                 if (newline && count <= seen)
210                                                         tailbuf_trunc();
211                                                 tailbuf_append(start, end - start + 1);
212                                                 seen++;
213                                                 newline = 1;
214                                         }
215                                         if (newline && count <= seen && nread - (start - buf) > 0)
216                                                 tailbuf_trunc();
217                                         tailbuf_append(start, nread - (start - buf));
218                                 }
219                         }
220                 }
221
222                 if (nread < 0) {
223                         perror_msg("read");
224                         status = EXIT_FAILURE;
225                 }
226
227 #ifdef CONFIG_FEATURE_FANCY_TAIL
228                 if (!from_top && units == BYTES) {
229                         if (count < seen)
230                                 seen = count;
231                         if (full_write(STDOUT_FILENO, tailbuf + count - seen, seen) < 0) {
232                                 perror_msg("write");
233                                 status = EXIT_FAILURE;
234                         }
235                 }
236 #endif
237
238                 if (!from_top && units == LINES) {
239                         if (full_write(STDOUT_FILENO, tailbuf, taillen) < 0) {
240                                 perror_msg("write");
241                                 status = EXIT_FAILURE;
242                         }
243                 }
244
245                 taillen = 0;
246         }
247
248         while (follow) {
249                 sleep(sleep_period);
250
251                 for (i = 0; i < nfiles; i++) {
252                         if (fds[i] == -1)
253                                 continue;
254
255                         if ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
256                                 if (show_headers || (!hide_headers && nfiles > 1))
257                                         printf("\n==> %s <==\n", argv[optind + i]);
258
259                                 do {
260                                         full_write(STDOUT_FILENO, buf, nread);
261                                 } while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0);
262                         }
263
264                         if (nread < 0) {
265                                 perror_msg("read");
266                                 status = EXIT_FAILURE;
267                         }
268                 }
269         }
270
271         return status;
272 }