Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 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 | * |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 22 | */ |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 23 | |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 24 | |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <getopt.h> |
| 27 | #include <string.h> |
Robert Griebl | 53146cc | 2002-05-27 22:24:53 +0000 | [diff] [blame^] | 28 | #include <ctype.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <unistd.h> |
| 31 | #include <sys/types.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 33 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 34 | static const struct suffix_mult tail_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 35 | { "b", 512 }, |
| 36 | { "k", 1024 }, |
| 37 | { "m", 1048576 }, |
| 38 | { NULL, 0 } |
| 39 | }; |
| 40 | |
Mark Whitley | eb60d8b | 2001-01-05 18:19:30 +0000 | [diff] [blame] | 41 | static const int BYTES = 0; |
| 42 | static const int LINES = 1; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 43 | |
| 44 | static char *tailbuf; |
| 45 | static int taillen; |
| 46 | static int newline; |
| 47 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 48 | static void tailbuf_append(char *buf, int len) |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 49 | { |
| 50 | tailbuf = xrealloc(tailbuf, taillen + len); |
| 51 | memcpy(tailbuf + taillen, buf, len); |
| 52 | taillen += len; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 55 | static void tailbuf_trunc(void) |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 56 | { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 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; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Eric Andersen | 98bbd68 | 2000-07-31 17:05:58 +0000 | [diff] [blame] | 64 | int tail_main(int argc, char **argv) |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 65 | { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 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; |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 71 | |
Robert Griebl | 53146cc | 2002-05-27 22:24:53 +0000 | [diff] [blame^] | 72 | if (( argc >= 2 ) && ( argv [1][0] == '-' ) && isdigit ( argv [1][1] )) { |
Robert Griebl | 13c26fc | 2002-05-17 22:18:04 +0000 | [diff] [blame] | 73 | count = atoi ( &argv [1][1] ); |
| 74 | optind = 2; |
| 75 | } |
| 76 | |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 77 | while ((opt = getopt(argc, argv, "c:fhn:q:s:v")) > 0) { |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 78 | switch (opt) { |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 79 | case 'f': |
| 80 | follow = 1; |
| 81 | break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 82 | #ifdef CONFIG_FEATURE_FANCY_TAIL |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 83 | case 'c': |
| 84 | units = BYTES; |
| 85 | /* FALLS THROUGH */ |
Eric Andersen | 98bbd68 | 2000-07-31 17:05:58 +0000 | [diff] [blame] | 86 | #endif |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 87 | case 'n': |
| 88 | count = parse_number(optarg, tail_suffixes); |
| 89 | if (count < 0) |
| 90 | count = -count; |
Eric Andersen | 98bbd68 | 2000-07-31 17:05:58 +0000 | [diff] [blame] | 91 | if (optarg[0] == '+') |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 92 | from_top = 1; |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 93 | break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 94 | #ifdef CONFIG_FEATURE_FANCY_TAIL |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 95 | case 'q': |
| 96 | hide_headers = 1; |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 97 | break; |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 98 | case 's': |
Eric Andersen | 963791a | 2001-02-18 20:13:18 +0000 | [diff] [blame] | 99 | sleep_period = parse_number(optarg, 0); |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 100 | break; |
| 101 | case 'v': |
| 102 | show_headers = 1; |
| 103 | break; |
| 104 | #endif |
| 105 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 106 | show_usage(); |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 107 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | } |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 109 | |
| 110 | /* open all the files */ |
| 111 | fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1)); |
| 112 | if (argc == optind) { |
| 113 | fds[nfiles++] = STDIN_FILENO; |
| 114 | argv[optind] = "standard input"; |
| 115 | } else { |
| 116 | for (i = optind; i < argc; i++) { |
| 117 | if (strcmp(argv[i], "-") == 0) { |
| 118 | fds[nfiles++] = STDIN_FILENO; |
| 119 | argv[i] = "standard input"; |
| 120 | } else if ((fds[nfiles++] = open(argv[i], O_RDONLY)) < 0) { |
| 121 | perror_msg("%s", argv[i]); |
| 122 | status = EXIT_FAILURE; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 127 | #ifdef CONFIG_FEATURE_FANCY_TAIL |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 128 | /* tail the files */ |
| 129 | if (!from_top && units == BYTES) |
| 130 | tailbuf = xmalloc(count); |
| 131 | #endif |
| 132 | |
| 133 | for (i = 0; i < nfiles; i++) { |
| 134 | if (fds[i] == -1) |
| 135 | continue; |
Eric Andersen | ce98c19 | 2001-06-26 15:07:08 +0000 | [diff] [blame] | 136 | if (!count) { |
| 137 | lseek(fds[i], 0, SEEK_END); |
| 138 | continue; |
| 139 | } |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 140 | seen = 0; |
| 141 | if (show_headers || (!hide_headers && nfiles > 1)) |
| 142 | printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]); |
| 143 | while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) { |
| 144 | if (from_top) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 145 | #ifdef CONFIG_FEATURE_FANCY_TAIL |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 146 | if (units == BYTES) { |
| 147 | if (count - 1 <= seen) |
| 148 | nwrite = nread; |
| 149 | else if (count - 1 <= seen + nread) |
| 150 | nwrite = nread + seen - (count - 1); |
| 151 | else |
| 152 | nwrite = 0; |
| 153 | seen += nread; |
| 154 | } else { |
| 155 | #else |
| 156 | { |
| 157 | #endif |
| 158 | if (count - 1 <= seen) |
| 159 | nwrite = nread; |
| 160 | else { |
| 161 | nwrite = 0; |
| 162 | for (s = memchr(buf, '\n', nread); s != NULL; |
| 163 | s = memchr(s+1, '\n', nread - (s + 1 - buf))) { |
| 164 | if (count - 1 <= ++seen) { |
| 165 | nwrite = nread - (s + 1 - buf); |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | if (full_write(STDOUT_FILENO, buf + nread - nwrite, |
| 172 | nwrite) < 0) { |
| 173 | perror_msg("write"); |
| 174 | status = EXIT_FAILURE; |
| 175 | break; |
| 176 | } |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 177 | } else { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 178 | #ifdef CONFIG_FEATURE_FANCY_TAIL |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 179 | if (units == BYTES) { |
| 180 | if (nread < count) { |
| 181 | memmove(tailbuf, tailbuf + nread, count - nread); |
| 182 | memcpy(tailbuf + count - nread, buf, nread); |
| 183 | } else { |
| 184 | memcpy(tailbuf, buf + nread - count, count); |
| 185 | } |
| 186 | seen += nread; |
| 187 | } else { |
| 188 | #else |
| 189 | { |
| 190 | #endif |
| 191 | for (start = buf, end = memchr(buf, '\n', nread); |
| 192 | end != NULL; start = end+1, |
| 193 | end = memchr(start, '\n', nread - (start - buf))) { |
| 194 | if (newline && count <= seen) |
| 195 | tailbuf_trunc(); |
| 196 | tailbuf_append(start, end - start + 1); |
| 197 | seen++; |
| 198 | newline = 1; |
| 199 | } |
| 200 | if (newline && count <= seen && nread - (start - buf) > 0) |
| 201 | tailbuf_trunc(); |
| 202 | tailbuf_append(start, nread - (start - buf)); |
Eric Andersen | d5fa3e3 | 2000-08-02 16:42:58 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 206 | |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 207 | if (nread < 0) { |
| 208 | perror_msg("read"); |
| 209 | status = EXIT_FAILURE; |
| 210 | } |
| 211 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 212 | #ifdef CONFIG_FEATURE_FANCY_TAIL |
Matt Kraai | 55bccf3 | 2001-01-05 02:57:53 +0000 | [diff] [blame] | 213 | if (!from_top && units == BYTES) { |
| 214 | if (count < seen) |
| 215 | seen = count; |
| 216 | if (full_write(STDOUT_FILENO, tailbuf + count - seen, seen) < 0) { |
| 217 | perror_msg("write"); |
| 218 | status = EXIT_FAILURE; |
| 219 | } |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | if (!from_top && units == LINES) { |
| 224 | if (full_write(STDOUT_FILENO, tailbuf, taillen) < 0) { |
| 225 | perror_msg("write"); |
| 226 | status = EXIT_FAILURE; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | taillen = 0; |
| 231 | } |
| 232 | |
| 233 | while (follow) { |
| 234 | sleep(sleep_period); |
| 235 | |
| 236 | for (i = 0; i < nfiles; i++) { |
| 237 | if (fds[i] == -1) |
| 238 | continue; |
| 239 | |
| 240 | if ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) { |
| 241 | if (show_headers || (!hide_headers && nfiles > 1)) |
| 242 | printf("\n==> %s <==\n", argv[optind + i]); |
| 243 | |
| 244 | do { |
| 245 | full_write(STDOUT_FILENO, buf, nread); |
| 246 | } while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0); |
| 247 | } |
| 248 | |
| 249 | if (nread < 0) { |
| 250 | perror_msg("read"); |
| 251 | status = EXIT_FAILURE; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return status; |
| 257 | } |