tail.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
  23. /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
  24. /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
  25. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  26. *
  27. * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
  28. * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
  29. * 1) mixing printf/write without fflush()ing stdout
  30. * 2) no check that any open files are present
  31. * 3) optstring had -q taking an arg
  32. * 4) no error checking on write in some cases, and a warning even then
  33. * 5) q and s interaction bug
  34. * 6) no check for lseek error
  35. * 7) lseek attempted when count==0 even if arg was +0 (from top)
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <sys/stat.h>
  44. #include "busybox.h"
  45. static const struct suffix_mult tail_suffixes[] = {
  46. { "b", 512 },
  47. { "k", 1024 },
  48. { "m", 1048576 },
  49. { NULL, 0 }
  50. };
  51. static int status
  52. #if EXIT_SUCCESS != 0
  53. = EXIT_SUCCESS /* If it is 0 (paranoid check), let bss initialize it. */
  54. #endif
  55. ;
  56. static void tail_xprint_header(const char *fmt, const char *filename)
  57. {
  58. /* If we get an output error, there is really no sense in continuing. */
  59. if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
  60. bb_perror_nomsg_and_die();
  61. }
  62. }
  63. /* len should probably be size_t */
  64. static void tail_xbb_full_write(const char *buf, size_t len)
  65. {
  66. /* If we get a write error, there is really no sense in continuing. */
  67. if (bb_full_write(STDOUT_FILENO, buf, len) < 0) {
  68. bb_perror_nomsg_and_die();
  69. }
  70. }
  71. static ssize_t tail_read(int fd, char *buf, size_t count)
  72. {
  73. ssize_t r;
  74. if ((r = safe_read(fd, buf, count)) < 0) {
  75. bb_perror_msg("read");
  76. status = EXIT_FAILURE;
  77. }
  78. return r;
  79. }
  80. static const char tail_opts[] =
  81. "fn:c:"
  82. #ifdef CONFIG_FEATURE_FANCY_TAIL
  83. "qs:v"
  84. #endif
  85. ;
  86. static const char header_fmt[] = "\n==> %s <==\n";
  87. int tail_main(int argc, char **argv)
  88. {
  89. long count = 10;
  90. unsigned int sleep_period = 1;
  91. int from_top = 0;
  92. int follow = 0;
  93. int header_threshhold = 1;
  94. int count_bytes = 0;
  95. char *tailbuf;
  96. size_t tailbufsize;
  97. int taillen = 0;
  98. int newline = 0;
  99. int *fds, nfiles, nread, nwrite, seen, i, opt;
  100. char *s, *buf;
  101. const char *fmt;
  102. /* Allow legacy syntax of an initial numeric option without -n. */
  103. if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-')
  104. /* && (isdigit)(argv[1][1]) */
  105. && (((unsigned int)(argv[1][1] - '0')) <= 9))))
  106. {
  107. optind = 2;
  108. optarg = argv[1];
  109. goto GET_COUNT;
  110. }
  111. while ((opt = getopt(argc, argv, tail_opts)) > 0) {
  112. switch (opt) {
  113. case 'f':
  114. follow = 1;
  115. break;
  116. case 'c':
  117. count_bytes = 1;
  118. /* FALLS THROUGH */
  119. case 'n':
  120. GET_COUNT:
  121. count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
  122. /* Note: Leading whitespace is an error trapped above. */
  123. if (*optarg == '+') {
  124. from_top = 1;
  125. } else {
  126. from_top = 0;
  127. }
  128. if (count < 0) {
  129. count = -count;
  130. }
  131. break;
  132. #ifdef CONFIG_FEATURE_FANCY_TAIL
  133. case 'q':
  134. header_threshhold = INT_MAX;
  135. break;
  136. case 's':
  137. sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
  138. break;
  139. case 'v':
  140. header_threshhold = 0;
  141. break;
  142. #endif
  143. default:
  144. bb_show_usage();
  145. }
  146. }
  147. /* open all the files */
  148. fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
  149. argv += optind;
  150. nfiles = i = 0;
  151. if ((argc -= optind) == 0) {
  152. struct stat statbuf;
  153. if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
  154. follow = 0;
  155. }
  156. /* --argv; */
  157. *argv = (char *) bb_msg_standard_input;
  158. goto DO_STDIN;
  159. }
  160. do {
  161. if ((argv[i][0] == '-') && !argv[i][1]) {
  162. DO_STDIN:
  163. fds[nfiles] = STDIN_FILENO;
  164. } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
  165. bb_perror_msg("%s", argv[i]);
  166. status = EXIT_FAILURE;
  167. continue;
  168. }
  169. argv[nfiles] = argv[i];
  170. ++nfiles;
  171. } while (++i < argc);
  172. if (!nfiles) {
  173. bb_error_msg_and_die("no files");
  174. }
  175. tailbufsize = BUFSIZ;
  176. /* tail the files */
  177. if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
  178. /* Hence, !from_top && count_bytes */
  179. if (tailbufsize < count) {
  180. tailbufsize = count + BUFSIZ;
  181. }
  182. }
  183. buf = tailbuf = xmalloc(tailbufsize);
  184. fmt = header_fmt + 1; /* Skip header leading newline on first output. */
  185. i = 0;
  186. do {
  187. /* Be careful. It would be possible to optimize the count-bytes
  188. * case if the file is seekable. If you do though, remember that
  189. * starting file position may not be the beginning of the file.
  190. * Beware of backing up too far. See example in wc.c.
  191. */
  192. if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
  193. continue;
  194. }
  195. if (nfiles > header_threshhold) {
  196. tail_xprint_header(fmt, argv[i]);
  197. fmt = header_fmt;
  198. }
  199. buf = tailbuf;
  200. taillen = 0;
  201. seen = 1;
  202. newline = 0;
  203. while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
  204. if (from_top) {
  205. nwrite = nread;
  206. if (seen < count) {
  207. if (count_bytes) {
  208. nwrite -= (count - seen);
  209. seen = count;
  210. } else {
  211. s = buf;
  212. do {
  213. --nwrite;
  214. if ((*s++ == '\n') && (++seen == count)) {
  215. break;
  216. }
  217. } while (nwrite);
  218. }
  219. }
  220. tail_xbb_full_write(buf + nread - nwrite, nwrite);
  221. } else if (count) {
  222. if (count_bytes) {
  223. taillen += nread;
  224. if (taillen > count) {
  225. memmove(tailbuf, tailbuf + taillen - count, count);
  226. taillen = count;
  227. }
  228. } else {
  229. int k = nread;
  230. int nbuf = 0;
  231. while (k) {
  232. --k;
  233. if (buf[k] == '\n') {
  234. ++nbuf;
  235. }
  236. }
  237. if (newline + nbuf < count) {
  238. newline += nbuf;
  239. taillen += nread;
  240. } else {
  241. int extra = 0;
  242. if (buf[nread-1] != '\n') {
  243. extra = 1;
  244. }
  245. k = newline + nbuf + extra - count;
  246. s = tailbuf;
  247. while (k) {
  248. if (*s == '\n') {
  249. --k;
  250. }
  251. ++s;
  252. }
  253. taillen += nread - (s - tailbuf);
  254. memmove(tailbuf, s, taillen);
  255. newline = count - extra;
  256. }
  257. if (tailbufsize < taillen + BUFSIZ) {
  258. tailbufsize = taillen + BUFSIZ;
  259. tailbuf = xrealloc(tailbuf, tailbufsize);
  260. }
  261. }
  262. buf = tailbuf + taillen;
  263. }
  264. }
  265. if (!from_top) {
  266. tail_xbb_full_write(tailbuf, taillen);
  267. }
  268. taillen = 0;
  269. } while (++i < nfiles);
  270. buf = xrealloc(tailbuf, BUFSIZ);
  271. fmt = NULL;
  272. while (follow) {
  273. sleep(sleep_period);
  274. i = 0;
  275. do {
  276. if (nfiles > header_threshhold) {
  277. fmt = header_fmt;
  278. }
  279. while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
  280. if (fmt) {
  281. tail_xprint_header(fmt, argv[i]);
  282. fmt = NULL;
  283. }
  284. tail_xbb_full_write(buf, nread);
  285. }
  286. } while (++i < nfiles);
  287. }
  288. return status;
  289. }