View | Details | Raw Unified | Return to bug 24955
Collapse All | Expand All

(-)Makefile (+1 lines)
Lines 2-6 Link Here
2
2
3
PROG=	tail
3
PROG=	tail
4
SRCS=	forward.c misc.c read.c reverse.c tail.c
4
SRCS=	forward.c misc.c read.c reverse.c tail.c
5
WARNS?=	2
5
6
6
.include <bsd.prog.mk>
7
.include <bsd.prog.mk>
(-)extern.h (-1 / +1 lines)
Lines 36-42 Link Here
36
 */
36
 */
37
37
38
#define	WR(p, size) do { \
38
#define	WR(p, size) do { \
39
	if (write(STDOUT_FILENO, p, size) != size) \
39
	if (write(STDOUT_FILENO, p, size) != (ssize_t)size) \
40
		oerr(); \
40
		oerr(); \
41
	} while(0)
41
	} while(0)
42
42
(-)forward.c (-30 / +37 lines)
Lines 94-103 Link Here
94
	off_t off;
94
	off_t off;
95
	struct stat *sbp;
95
	struct stat *sbp;
96
{
96
{
97
	int ch, kq = -1;
97
	int ch, n, kq = -1;
98
	int action = USE_SLEEP;
98
	int action = USE_SLEEP;
99
	struct kevent ev[2];
99
	struct kevent ev[2];
100
	struct stat sb2;
100
	struct stat sb2;
101
	struct timespec ts;
101
102
102
	switch(style) {
103
	switch(style) {
103
	case FBYTES:
104
	case FBYTES:
Lines 170-175 Link Here
170
			if (lines(fp, off))
171
			if (lines(fp, off))
171
				return;
172
				return;
172
		break;
173
		break;
174
	case NOTSET:
175
	case REVERSE:
176
		break;
173
	}
177
	}
174
178
175
	if (fflag) {
179
	if (fflag) {
Lines 193-201 Link Here
193
		clearerr(fp);
197
		clearerr(fp);
194
198
195
		switch (action) {
199
		switch (action) {
196
		case ADD_EVENTS: {
200
		case ADD_EVENTS:
197
			int n = 0;
201
			n = 0;
198
			struct timespec ts = { 0, 0 };
202
			ts.tv_sec = 0;
203
			ts.tv_nsec = 0;
199
204
200
			if (Fflag && fileno(fp) != STDIN_FILENO) {
205
			if (Fflag && fileno(fp) != STDIN_FILENO) {
201
				EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
206
				EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
Lines 208-231 Link Here
208
			n++;
213
			n++;
209
214
210
			if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
215
			if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
211
				close(kq);
212
				kq = -1;
213
				action = USE_SLEEP;
216
				action = USE_SLEEP;
214
			} else {
217
			} else {
215
				action = USE_KQUEUE;
218
				action = USE_KQUEUE;
216
			}
219
			}
217
			break;
220
			break;
218
		}
219
221
220
		case USE_KQUEUE:
222
		case USE_KQUEUE:
221
			if (kevent(kq, NULL, 0, ev, 1, NULL) < 0)
223
			ts.tv_sec = 1;
224
			ts.tv_nsec = 0;
225
			/*
226
			 * In the -F case we set a timeout to ensure that
227
			 * we re-stat the file at least once every second.
228
			 */
229
			n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
230
			if (n < 0)
222
				err(1, "kevent");
231
				err(1, "kevent");
223
232
			if (n == 0) {
224
			if (ev->filter == EVFILT_VNODE) {
233
				/* timeout */
225
				/* file was rotated, wait until it reappears */
234
				break;
226
				action = USE_SLEEP;
235
			} else if (ev->filter == EVFILT_READ && ev->data < 0) {
227
			} else if (ev->data < 0) {
236
				 /* file shrank, reposition to end */
228
				/* file shrank, reposition to end */
229
				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
237
				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
230
					ierr();
238
					ierr();
231
					return;
239
					return;
Lines 236-261 Link Here
236
		case USE_SLEEP:
244
		case USE_SLEEP:
237
                	(void) usleep(250000);
245
                	(void) usleep(250000);
238
	                clearerr(fp);
246
	                clearerr(fp);
247
			break;
248
		}
239
249
240
			if (Fflag && fileno(fp) != STDIN_FILENO &&
250
		if (Fflag && fileno(fp) != STDIN_FILENO) {
241
			    stat(fname, &sb2) != -1) {
251
			while (stat(fname, &sb2) != 0)
242
				if (sb2.st_ino != sbp->st_ino ||
252
				/* file was rotated, wait until it reappears */
243
				    sb2.st_dev != sbp->st_dev ||
253
				(void)sleep(1);
244
				    sb2.st_rdev != sbp->st_rdev ||
254
			if (sb2.st_ino != sbp->st_ino ||
245
				    sb2.st_nlink == 0) {
255
			    sb2.st_dev != sbp->st_dev ||
246
					fp = freopen(fname, "r", fp);
256
			    sb2.st_rdev != sbp->st_rdev ||
247
					if (fp == NULL) {
257
			    sb2.st_nlink == 0) {
248
						ierr();
258
				fp = freopen(fname, "r", fp);
249
						break;
259
				if (fp == NULL) {
250
					}
260
					ierr();
261
				} else {
251
					*sbp = sb2;
262
					*sbp = sb2;
252
					if (kq != -1)
263
					action = ADD_EVENTS;
253
						action = ADD_EVENTS;
254
				} else if (kq != -1) {
255
					action = USE_KQUEUE;
256
				}
264
				}
257
			}
265
			}
258
			break;
259
		}
266
		}
260
	}
267
	}
261
}
268
}
(-)read.c (-15 / +15 lines)
Lines 115-121 Link Here
115
	} else {
115
	} else {
116
		if (wrap && (len = ep - p))
116
		if (wrap && (len = ep - p))
117
			WR(p, len);
117
			WR(p, len);
118
		if (len = p - sp)
118
		if ((len = p - sp))
119
			WR(sp, len);
119
			WR(sp, len);
120
	}
120
	}
