summaryrefslogtreecommitdiff
path: root/simpletail.c
blob: b5df235c7174589c9bd8e6610ad97f79dceaaa99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * simpletail.c
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "inotify.h"
#include "inotify-syscalls.h"

#include "inotail.h"

#define BUFFER_SIZE 4096

off_t lines(int fd, int file_size, unsigned int nr_lines)
{
	int i;
	char buf[BUFFER_SIZE];
	off_t offset = file_size;

	/* Negative offsets don't make sense here */
	if (offset < 0)
		offset = 0;

	while (offset > 0 && nr_lines > 0) {
		int rc;
		int block_size = BUFFER_SIZE; /* Size of the current block we're reading */

		if (offset < BUFFER_SIZE)
			block_size = offset;

		/* Start of current block */
		offset -= block_size;

		dprintf("  offset: %lu\n", offset);

		lseek(fd, offset, SEEK_SET);

		rc = read(fd, &buf, block_size);

		for (i = block_size; i > 0; i--) {
			if (buf[i] == '\n') {
				dprintf("  Found \\n at position %d\n", i);
				nr_lines--;

				if (nr_lines == 0)
					break;
			}
		}
	}

	if (nr_lines == 0)
		offset += i + 1; /* We don't want the first \n */

	return offset;
}

int watch_file(const char *filename, off_t offset)
{
	int ifd, watch;
	struct inotify_event *inev;
	char buf[BUFFER_SIZE];

	dprintf(">> Watching %s\n", filename);

	ifd = inotify_init();
	if (ifd < 0) {
		perror("inotify_init()");
		exit(-2);
	}

	watch = inotify_add_watch(ifd, filename, IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT);

	memset(&buf, 0, sizeof(buf));

	while (1) {
		int len;

		len = read(ifd, buf, sizeof(buf));
		inev = (struct inotify_event *) &buf;

		while (len > 0) {
			if (inev->mask & IN_MODIFY) {
				int ffd, block_size;
				char fbuf[BUFFER_SIZE];
				struct stat finfo;

				dprintf("  File '%s' modified.\n", filename);
				dprintf("  offset: %lu.\n", offset);

				ffd = open(filename, O_RDONLY);
				if (fstat(ffd, &finfo) < 0) {
					perror("fstat()");
					return -1;
				}

				/* XXX: block_size could be bigger than BUFFER_SIZE */
				block_size = finfo.st_size - offset;
				if (block_size < 0)
					block_size = 0;

				lseek(ffd, offset, SEEK_SET);
				while (read(ffd, &fbuf, BUFFER_SIZE) != 0) {
					write(STDOUT_FILENO, fbuf, block_size);
				}

				offset = finfo.st_size;

				close(ffd);
			}

			if (inev->mask & IN_DELETE_SELF) {
				dprintf("  File '%s' deleted.\n", filename);
				return -1;
			}
			if (inev->mask & IN_MOVE_SELF) {
				dprintf("  File '%s' moved.\n", filename);
				return -1;
			}
			if (inev->mask & IN_UNMOUNT) {
				dprintf("  Device containing file '%s' unmounted.\n", filename);
				return -1;
			}

			len -= sizeof(struct inotify_event) + inev->len;
			inev = (struct inotify_event *) ((char *) inev + sizeof(struct inotify_event) + inev->len);
		}
	}
}

int main(int argc, char **argv)
{
	int fd;
	int nr_lines = 0;
	int ret = 0;
	struct stat finfo;
	char buf[BUFFER_SIZE];
	off_t offset = 0;

	if (argc < 3) {
		fprintf(stderr, "%s <nr-lines> <file> <forever?>\n", argv[0]);
		return -1;
	}

	nr_lines = strtol(argv[1], NULL, 0) + 1;
	fd = open(argv[2], O_RDONLY);

	if (fd < 0) {
		perror("open()");
		return -1;
	}

	if (fstat(fd, &finfo) < 0) {
		perror("fstat()");
		return -1;
	}

	offset = lines(fd, finfo.st_size, nr_lines);
	dprintf("  offset: %lu.\n", offset);

	lseek(fd, offset, SEEK_SET);
	while (read(fd, &buf, BUFFER_SIZE) != 0) {
		write(STDOUT_FILENO, buf, finfo.st_size - offset);
	}

	close(fd);

	if (argv[3])
		ret = watch_file(argv[2], finfo.st_size);

	return ret;
}