tail.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. const char *str_c, *str_n;
  76. char *tailbuf;
  77. size_t tailbufsize;
  78. unsigned header_threshhold = 1;
  79. unsigned nfiles;
  80. int i, opt;
  81. int *fds;
  82. const char *fmt;
  83. #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
  84. /* Allow legacy syntax of an initial numeric option without -n. */
  85. if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
  86. && isdigit(argv[1][1])
  87. ) {
  88. count = eat_num(argv[1]);
  89. argv++;
  90. argc--;
  91. }
  92. #endif
  93. /* -s NUM, -F imlies -f */
  94. IF_FEATURE_FANCY_TAIL(opt_complementary = "s+:Ff";)
  95. opt = getopt32(argv, "fc:n:" IF_FEATURE_FANCY_TAIL("qs:vF"),
  96. &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period));
  97. #define FOLLOW (opt & 0x1)
  98. #define COUNT_BYTES (opt & 0x2)
  99. //if (opt & 0x1) // -f
  100. if (opt & 0x2) count = eat_num(str_c); // -c
  101. if (opt & 0x4) count = eat_num(str_n); // -n
  102. #if ENABLE_FEATURE_FANCY_TAIL
  103. /* q: make it impossible for nfiles to be > header_threshhold */
  104. if (opt & 0x8) header_threshhold = UINT_MAX; // -q
  105. if (opt & 0x20) header_threshhold = 0; // -v
  106. #define FOLLOW_RETRY (opt & 0x40)
  107. #else
  108. #define FOLLOW_RETRY 0
  109. #endif
  110. argc -= optind;
  111. argv += optind;
  112. from_top = G.status; /* 1 if there was "-c +N" or "-n +N" */
  113. G.status = EXIT_SUCCESS;
  114. /* open all the files */
  115. fds = xmalloc(sizeof(fds[0]) * (argc + 1));
  116. if (!argv[0]) {
  117. struct stat statbuf;
  118. if (fstat(STDIN_FILENO, &statbuf) == 0
  119. && S_ISFIFO(statbuf.st_mode)
  120. ) {
  121. opt &= ~1; /* clear FOLLOW */
  122. }
  123. argv[0] = (char *) bb_msg_standard_input;
  124. }
  125. nfiles = i = 0;
  126. do {
  127. int fd = open_or_warn_stdin(argv[i]);
  128. if (fd < 0 && !FOLLOW_RETRY) {
  129. G.status = EXIT_FAILURE;
  130. continue;
  131. }
  132. fds[nfiles] = fd;
  133. argv[nfiles++] = argv[i];
  134. } while (++i < argc);
  135. if (!nfiles)
  136. bb_error_msg_and_die("no files");
  137. /* prepare the buffer */
  138. tailbufsize = BUFSIZ;
  139. if (!from_top && COUNT_BYTES) {
  140. if (tailbufsize < count + BUFSIZ) {
  141. tailbufsize = count + BUFSIZ;
  142. }
  143. }
  144. tailbuf = xmalloc(tailbufsize);
  145. /* tail the files */
  146. fmt = header_fmt + 1; /* skip header leading newline on first output */
  147. i = 0;
  148. do {
  149. char *buf;
  150. int taillen;
  151. int newlines_seen;
  152. unsigned seen;
  153. int nread;
  154. if (ENABLE_FEATURE_FANCY_TAIL && fds[i] < 0)
  155. continue; /* may happen with -E */
  156. if (nfiles > header_threshhold) {
  157. tail_xprint_header(fmt, argv[i]);
  158. fmt = header_fmt;
  159. }
  160. /* Optimizing count-bytes case if the file is seekable.
  161. * Beware of backing up too far.
  162. * Also we exclude files with size 0 (because of /proc/xxx) */
  163. if (COUNT_BYTES && !from_top) {
  164. off_t current = lseek(fds[i], 0, SEEK_END);
  165. if (current > 0) {
  166. if (count == 0)
  167. continue; /* showing zero lines is easy :) */
  168. current -= count;
  169. if (current < 0)
  170. current = 0;
  171. xlseek(fds[i], current, SEEK_SET);
  172. bb_copyfd_size(fds[i], STDOUT_FILENO, count);
  173. continue;
  174. }
  175. }
  176. buf = tailbuf;
  177. taillen = 0;
  178. seen = 1;
  179. newlines_seen = 0;
  180. while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
  181. if (from_top) {
  182. int nwrite = nread;
  183. if (seen < count) {
  184. if (COUNT_BYTES) {
  185. nwrite -= (count - seen);
  186. seen = count;
  187. } else {
  188. char *s = buf;
  189. do {
  190. --nwrite;
  191. if (*s++ == '\n' && ++seen == count) {
  192. break;
  193. }
  194. } while (nwrite);
  195. }
  196. }
  197. xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
  198. } else if (count) {
  199. if (COUNT_BYTES) {
  200. taillen += nread;
  201. if (taillen > (int)count) {
  202. memmove(tailbuf, tailbuf + taillen - count, count);
  203. taillen = count;
  204. }
  205. } else {
  206. int k = nread;
  207. int newlines_in_buf = 0;
  208. do { /* count '\n' in last read */
  209. k--;
  210. if (buf[k] == '\n') {
  211. newlines_in_buf++;
  212. }
  213. } while (k);
  214. if (newlines_seen + newlines_in_buf < (int)count) {
  215. newlines_seen += newlines_in_buf;
  216. taillen += nread;
  217. } else {
  218. int extra = (buf[nread-1] != '\n');
  219. char *s;
  220. k = newlines_seen + newlines_in_buf + extra - count;
  221. s = tailbuf;
  222. while (k) {
  223. if (*s == '\n') {
  224. k--;
  225. }
  226. s++;
  227. }
  228. taillen += nread - (s - tailbuf);
  229. memmove(tailbuf, s, taillen);
  230. newlines_seen = count - extra;
  231. }
  232. if (tailbufsize < (size_t)taillen + BUFSIZ) {
  233. tailbufsize = taillen + BUFSIZ;
  234. tailbuf = xrealloc(tailbuf, tailbufsize);
  235. }
  236. }
  237. buf = tailbuf + taillen;
  238. }
  239. } /* while (tail_read() > 0) */
  240. if (!from_top) {
  241. xwrite(STDOUT_FILENO, tailbuf, taillen);
  242. }
  243. } while (++i < nfiles);
  244. tailbuf = xrealloc(tailbuf, BUFSIZ);
  245. fmt = NULL;
  246. if (FOLLOW) while (1) {
  247. sleep(sleep_period);
  248. i = 0;
  249. do {
  250. int nread;
  251. const char *filename = argv[i];
  252. int fd = fds[i];
  253. if (FOLLOW_RETRY) {
  254. struct stat sbuf, fsbuf;
  255. if (fd < 0
  256. || fstat(fd, &fsbuf) < 0
  257. || stat(filename, &sbuf) < 0
  258. || fsbuf.st_dev != sbuf.st_dev
  259. || fsbuf.st_ino != sbuf.st_ino
  260. ) {
  261. int new_fd;
  262. if (fd >= 0)
  263. close(fd);
  264. new_fd = open(filename, O_RDONLY);
  265. if (new_fd >= 0) {
  266. bb_error_msg("%s has %s; following end of new file",
  267. filename, (fd < 0) ? "appeared" : "been replaced"
  268. );
  269. } else if (fd >= 0) {
  270. bb_perror_msg("%s has become inaccessible", filename);
  271. }
  272. fds[i] = fd = new_fd;
  273. }
  274. }
  275. if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
  276. continue;
  277. if (nfiles > header_threshhold) {
  278. fmt = header_fmt;
  279. }
  280. while ((nread = tail_read(fd, tailbuf, BUFSIZ)) > 0) {
  281. if (fmt) {
  282. tail_xprint_header(fmt, filename);
  283. fmt = NULL;
  284. }
  285. xwrite(STDOUT_FILENO, tailbuf, nread);
  286. }
  287. } while (++i < nfiles);
  288. }
  289. if (ENABLE_FEATURE_CLEAN_UP) {
  290. free(fds);
  291. }
  292. return G.status;
  293. }