121
	return 0;
121
	return 0;
Lines 140-154 Link Here
140
		u_int blen;
140
		u_int blen;
141
		u_int len;
141
		u_int len;
142
		char *l;
142
		char *l;
143
	} *lines;
143
	} *slines;
144
	int ch;
144
	int ch;
145
	char *p;
145
	char *p;
146
	int blen, cnt, recno, wrap;
146
	int blen, cnt, recno, wrap;
147
	char *sp;
147
	char *sp;
148
148
149
	if ((lines = malloc(off * sizeof(*lines))) == NULL)
149
	if ((slines = malloc(off * sizeof(*slines))) == NULL)
150
		err(1, "malloc");
150
		err(1, "malloc");
151
	bzero(lines, off * sizeof(*lines));
151
	bzero(slines, off * sizeof(*slines));
152
	sp = NULL;
152
	sp = NULL;
153
	blen = cnt = recno = wrap = 0;
153
	blen = cnt = recno = wrap = 0;
154
154
Lines 160-172 Link Here
160
		}
160
		}
161
		*p++ = ch;
161
		*p++ = ch;
162
		if (ch == '\n') {
162
		if (ch == '\n') {
163
			if (lines[recno].blen < cnt) {
163
			if (slines[recno].blen < (u_int)cnt) {
164
				lines[recno].blen = cnt + 256;
164
				slines[recno].blen = cnt + 256;
165
				if ((lines[recno].l = realloc(lines[recno].l,
165
				if ((slines[recno].l = realloc(slines[recno].l,
166
				    lines[recno].blen)) == NULL)
166
				    slines[recno].blen)) == NULL)
167
					err(1, "realloc");
167
					err(1, "realloc");
168
			}
168
			}
169
			bcopy(sp, lines[recno].l, lines[recno].len = cnt);
169
			bcopy(sp, slines[recno].l, slines[recno].len = cnt);
170
			cnt = 0;
170
			cnt = 0;
171
			p = sp;
171
			p = sp;
172
			if (++recno == off) {
172
			if (++recno == off) {
Lines 180-187 Link Here
180
		return 1;
180
		return 1;
181
	}
181
	}
182
	if (cnt) {
182
	if (cnt) {
183
		lines[recno].l = sp;
183
		slines[recno].l = sp;
184
		lines[recno].len = cnt;
184
		slines[recno].len = cnt;
185
		if (++recno == off) {
185
		if (++recno == off) {
186
			wrap = 1;
186
			wrap = 1;
187
			recno = 0;
187
			recno = 0;
Lines 190-205 Link Here
190
190
191
	if (rflag) {
191
	if (rflag) {
192
		for (cnt = recno - 1; cnt >= 0; --cnt)
192
		for (cnt = recno - 1; cnt >= 0; --cnt)
193
			WR(lines[cnt].l, lines[cnt].len);
193
			WR(slines[cnt].l, slines[cnt].len);
194
		if (wrap)
194
		if (wrap)
195
			for (cnt = off - 1; cnt >= recno; --cnt)
195
			for (cnt = off - 1; cnt >= recno; --cnt)
196
				WR(lines[cnt].l, lines[cnt].len);
196
				WR(slines[cnt].l, slines[cnt].len);
197
	} else {
197
	} else {
198
		if (wrap)
198
		if (wrap)
199
			for (cnt = recno; cnt < off; ++cnt)
199
			for (cnt = recno; cnt < off; ++cnt)
200
				WR(lines[cnt].l, lines[cnt].len);
200
				WR(slines[cnt].l, slines[cnt].len);
201
		for (cnt = 0; cnt < recno; ++cnt)
201
		for (cnt = 0; cnt < recno; ++cnt)
202
			WR(lines[cnt].l, lines[cnt].len);
202
			WR(slines[cnt].l, slines[cnt].len);
203
	}
203
	}
204
	return 0;
204
	return 0;
205
}
205
}
(-)reverse.c (+2 lines)
Lines 101-106 Link Here
101
		case REVERSE:
101
		case REVERSE:
102
			r_buf(fp);
102
			r_buf(fp);
103
			break;
103
			break;
104
		case NOTSET:
105
			break;
104
		}
106
		}
105
}
107
}
106
108
(-)tail.c (-3 / +3 lines)
Lines 169-175 Link Here
169
	}
