tail.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
  10. /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
  12. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  13. *
  14. * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
  15. * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
  16. * 1) mixing printf/write without fflush()ing stdout
  17. * 2) no check that any open files are present
  18. * 3) optstring had -q taking an arg
  19. * 4) no error checking on write in some cases, and a warning even then
  20. * 5) q and s interaction bug
  21. * 6) no check for lseek error
  22. * 7) lseek attempted when count==0 even if arg was +0 (from top)
  23. */
  24. #include "libbb.h"
  25. static const struct suffix_mult tail_suffixes[] = {
  26. { "b", 512 },
  27. { "k", 1024 },
  28. { "m", 1024*1024 },
  29. { }
  30. };
  31. struct globals {
  32. bool status;
  33. };
  34. #define G (*(struct globals*)&bb_common_bufsiz1)
  35. static void tail_xprint_header(const char *fmt, const char *filename)
  36. {
  37. if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
  38. bb_perror_nomsg_and_die();
  39. }
  40. static ssize_t tail_read(int fd, char *buf, size_t count)
  41. {
  42. ssize_t r;
  43. off_t current;
  44. struct stat sbuf;
  45. /* (A good comment is missing here) */
  46. current = lseek(fd, 0, SEEK_CUR);
  47. /* /proc files report zero st_size, don't lseek them. */
  48. if (fstat(fd, &sbuf) == 0 && sbuf.st_size)
  49. if (sbuf.st_size < current)
  50. lseek(fd, 0, SEEK_SET);
  51. r = full_read(fd, buf, count);
  52. if (r < 0) {
  53. bb_perror_msg(bb_msg_read_error);
  54. G.status = EXIT_FAILURE;
  55. }
  56. return r;
  57. }
  58. static const char header_fmt[] ALIGN1 = "\n==> %s <==\n";
  59. static unsigned eat_num(const char *p)
  60. {
  61. if (*p == '-')
  62. p++;
  63. else if (*p == '+') {
  64. p++;
  65. G.status = 1; /* mark that we saw "+" */
  66. }
  67. return xatou_sfx(p, tail_suffixes);
  68. }
  69. int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  70. int tail_main(int argc, char **argv)
  71. {
  72. unsigned count = 10;
  73. unsigned sleep_period = 1;
  74. bool from_top;
  75. int header_threshhold = 1;
  76. const char *str_c, *str_n;
  77. char *tailbuf;
  78. size_t tailbufsize;
  79. int taillen = 0;
  80. int newlines_seen = 0;
  81. int nfiles, nread, nwrite, seen, i, opt;
  82. int *fds;
  83. char *s, *buf;
  84. const char *fmt;
  85. #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
  86. /* Allow legacy syntax of an initial numeric option without -n. */
  87. if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
  88. && isdigit(argv[1][1])
  89. ) {
  90. count = eat_num(&argv[1][1]);
  91. argv++;
  92. argc--;
  93. }
  94. #endif
  95. USE_FEATURE_FANCY_TAIL(opt_complementary = "s+";) /* -s N */
  96. opt = getopt32(argv, "fc:n:" USE_FEATURE_FANCY_TAIL("qs:v"),
  97. &str_c, &str_n USE_FEATURE_FANCY_TAIL(,&sleep_period));
  98. #define FOLLOW (opt & 0x1)
  99. #define COUNT_BYTES (opt & 0x2)
  100. //if (opt & 0x1) // -f
  101. if (opt & 0x2) count = eat_num(str_c); // -c
  102. if (opt & 0x4) count = eat_num(str_n); // -n
  103. #if ENABLE_FEATURE_FANCY_TAIL
  104. if (opt & 0x8) header_threshhold = INT_MAX; // -q
  105. if (opt & 0x20) header_threshhold = 0; // -v
  106. #endif
  107. argc -= optind;
  108. argv += optind;
  109. from_top = G.status; /* 1 if there was "-c +N" or "-n +N" */
  110. G.status = EXIT_SUCCESS;
  111. /* open all the files */
  112. fds = xmalloc(sizeof(int) * (argc + 1));
  113. if (!argv[0]) {
  114. struct stat statbuf;
  115. if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
  116. opt &= ~1; /* clear FOLLOW */
  117. }
  118. *argv = (char *) bb_msg_standard_input;
  119. }
  120. nfiles = i = 0;
  121. do {
  122. int fd = open_or_warn_stdin(argv[i]);
  123. if (fd < 0) {
  124. G.status = EXIT_FAILURE;
  125. continue;
  126. }
  127. fds[nfiles] = fd;
  128. argv[nfiles++] = argv[i];
  129. } while (++i < argc);
  130. if (!nfiles)
  131. bb_error_msg_and_die("no files");
  132. /* prepare the buffer */
  133. tailbufsize = BUFSIZ;
  134. if (!from_top && COUNT_BYTES) {
  135. if (tailbufsize < count + BUFSIZ) {
  136. tailbufsize = count + BUFSIZ;
  137. }
  138. }
  139. tailbuf = xmalloc(tailbufsize);
  140. /* tail the files */
  141. fmt = header_fmt + 1; /* Skip header leading newline on first output. */
  142. i = 0;
  143. do {
  144. if (nfiles > header_threshhold) {
  145. tail_xprint_header(fmt, argv[i]);
  146. fmt = header_fmt;
  147. }
  148. /* Optimizing count-bytes case if the file is seekable.
  149. * Beware of backing up too far.
  150. * Also we exclude files with size 0 (because of /proc/xxx) */
  151. if (COUNT_BYTES && !from_top) {
  152. off_t current = lseek(fds[i], 0, SEEK_END);
  153. if (current > 0) {
  154. if (count == 0)
  155. continue; /* showing zero lines is easy :) */
  156. current -= count;
  157. if (current < 0)
  158. current = 0;
  159. xlseek(fds[i], current, SEEK_SET);
  160. bb_copyfd_size(fds[i], STDOUT_FILENO, count);
  161. continue;
  162. }
  163. }
  164. buf = tailbuf;
  165. taillen = 0;
  166. seen = 1;
  167. newlines_seen = 0;
  168. while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
  169. if (from_top) {
  170. nwrite = nread;
  171. if (seen < count) {
  172. if (COUNT_BYTES) {
  173. nwrite -= (count - seen);
  174. seen = count;
  175. } else {
  176. s = buf;
  177. do {
  178. --nwrite;
  179. if (*s++ == '\n' && ++seen == count) {
  180. break;
  181. }
  182. } while (nwrite);
  183. }
  184. }
  185. xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
  186. } else if (count) {
  187. if (COUNT_BYTES) {
  188. taillen += nread;
  189. if (taillen > count) {
  190. memmove(tailbuf, tailbuf + taillen - count, count);
  191. taillen = count;
  192. }
  193. } else {
  194. int k = nread;
  195. int newlines_in_buf = 0;
  196. do { /* count '\n' in last read */
  197. k--;
  198. if (buf[k] == '\n') {
  199. newlines_in_buf++;
  200. }
  201. } while (k);
  202. if (newlines_seen + newlines_in_buf < count) {
  203. newlines_seen += newlines_in_buf;
  204. taillen += nread;
  205. } else {
  206. int extra = (buf[nread-1] != '\n');
  207. k = newlines_seen + newlines_in_buf + extra - count;
  208. s = tailbuf;
  209. while (k) {
  210. if (*s == '\n') {
  211. k--;
  212. }
  213. s++;
  214. }
  215. taillen += nread - (s - tailbuf);
  216. memmove(tailbuf, s, taillen);
  217. newlines_seen = count - extra;
  218. }
  219. if (tailbufsize < taillen + BUFSIZ) {
  220. tailbufsize = taillen + BUFSIZ;
  221. tailbuf = xrealloc(tailbuf, tailbufsize);
  222. }
  223. }
  224. buf = tailbuf + taillen;
  225. }
  226. } /* while (tail_read() > 0) */
  227. if (!from_top) {
  228. xwrite(STDOUT_FILENO, tailbuf, taillen);
  229. }
  230. } while (++i < nfiles);
  231. buf = xrealloc(tailbuf, BUFSIZ);
  232. fmt = NULL;
  233. if (FOLLOW) while (1) {
  234. sleep(sleep_period);
  235. i = 0;
  236. do {
  237. if (nfiles > header_threshhold) {
  238. fmt = header_fmt;
  239. }
  240. while ((nread = tail_read(fds[i], buf, BUFSIZ)) > 0) {
  241. if (fmt) {
  242. tail_xprint_header(fmt, argv[i]);
  243. fmt = NULL;
  244. }
  245. xwrite(STDOUT_FILENO, buf, nread);
  246. }
  247. } while (++i < nfiles);
  248. }
  249. if (ENABLE_FEATURE_CLEAN_UP) {
  250. free(fds);
  251. }
  252. return G.status;
  253. }