tail.c revision 1590
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Edward Sze-Tyan Wang.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char copyright[] =
39"@(#) Copyright (c) 1991, 1993\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44static char sccsid[] = "@(#)tail.c	8.1 (Berkeley) 6/6/93";
45#endif /* not lint */
46
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <errno.h>
50#include <unistd.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include "extern.h"
55
56int fflag, rflag, rval;
57char *fname;
58
59static void obsolete __P((char **));
60static void usage __P((void));
61
62int
63main(argc, argv)
64	int argc;
65	char *argv[];
66{
67	struct stat sb;
68	FILE *fp;
69	long off;
70	enum STYLE style;
71	int ch, first;
72	char *p;
73
74	/*
75	 * Tail's options are weird.  First, -n10 is the same as -n-10, not
76	 * -n+10.  Second, the number options are 1 based and not offsets,
77	 * so -n+1 is the first line, and -c-1 is the last byte.  Third, the
78	 * number options for the -r option specify the number of things that
79	 * get displayed, not the starting point in the file.  The one major
80	 * incompatibility in this version as compared to historical versions
81	 * is that the 'r' option couldn't be modified by the -lbc options,
82	 * i.e. it was always done in lines.  This version treats -rc as a
83	 * number of characters in reverse order.  Finally, the default for
84	 * -r is the entire file, not 10 lines.
85	 */
86#define	ARG(units, forward, backward) {					\
87	if (style)							\
88		usage();						\
89	off = strtol(optarg, &p, 10) * (units);				\
90	if (*p)								\
91		err(1, "illegal offset -- %s", optarg);			\
92	switch(optarg[0]) {						\
93	case '+':							\
94		if (off)						\
95			off -= (units);					\
96			style = (forward);				\
97		break;							\
98	case '-':							\
99		off = -off;						\
100		/* FALLTHROUGH */					\
101	default:							\
102		style = (backward);					\
103		break;							\
104	}								\
105}
106
107	obsolete(argv);
108	style = NOTSET;
109	while ((ch = getopt(argc, argv, "b:c:fn:r")) != EOF)
110		switch(ch) {
111		case 'b':
112			ARG(512, FBYTES, RBYTES);
113			break;
114		case 'c':
115			ARG(1, FBYTES, RBYTES);
116			break;
117		case 'f':
118			fflag = 1;
119			break;
120		case 'n':
121			ARG(1, FLINES, RLINES);
122			break;
123		case 'r':
124			rflag = 1;
125			break;
126		case '?':
127		default:
128			usage();
129		}
130	argc -= optind;
131	argv += optind;
132
133	if (fflag && argc > 1)
134		err(1, "-f option only appropriate for a single file");
135
136	/*
137	 * If displaying in reverse, don't permit follow option, and convert
138	 * style values.
139	 */
140	if (rflag) {
141		if (fflag)
142			usage();
143		if (style == FBYTES)
144			style = RBYTES;
145		else if (style == FLINES)
146			style = RLINES;
147	}
148
149	/*
150	 * If style not specified, the default is the whole file for -r, and
151	 * the last 10 lines if not -r.
152	 */
153	if (style == NOTSET)
154		if (rflag) {
155			off = 0;
156			style = REVERSE;
157		} else {
158			off = 10;
159			style = RLINES;
160		}
161
162	if (*argv)
163		for (first = 1; fname = *argv++;) {
164			if ((fp = fopen(fname, "r")) == NULL ||
165			    fstat(fileno(fp), &sb)) {
166				ierr();
167				continue;
168			}
169			if (argc > 1) {
170				(void)printf("%s==> %s <==\n",
171				    first ? "" : "\n", fname);
172				first = 0;
173				(void)fflush(stdout);
174			}
175
176			if (rflag)
177				reverse(fp, style, off, &sb);
178			else
179				forward(fp, style, off, &sb);
180			(void)fclose(fp);
181		}
182	else {
183		fname = "stdin";
184
185		if (fstat(fileno(stdin), &sb)) {
186			ierr();
187			exit(1);
188		}
189
190		/*
191		 * Determine if input is a pipe.  4.4BSD will set the SOCKET
192		 * bit in the st_mode field for pipes.  Fix this then.
193		 */
194		if (lseek(fileno(stdin), (off_t)0, SEEK_CUR) == -1 &&
195		    errno == ESPIPE) {
196			errno = 0;
197			fflag = 0;		/* POSIX.2 requires this. */
198		}
199
200		if (rflag)
201			reverse(stdin, style, off, &sb);
202		else
203			forward(stdin, style, off, &sb);
204	}
205	exit(rval);
206}
207
208/*
209 * Convert the obsolete argument form into something that getopt can handle.
210 * This means that anything of the form [+-][0-9][0-9]*[lbc][fr] that isn't
211 * the option argument for a -b, -c or -n option gets converted.
212 */
213static void
214obsolete(argv)
215	char *argv[];
216{
217	register char *ap, *p, *t;
218	int len;
219	char *start;
220
221	while (ap = *++argv) {
222		/* Return if "--" or not an option of any form. */
223		if (ap[0] != '-') {
224			if (ap[0] != '+')
225				return;
226		} else if (ap[1] == '-')
227			return;
228
229		switch(*++ap) {
230		/* Old-style option. */
231		case '0': case '1': case '2': case '3': case '4':
232		case '5': case '6': case '7': case '8': case '9':
233
234			/* Malloc space for dash, new option and argument. */
235			len = strlen(*argv);
236			if ((start = p = malloc(len + 3)) == NULL)
237				err(1, "%s", strerror(errno));
238			*p++ = '-';
239
240			/*
241			 * Go to the end of the option argument.  Save off any
242			 * trailing options (-3lf) and translate any trailing
243			 * output style characters.
244			 */
245			t = *argv + len - 1;
246			if (*t == 'f' || *t == 'r') {
247				*p++ = *t;
248				*t-- = '\0';
249			}
250			switch(*t) {
251			case 'b':
252				*p++ = 'b';
253				*t = '\0';
254				break;
255			case 'c':
256				*p++ = 'c';
257				*t = '\0';
258				break;
259			case 'l':
260				*t = '\0';
261				/* FALLTHROUGH */
262			case '0': case '1': case '2': case '3': case '4':
263			case '5': case '6': case '7': case '8': case '9':
264				*p++ = 'n';
265				break;
266			default:
267				err(1, "illegal option -- %s", *argv);
268			}
269			*p++ = *argv[0];
270			(void)strcpy(p, ap);
271			*argv = start;
272			continue;
273
274		/*
275		 * Options w/ arguments, skip the argument and continue
276		 * with the next option.
277		 */
278		case 'b':
279		case 'c':
280		case 'n':
281			if (!ap[1])
282				++argv;
283			/* FALLTHROUGH */
284		/* Options w/o arguments, continue with the next option. */
285		case 'f':
286		case 'r':
287			continue;
288
289		/* Illegal option, return and let getopt handle it. */
290		default:
291			return;
292		}
293	}
294}
295
296static void
297usage()
298{
299	(void)fprintf(stderr,
300	    "usage: tail [-f | -r] [-b # | -c # | -n #] [file ...]\n");
301	exit(1);
302}
303