1 /* tailf.c -- tail a log file and then follow it
2 * Created: Tue Jan 9 15:49:21 1996 by faith@acm.org
3 * Copyright 1996, 2003 Rickard E. Faith (faith@acm.org)
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * less -F and tail -f cause a disk access every five seconds. This
24 * program avoids this problem by waiting for the file size to change.
25 * Hence, the file is not accessed, and the access time does not need to be
26 * flushed back to disk. This is sort of a "stealth" tail.
34 #include <sys/types.h>
40 #ifdef HAVE_INOTIFY_INIT
41 #include <sys/inotify.h>
45 #define DEFAULT_LINES 10
48 tailf(const char *filename, int lines)
56 if (!(str = fopen(filename, "r")))
57 err(EXIT_FAILURE, _("cannot open \"%s\" for read"), filename);
59 buf = malloc(lines * BUFSIZ);
61 while (fgets(p, BUFSIZ, str)) {
62 if (++tail >= lines) {
66 p = buf + (tail * BUFSIZ);
70 for (i = tail; i < lines; i++)
71 fputs(buf + (i * BUFSIZ), stdout);
72 for (i = 0; i < tail; i++)
73 fputs(buf + (i * BUFSIZ), stdout);
75 for (i = head; i < tail; i++)
76 fputs(buf + (i * BUFSIZ), stdout);
85 roll_file(const char *filename, off_t *size)
91 if (!(fd = open(filename, O_RDONLY)))
92 err(EXIT_FAILURE, _("cannot open \"%s\" for read"), filename);
94 if (fstat(fd, &st) == -1)
95 err(EXIT_FAILURE, _("cannot stat \"%s\""), filename);
97 if (st.st_size == *size) {
102 if (lseek(fd, *size, SEEK_SET) != (off_t)-1) {
105 while ((rc = read(fd, buf, sizeof(buf))) > 0) {
106 wc = write(STDOUT_FILENO, buf, rc);
108 warnx(_("incomplete write to \"%s\" (written %zd, expected %zd)\n"),
118 watch_file(const char *filename, off_t *size)
121 roll_file(filename, size);
127 #ifdef HAVE_INOTIFY_INIT
129 #define EVENTS (IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)
133 watch_file_inotify(const char *filename, off_t *size)
135 char buf[ NEVENTS * sizeof(struct inotify_event) ];
143 ffd = inotify_add_watch(fd, filename, EVENTS);
146 errx(EXIT_FAILURE, _("%s: cannot add inotify watch "
147 "(limit of inotify watches was reached)."),
150 err(EXIT_FAILURE, _("%s: cannot add inotify watch."), filename);
154 len = read(fd, buf, sizeof(buf));
155 if (len < 0 && (errno == EINTR || errno == EAGAIN))
159 _("%s: cannot read inotify events"), filename);
161 for (e = 0; e < len; ) {
162 struct inotify_event *ev = (struct inotify_event *) &buf[e];
164 if (ev->mask & IN_MODIFY)
165 roll_file(filename, size);
171 e += sizeof(struct inotify_event) + ev->len;
178 #endif /* HAVE_INOTIFY_INIT */
180 int main(int argc, char **argv)
182 const char *filename;
183 int lines = DEFAULT_LINES;
187 setlocale(LC_ALL, "");
188 bindtextdomain(PACKAGE, LOCALEDIR);
194 for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
195 if (!strcmp(*argv, "-n") || !strcmp(*argv, "--lines")) {
197 if (argc > 0 && (lines = atoi(argv[0])) <= 0)
198 errx(EXIT_FAILURE, _("invalid number of lines"));
200 else if (isdigit(argv[0][1])) {
201 if ((lines = atoi(*argv + 1)) <= 0)
202 errx(EXIT_FAILURE, _("invalid number of lines"));
205 errx(EXIT_FAILURE, _("invalid option"));
209 errx(EXIT_FAILURE, _("usage: tailf [-n N | -N] logfile"));
213 if (stat(filename, &st) != 0)
214 err(EXIT_FAILURE, _("cannot stat \"%s\""), filename);
217 tailf(filename, lines);
219 #ifdef HAVE_INOTIFY_INIT
220 if (!watch_file_inotify(filename, &size))
222 watch_file(filename, &size);