tail.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. off_t current,end;
  75. struct stat sbuf;
  76. end = current = lseek(fd, 0, SEEK_CUR);
  77. if (!fstat(fd, &sbuf))
  78. end = sbuf.st_size;
  79. lseek(fd, end < current ? 0 : current, SEEK_SET);
  80. if ((r = safe_read(fd, buf, count)) < 0) {
  81. bb_perror_msg("read");
  82. status = EXIT_FAILURE;
  83. }
  84. return r;
  85. }
  86. static const char tail_opts[] =
  87. "fn:c:"
  88. #ifdef CONFIG_FEATURE_FANCY_TAIL
  89. "qs:v"
  90. #endif
  91. ;
  92. static const char header_fmt[] = "\n==> %s <==\n";
  93. int tail_main(int argc, char **argv)
  94. {
  95. long count = 10;
  96. unsigned int sleep_period = 1;
  97. int from_top = 0;
  98. int follow = 0;
  99. int header_threshhold = 1;
  100. int count_bytes = 0;
  101. char *tailbuf;
  102. size_t tailbufsize;
  103. int taillen = 0;
  104. int newline = 0;
  105. int *fds, nfiles, nread, nwrite, seen, i, opt;
  106. char *s, *buf;
  107. const char *fmt;
  108. /* Allow legacy syntax of an initial numeric option without -n. */
  109. if (argc >=2 && ((argv[1][0] == '+') || ((argv[1][0] == '-')
  110. /* && (isdigit)(argv[1][1]) */
  111. && (((unsigned int)(argv[1][1] - '0')) <= 9))))
  112. {
  113. optind = 2;
  114. optarg = argv[1];
  115. goto GET_COUNT;
  116. }
  117. while ((opt = getopt(argc, argv, tail_opts)) > 0) {
  118. switch (opt) {
  119. case 'f':
  120. follow = 1;
  121. break;
  122. case 'c':
  123. count_bytes = 1;
  124. /* FALLS THROUGH */
  125. case 'n':
  126. GET_COUNT:
  127. count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
  128. /* Note: Leading whitespace is an error trapped above. */
  129. if (*optarg == '+') {
  130. from_top = 1;
  131. } else {
  132. from_top = 0;
  133. }
  134. if (count < 0) {
  135. count = -count;
  136. }
  137. break;
  138. #ifdef CONFIG_FEATURE_FANCY_TAIL
  139. case 'q':
  140. header_threshhold = INT_MAX;
  141. break;
  142. case 's':
  143. sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
  144. break;
  145. case 'v':
  146. header_threshhold = 0;
  147. break;
  148. #endif
  149. default:
  150. bb_show_usage();
  151. }
  152. }
  153. /* open all the files */
  154. fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
  155. argv += optind;
  156. nfiles = i = 0;
  157. if ((argc -= optind) == 0) {
  158. struct stat statbuf;
  159. if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
  160. follow = 0;
  161. }
  162. /* --argv; */
  163. *argv = (char *) bb_msg_standard_input;
  164. goto DO_STDIN;
  165. }
  166. do {
  167. if ((argv[i][0] == '-') && !argv[i][1]) {
  168. DO_STDIN:
  169. fds[nfiles] = STDIN_FILENO;
  170. } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
  171. bb_perror_msg("%s", argv[i]);
  172. status = EXIT_FAILURE;
  173. continue;
  174. }
  175. argv[nfiles] = argv[i];
  176. ++nfiles;
  177. } while (++i < argc);
  178. if (!nfiles) {
  179. bb_error_msg_and_die("no files");
  180. }
  181. tailbufsize = BUFSIZ;
  182. /* tail the files */
  183. if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
  184. /* Hence, !from_top && count_bytes */
  185. if (tailbufsize < count) {
  186. tailbufsize = count + BUFSIZ;
  187. }
  188. }
  189. buf = tailbuf = xmalloc(tailbufsize);
  190. fmt = header_fmt + 1; /* Skip header leading newline on first output. */
  191. i = 0;
  192. do {
  193. /* Be careful. It would be possible to optimize the count-bytes
  194. * case if the file is seekable. If you do though, remember that
  195. * starting file position may not be the beginning of the file.
  196. * Beware of backing up too far. See example in wc.c.
  197. */
  198. if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
  199. continue;
  200. }
  201. if (nfiles > header_threshhold) {
  202. tail_xprint_header(fmt, argv[i]);
  203. fmt = header_fmt;
  204. }
  205. buf = tailbuf;
  206. taillen = 0;
  207. seen = 1;
  208. newline = 0;
  209. while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
  210. if (from_top) {
  211. nwrite = nread;
  212. if (seen < count) {
  213. if (count_bytes) {
  214. nwrite -= (count - seen);
  215. seen = count;
  216. } else {
  217. s = buf;
  218. do {
  219. --nwrite;
  220. if ((*s++ == '\n') && (++seen == count)) {
  221. break;
  222. }
  223. } while (nwrite);
  224. }
  225. }
  226. tail_xbb_full_write(buf + nread - nwrite, nwrite);
  227. } else if (count) {
  228. if (count_bytes) {
  229. taillen += nread;
  230. if (taillen > count) {
  231. memmove(tailbuf, tailbuf + taillen - count, count);
  232. taillen = count;
  233. }
  234. } else {
  235. int k = nread;
  236. int nbuf = 0;
  237. while (k) {
  238. --k;
  239. if (buf[k] == '\n') {
  240. ++nbuf;
  241. }
  242. }
  243. if (newline + nbuf < count) {
  244. newline += nbuf;
  245. taillen += nread;
  246. } else {
  247. int extra = 0;
  248. if (buf[nread-1] != '\n') {
  249. extra = 1;
  250. }
  251. k = newline + nbuf + extra - count;
  252. s = tailbuf;
  253. while (k) {
  254. if (*s == '\n') {
  255. --k;
  256. }
  257. ++s;
  258. }
  259. taillen += nread - (s - tailbuf);
  260. memmove(tailbuf, s, taillen);
  261. newline = count - extra;
  262. }
  263. if (tailbufsize < taillen + BUFSIZ) {
  264. tailbufsize = taillen + BUFSIZ;
  265. tailbuf = xrealloc(tailbuf, tailbufsize);
  266. }
  267. }
  268. buf = tailbuf + taillen;
  269. }
  270. }
  271. if (!from_top) {
  272. tail_xbb_full_write(tailbuf, taillen);
  273. }
  274. taillen = 0;
  275. } while (++i < nfiles);
  276. buf = xrealloc(tailbuf, BUFSIZ);
  277. fmt = NULL;
  278. while (follow) {
  279. sleep(sleep_period);
  280. i = 0;
  281. do {
  282. if (nfiles > header_threshhold) {
  283. fmt = header_fmt;
  284. }
  285. while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
  286. if (fmt) {
  287. tail_xprint_header(fmt, argv[i]);
  288. fmt = NULL;
  289. }
  290. tail_xbb_full_write(buf, nread);
  291. }
  292. } while (++i < nfiles);
  293. }
  294. return status;
  295. }