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