adduser: prefer to call addgroup --gid, not non-std addgroup -g
[platform/upstream/busybox.git] / coreutils / tail.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini tail implementation for busybox
4  *
5  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
11 /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
13
14 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
15  *
16  * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
17  * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
18  * 1) mixing printf/write without fflush()ing stdout
19  * 2) no check that any open files are present
20  * 3) optstring had -q taking an arg
21  * 4) no error checking on write in some cases, and a warning even then
22  * 5) q and s interaction bug
23  * 6) no check for lseek error
24  * 7) lseek attempted when count==0 even if arg was +0 (from top)
25  */
26
27 //usage:#define tail_trivial_usage
28 //usage:       "[OPTIONS] [FILE]..."
29 //usage:#define tail_full_usage "\n\n"
30 //usage:       "Print last 10 lines of each FILE (or stdin) to stdout.\n"
31 //usage:       "With more than one FILE, precede each with a filename header.\n"
32 //usage:     "\nOptions:"
33 //usage:     "\n        -f              Print data as file grows"
34 //usage:        IF_FEATURE_FANCY_TAIL(
35 //usage:     "\n        -s SECONDS      Wait SECONDS between reads with -f"
36 //usage:        )
37 //usage:     "\n        -n N[kbm]       Print last N lines"
38 //usage:        IF_FEATURE_FANCY_TAIL(
39 //usage:     "\n        -c N[kbm]       Print last N bytes"
40 //usage:     "\n        -q              Never print headers"
41 //usage:     "\n        -v              Always print headers"
42 //usage:     "\n"
43 //usage:     "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
44 //usage:     "\nIf N starts with a '+', output begins with the Nth item from the start"
45 //usage:     "\nof each file, not from the end."
46 //usage:        )
47 //usage:
48 //usage:#define tail_example_usage
49 //usage:       "$ tail -n 1 /etc/resolv.conf\n"
50 //usage:       "nameserver 10.0.0.1\n"
51
52 #include "libbb.h"
53
54 static const struct suffix_mult tail_suffixes[] = {
55         { "b", 512 },
56         { "k", 1024 },
57         { "m", 1024*1024 },
58         { "", 0 }
59 };
60
61 struct globals {
62         bool from_top;
63         bool exitcode;
64 } FIX_ALIASING;
65 #define G (*(struct globals*)&bb_common_bufsiz1)
66
67 static void tail_xprint_header(const char *fmt, const char *filename)
68 {
69         if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
70                 bb_perror_nomsg_and_die();
71 }
72
73 static ssize_t tail_read(int fd, char *buf, size_t count)
74 {
75         ssize_t r;
76         off_t current;
77         struct stat sbuf;
78
79         /* /proc files report zero st_size, don't lseek them. */
80         if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) {
81                 current = lseek(fd, 0, SEEK_CUR);
82                 if (sbuf.st_size < current)
83                         xlseek(fd, 0, SEEK_SET);
84         }
85
86         r = full_read(fd, buf, count);
87         if (r < 0) {
88                 bb_perror_msg(bb_msg_read_error);
89                 G.exitcode = EXIT_FAILURE;
90         }
91
92         return r;
93 }
94
95 #define header_fmt_str "\n==> %s <==\n"
96
97 static unsigned eat_num(const char *p)
98 {
99         if (*p == '-')
100                 p++;
101         else if (*p == '+') {
102                 p++;
103                 G.from_top = 1;
104         }
105         return xatou_sfx(p, tail_suffixes);
106 }
107
108 int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
109 int tail_main(int argc, char **argv)
110 {
111         unsigned count = 10;
112         unsigned sleep_period = 1;
113         const char *str_c, *str_n;
114
115         char *tailbuf;
116         size_t tailbufsize;
117         unsigned header_threshhold = 1;
118         unsigned nfiles;
119         int i, opt;
120
121         int *fds;
122         const char *fmt;
123
124 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
125         /* Allow legacy syntax of an initial numeric option without -n. */
126         if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
127          && isdigit(argv[1][1])
128         ) {
129                 count = eat_num(argv[1]);
130                 argv++;
131                 argc--;
132         }
133 #endif
134
135         /* -s NUM, -F imlies -f */
136         IF_FEATURE_FANCY_TAIL(opt_complementary = "s+:Ff";)
137         opt = getopt32(argv, "fc:n:" IF_FEATURE_FANCY_TAIL("qs:vF"),
138                         &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period));
139 #define FOLLOW (opt & 0x1)
140 #define COUNT_BYTES (opt & 0x2)
141         //if (opt & 0x1) // -f
142         if (opt & 0x2) count = eat_num(str_c); // -c
143         if (opt & 0x4) count = eat_num(str_n); // -n
144 #if ENABLE_FEATURE_FANCY_TAIL
145         /* q: make it impossible for nfiles to be > header_threshhold */
146         if (opt & 0x8) header_threshhold = UINT_MAX; // -q
147         //if (opt & 0x10) // -s
148         if (opt & 0x20) header_threshhold = 0; // -v
149 # define FOLLOW_RETRY (opt & 0x40)
150 #else
151 # define FOLLOW_RETRY 0
152 #endif
153         argc -= optind;
154         argv += optind;
155
156         /* open all the files */
157         fds = xmalloc(sizeof(fds[0]) * (argc + 1));
158         if (!argv[0]) {
159                 struct stat statbuf;
160
161                 if (fstat(STDIN_FILENO, &statbuf) == 0
162                  && S_ISFIFO(statbuf.st_mode)
163                 ) {
164                         opt &= ~1; /* clear FOLLOW */
165                 }
166                 argv[0] = (char *) bb_msg_standard_input;
167         }
168         nfiles = i = 0;
169         do {
170                 int fd = open_or_warn_stdin(argv[i]);
171                 if (fd < 0 && !FOLLOW_RETRY) {
172                         G.exitcode = EXIT_FAILURE;
173                         continue;
174                 }
175                 fds[nfiles] = fd;
176                 argv[nfiles++] = argv[i];
177         } while (++i < argc);
178
179         if (!nfiles)
180                 bb_error_msg_and_die("no files");
181
182         /* prepare the buffer */
183         tailbufsize = BUFSIZ;
184         if (!G.from_top && COUNT_BYTES) {
185                 if (tailbufsize < count + BUFSIZ) {
186                         tailbufsize = count + BUFSIZ;
187                 }
188         }
189         /* tail -c1024m REGULAR_FILE doesn't really need 1G mem block.
190          * (In fact, it doesn't need ANY memory). So delay allocation.
191          */
192         tailbuf = NULL;
193
194         /* tail the files */
195
196         fmt = header_fmt_str + 1; /* skip leading newline in the header on the first output */
197         i = 0;
198         do {
199                 char *buf;
200                 int taillen;
201                 int newlines_seen;
202                 unsigned seen;
203                 int nread;
204                 int fd = fds[i];
205
206                 if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
207                         continue; /* may happen with -E */
208
209                 if (nfiles > header_threshhold) {
210                         tail_xprint_header(fmt, argv[i]);
211                         fmt = header_fmt_str;
212                 }
213
214                 if (!G.from_top) {
215                         off_t current = lseek(fd, 0, SEEK_END);
216                         if (current > 0) {
217                                 unsigned off;
218                                 if (COUNT_BYTES) {
219                                 /* Optimizing count-bytes case if the file is seekable.
220                                  * Beware of backing up too far.
221                                  * Also we exclude files with size 0 (because of /proc/xxx) */
222                                         if (count == 0)
223                                                 continue; /* showing zero bytes is easy :) */
224                                         current -= count;
225                                         if (current < 0)
226                                                 current = 0;
227                                         xlseek(fd, current, SEEK_SET);
228                                         bb_copyfd_size(fd, STDOUT_FILENO, count);
229                                         continue;
230                                 }
231 #if 1 /* This is technically incorrect for *LONG* strings, but very useful */
232                                 /* Optimizing count-lines case if the file is seekable.
233                                  * We assume the lines are <64k.
234                                  * (Users complain that tail takes too long
235                                  * on multi-gigabyte files) */
236                                 off = (count | 0xf); /* for small counts, be more paranoid */
237                                 if (off > (INT_MAX / (64*1024)))
238                                         off = (INT_MAX / (64*1024));
239                                 current -= off * (64*1024);
240                                 if (current < 0)
241                                         current = 0;
242                                 xlseek(fd, current, SEEK_SET);
243 #endif
244                         }
245                 }
246
247                 if (!tailbuf)
248                         tailbuf = xmalloc(tailbufsize);
249
250                 buf = tailbuf;
251                 taillen = 0;
252                 /* "We saw 1st line/byte".
253                  * Used only by +N code ("start from Nth", 1-based): */
254                 seen = 1;
255                 newlines_seen = 0;
256                 while ((nread = tail_read(fd, buf, tailbufsize-taillen)) > 0) {
257                         if (G.from_top) {
258                                 int nwrite = nread;
259                                 if (seen < count) {
260                                         /* We need to skip a few more bytes/lines */
261                                         if (COUNT_BYTES) {
262                                                 nwrite -= (count - seen);
263                                                 seen = count;
264                                         } else {
265                                                 char *s = buf;
266                                                 do {
267                                                         --nwrite;
268                                                         if (*s++ == '\n' && ++seen == count) {
269                                                                 break;
270                                                         }
271                                                 } while (nwrite);
272                                         }
273                                 }
274                                 if (nwrite > 0)
275                                         xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
276                         } else if (count) {
277                                 if (COUNT_BYTES) {
278                                         taillen += nread;
279                                         if (taillen > (int)count) {
280                                                 memmove(tailbuf, tailbuf + taillen - count, count);
281                                                 taillen = count;
282                                         }
283                                 } else {
284                                         int k = nread;
285                                         int newlines_in_buf = 0;
286
287                                         do { /* count '\n' in last read */
288                                                 k--;
289                                                 if (buf[k] == '\n') {
290                                                         newlines_in_buf++;
291                                                 }
292                                         } while (k);
293
294                                         if (newlines_seen + newlines_in_buf < (int)count) {
295                                                 newlines_seen += newlines_in_buf;
296                                                 taillen += nread;
297                                         } else {
298                                                 int extra = (buf[nread-1] != '\n');
299                                                 char *s;
300
301                                                 k = newlines_seen + newlines_in_buf + extra - count;
302                                                 s = tailbuf;
303                                                 while (k) {
304                                                         if (*s == '\n') {
305                                                                 k--;
306                                                         }
307                                                         s++;
308                                                 }
309                                                 taillen += nread - (s - tailbuf);
310                                                 memmove(tailbuf, s, taillen);
311                                                 newlines_seen = count - extra;
312                                         }
313                                         if (tailbufsize < (size_t)taillen + BUFSIZ) {
314                                                 tailbufsize = taillen + BUFSIZ;
315                                                 tailbuf = xrealloc(tailbuf, tailbufsize);
316                                         }
317                                 }
318                                 buf = tailbuf + taillen;
319                         }
320                 } /* while (tail_read() > 0) */
321                 if (!G.from_top) {
322                         xwrite(STDOUT_FILENO, tailbuf, taillen);
323                 }
324         } while (++i < nfiles);
325
326         tailbuf = xrealloc(tailbuf, BUFSIZ);
327
328         fmt = NULL;
329
330         if (FOLLOW) while (1) {
331                 sleep(sleep_period);
332
333                 i = 0;
334                 do {
335                         int nread;
336                         const char *filename = argv[i];
337                         int fd = fds[i];
338
339                         if (FOLLOW_RETRY) {
340                                 struct stat sbuf, fsbuf;
341
342                                 if (fd < 0
343                                  || fstat(fd, &fsbuf) < 0
344                                  || stat(filename, &sbuf) < 0
345                                  || fsbuf.st_dev != sbuf.st_dev
346                                  || fsbuf.st_ino != sbuf.st_ino
347                                 ) {
348                                         int new_fd;
349
350                                         if (fd >= 0)
351                                                 close(fd);
352                                         new_fd = open(filename, O_RDONLY);
353                                         if (new_fd >= 0) {
354                                                 bb_error_msg("%s has %s; following end of new file",
355                                                         filename, (fd < 0) ? "appeared" : "been replaced"
356                                                 );
357                                         } else if (fd >= 0) {
358                                                 bb_perror_msg("%s has become inaccessible", filename);
359                                         }
360                                         fds[i] = fd = new_fd;
361                                 }
362                         }
363                         if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
364                                 continue;
365                         if (nfiles > header_threshhold) {
366                                 fmt = header_fmt_str;
367                         }
368                         while ((nread = tail_read(fd, tailbuf, BUFSIZ)) > 0) {
369                                 if (fmt) {
370                                         tail_xprint_header(fmt, filename);
371                                         fmt = NULL;
372                                 }
373                                 xwrite(STDOUT_FILENO, tailbuf, nread);
374                         }
375                 } while (++i < nfiles);
376         } /* while (1) */
377
378         if (ENABLE_FEATURE_CLEAN_UP) {
379                 free(fds);
380                 free(tailbuf);
381         }
382         return G.exitcode;
383 }