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