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#define FOR_tail
33#include "toys.h"
34#include <sys/inotify.h>
35
36GLOBALS(
37 long lines;
38 long bytes;
39
40 int file_no, ffd, *files;
41)
42
43struct line_list {
44 struct line_list *next, *prev;
45 char *data;
46 int len;
47};
48
49static struct line_list *get_chunk(int fd, int len)
50{
51 struct line_list *line = xmalloc(sizeof(struct line_list)+len);
52
53 memset(line, 0, sizeof(struct line_list));
54 line->data = ((char *)line) + sizeof(struct line_list);
55 line->len = readall(fd, line->data, len);
56
57 if (line->len < 1) {
58 free(line);
59 return 0;
60 }
61
62 return line;
63}
64
65static void dump_chunk(void *ptr)
66{
67 struct line_list *list = ptr;
68
69 xwrite(1, list->data, list->len);
70 free(list);
71}
72
73
74
75
76static int try_lseek(int fd, long bytes, long lines)
77{
78 struct line_list *list = 0, *temp;
79 int flag = 0, chunk = sizeof(toybuf);
80 off_t pos = lseek(fd, 0, SEEK_END);
81
82
83 if (pos<0) return 0;
84
85
86 if (bytes) {
87 if (lseek(fd, bytes, SEEK_END)<0) lseek(fd, 0, SEEK_SET);
88 xsendfile(fd, 1);
89 return 1;
90 }
91
92
93
94 bytes = pos;
95 while (lines && pos) {
96 int offset;
97
98
99 if (chunk>pos) chunk = pos;
100 pos -= chunk;
101 if (pos != lseek(fd, pos, SEEK_SET)) {
102 perror_msg("seek failed");
103 break;
104 }
105 if (!(temp = get_chunk(fd, chunk))) break;
106 temp->next = list;
107 list = temp;
108
109
110 offset = list->len;
111 while (offset--) {
112
113 if (!flag) flag++;
114
115
116 else if (list->data[offset] == '\n' && !++lines) {
117 offset++;
118 list->data += offset;
119 list->len -= offset;
120
121 break;
122 }
123 }
124 }
125
126
127 llist_traverse(list, dump_chunk);
128
129
130 lseek(fd, bytes, SEEK_SET);
131 return 1;
132}
133
134
135static void do_tail(int fd, char *name)
136{
137 long bytes = TT.bytes, lines = TT.lines;
138 int linepop = 1;
139
140 if (toys.optflags & FLAG_f) {
141 int f = TT.file_no*2;
142 char *s = name;
143
144 if (!fd) sprintf(s = toybuf, "/proc/self/fd/%d", fd);
145 TT.files[f++] = fd;
146 if (0 > (TT.files[f] = inotify_add_watch(TT.ffd, s, IN_MODIFY)))
147 perror_msg("bad -f on '%s'", name);
148 }
149
150 if (TT.file_no++) xputc('\n');
151 if (toys.optc > 1) xprintf("==> %s <==\n", name);
152
153
154
155 if (bytes<0 || lines<0) {
156 struct line_list *list = 0, *new;
157
158
159
160 if (CFG_TAIL_SEEK && try_lseek(fd, bytes, lines)) return;
161
162
163 for (;;) {
164
165 if (!(new = get_chunk(fd, sizeof(toybuf)))) break;
166 dlist_add_nomalloc((void *)&list, (void *)new);
167
168
169 if (TT.bytes) {
170 bytes += new->len;
171 if (bytes > 0) {
172 while (list->len <= bytes) {
173 bytes -= list->len;
174 free(dlist_pop(&list));
175 }
176 list->data += bytes;
177 list->len -= bytes;
178 bytes = 0;
179 }
180 } else {
181 int len = new->len, count;
182 char *try = new->data;
183
184
185
186 for (count=0; count<len; count++) {
187 if (linepop) lines++;
188 linepop = try[count] == '\n';
189
190 if (lines > 0) {
191 char c;
192
193 do {
194 c = *list->data;
195 if (!--(list->len)) free(dlist_pop(&list));
196 else list->data++;
197 } while (c != '\n');
198 lines--;
199 }
200 }
201 }
202 }
203
204
205 llist_traverse(list, dump_chunk);
206
207
208 } else for (;;) {
209 int len, offset = 0;
210
211
212 len = read(fd, toybuf, sizeof(toybuf));
213 if (len<1) break;
214 while (bytes > 1 || lines > 1) {
215 bytes--;
216 if (toybuf[offset++] == '\n') lines--;
217 if (offset >= len) break;
218 }
219 if (offset<len) xwrite(1, toybuf+offset, len-offset);
220 }
221}
222
223void tail_main(void)
224{
225 char **args = toys.optargs;
226
227 if (!(toys.optflags&(FLAG_n|FLAG_c))) {
228 char *arg = *args;
229
230
231 if (arg && *arg == '-' && arg[1]) {
232 TT.lines = atolx(*(args++));
233 toys.optc--;
234 } else {
235
236 TT.lines = -10;
237 }
238 }
239
240
241 if (toys.optflags&FLAG_f) {
242 if ((TT.ffd = inotify_init()) < 0) perror_exit("inotify_init");
243 TT.files = xmalloc(toys.optc*8);
244 }
245 loopfiles_rw(args, O_RDONLY|WARN_ONLY|(O_CLOEXEC*!(toys.optflags&FLAG_f)),
246 0, do_tail);
247
248 if ((toys.optflags & FLAG_f) && TT.file_no) {
249 int len, last_fd = TT.files[(TT.file_no-1)*2], i, fd;
250 struct inotify_event ev;
251
252 for (;;) {
253 if (sizeof(ev)!=read(TT.ffd, &ev, sizeof(ev))) perror_exit("inotify");
254
255 for (i = 0; i<TT.file_no && ev.wd!=TT.files[(i*2)+1]; i++);
256 if (i==TT.file_no) continue;
257 fd = TT.files[i*2];
258
259
260 while ((len = read(fd, toybuf, sizeof(toybuf)))>0) {
261 if (last_fd != fd) {
262 last_fd = fd;
263 xprintf("\n==> %s <==\n", args[i]);
264 }
265
266 xwrite(1, toybuf, len);
267 }
268 }
269 }
270}
271