3
0

tail.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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, i, opt;
  82. unsigned seen;
  83. int *fds;
  84. char *s, *buf;
  85. const char *fmt;
  86. #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
  87. /* Allow legacy syntax of an initial numeric option without -n. */
  88. if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
  89. && isdigit(argv[1][1])
  90. ) {
  91. count = eat_num(&argv[1][1]);
  92. argv++;
  93. argc--;
  94. }
  95. #endif
  96. USE_FEATURE_FANCY_TAIL(opt_complementary = "s+";) /* -s N */
  97. opt = getopt32(argv, "fc:n:" USE_FEATURE_FANCY_TAIL("qs:v"),
  98. &str_c, &str_n USE_FEATURE_FANCY_TAIL(,&sleep_period));
  99. #define FOLLOW (opt & 0x1)
  100. #define COUNT_BYTES (opt & 0x2)
  101. //if (opt & 0x1) // -f
  102. if (opt & 0x2) count = eat_num(str_c); // -c
  103. if (opt & 0x4) count = eat_num(str_n); // -n
  104. #if ENABLE_FEATURE_FANCY_TAIL
  105. if (opt & 0x8) header_threshhold = INT_MAX; // -q
  106. if (opt & 0x20) header_threshhold = 0; // -v
  107. #endif
  108. argc -= optind;
  109. argv += optind;
  110. from_top = G.status; /* 1 if there was "-c +N" or "-n +N" */
  111. G.status = EXIT_SUCCESS;
  112. /* open all the files */
  113. fds = xmalloc(sizeof(int) * (argc + 1));
  114. if (!argv[0]) {
  115. struct stat statbuf;
  116. if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
  117. opt &= ~1; /* clear FOLLOW */
  118. }
  119. *argv = (char *) bb_msg_standard_input;
  120. }
  121. nfiles = i = 0;
  122. do {
  123. int fd = open_or_warn_stdin(argv[i]);
  124. if (fd < 0) {
  125. G.status = EXIT_FAILURE;
  126. continue;
  127. }
  128. fds[nfiles] = fd;
  129. argv[nfiles++] = argv[i];
  130. } while (++i < argc);
  131. if (!nfiles)
  132. bb_error_msg_and_die("no files");
  133. /* prepare the buffer */
  134. tailbufsize = BUFSIZ;
  135. if (!from_top && COUNT_BYTES) {
  136. if (tailbufsize < count + BUFSIZ) {
  137. tailbufsize = count + BUFSIZ;
  138. }
  139. }
  140. tailbuf = xmalloc(tailbufsize);
  141. /* tail the files */
  142. fmt = header_fmt + 1; /* Skip header leading newline on first output. */
  143. i = 0;
  144. do {
  145. if (nfiles > header_threshhold) {
  146. tail_xprint_header(fmt, argv[i]);
  147. fmt = header_fmt;
  148. }
  149. /* Optimizing count-bytes case if the file is seekable.
  150. * Beware of backing up too far.
  151. * Also we exclude files with size 0 (because of /proc/xxx) */
  152. if (COUNT_BYTES && !from_top) {
  153. off_t current = lseek(fds[i], 0, SEEK_END);
  154. if (current > 0) {
  155. if (count == 0)
  156. continue; /* showing zero lines is easy :) */
  157. current -= count;
  158. if (current < 0)
  159. current = 0;
  160. xlseek(fds[i], current, SEEK_SET);
  161. bb_copyfd_size(fds[i], STDOUT_FILENO, count);
  162. continue;
  163. }
  164. }
  165. buf = tailbuf;
  166. taillen = 0;
  167. seen = 1;
  168. newlines_seen = 0;
  169. while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
  170. if (from_top) {
  171. nwrite = nread;
  172. if (seen < count) {
  173. if (COUNT_BYTES) {
  174. nwrite -= (count - seen);
  175. seen = count;
  176. } else {
  177. s = buf;
  178. do {
  179. --nwrite;
  180. if (*s++ == '\n' && ++seen == count) {
  181. break;
  182. }
  183. } while (nwrite);
  184. }
  185. }
  186. xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
  187. } else if (count) {
  188. if (COUNT_BYTES) {
  189. taillen += nread;
  190. if (taillen > (int)count) {
  191. memmove(tailbuf, tailbuf + taillen - count, count);
  192. taillen = count;
  193. }
  194. } else {
  195. int k = nread;
  196. int newlines_in_buf = 0;
  197. do { /* count '\n' in last read */
  198. k--;
  199. if (buf[k] == '\n') {
  200. newlines_in_buf++;
  201. }
  202. } while (k);
  203. if (newlines_seen + newlines_in_buf < (int)count) {
  204. newlines_seen += newlines_in_buf;
  205. taillen += nread;
  206. } else {
  207. int extra = (buf[nread-1] != '\n');
  208. k = newlines_seen + newlines_in_buf + extra - count;
  209. s = tailbuf;
  210. while (k) {
  211. if (*s == '\n') {
  212. k--;
  213. }
  214. s++;
  215. }
  216. taillen += nread - (s - tailbuf);
  217. memmove(tailbuf, s, taillen);
  218. newlines_seen = count - extra;
  219. }
  220. if (tailbufsize < (size_t)taillen + BUFSIZ) {
  221. tailbufsize = taillen + BUFSIZ;
  222. tailbuf = xrealloc(tailbuf, tailbufsize);
  223. }
  224. }
  225. buf = tailbuf + taillen;
  226. }
  227. } /* while (tail_read() > 0) */
  228. if (!from_top) {
  229. xwrite(STDOUT_FILENO, tailbuf, taillen);
  230. }
  231. } while (++i < nfiles);
  232. buf = xrealloc(tailbuf, BUFSIZ);
  233. fmt = NULL;
  234. if (FOLLOW) while (1) {
  235. sleep(sleep_period);
  236. i = 0;
  237. do {
  238. if (nfiles > header_threshhold) {
  239. fmt = header_fmt;
  240. }
  241. while ((nread = tail_read(fds[i], buf, BUFSIZ)) > 0) {
  242. if (fmt) {
  243. tail_xprint_header(fmt, argv[i]);
  244. fmt = NULL;
  245. }
  246. xwrite(STDOUT_FILENO, buf, nread);
  247. }
  248. } while (++i < nfiles);
  249. }
  250. if (ENABLE_FEATURE_CLEAN_UP) {
  251. free(fds);
  252. }
  253. return G.status;
  254. }