169
	}
170
170
171
	if (*argv)
171
	if (*argv)
172
		for (first = 1; fname = *argv++;) {
172
		for (first = 1; (fname = *argv++);) {
173
			if ((fp = fopen(fname, "r")) == NULL ||
173
			if ((fp = fopen(fname, "r")) == NULL ||
174
			    fstat(fileno(fp), &sb)) {
174
			    fstat(fileno(fp), &sb)) {
175
				ierr();
175
				ierr();
Lines 189-195 Link Here
189
			(void)fclose(fp);
189
			(void)fclose(fp);
190
		}
190
		}
191
	else {
191
	else {
192
		fname = "stdin";
192
		fname = strdup("stdin");
193
193
194
		if (fstat(fileno(stdin), &sb)) {
194
		if (fstat(fileno(stdin), &sb)) {
195
			ierr();
195
			ierr();
Lines 227-233 Link Here
227
	size_t len;
227
	size_t len;
228
	char *start;
228
	char *start;
229
229
230
	while (ap = *++argv) {
230
	while ((ap = *++argv)) {
231
		/* Return if "--" or not an option of any form. */
231
		/* Return if "--" or not an option of any form. */
232
		if (ap[0] != '-') {
232
		if (ap[0] != '-') {
233
			if (ap[0] != '+')
233
			if (ap[0] != '+')

Return to bug 24955