xref: /netbsd-src/usr.bin/tail/tail.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * 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
38 char copyright[] =
39 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
40  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 /*static char sccsid[] = "from: @(#)tail.c	5.7 (Berkeley) 2/12/92";*/
45 static char rcsid[] = "$Id: tail.c,v 1.3 1994/04/24 20:19:19 deraadt Exp $";
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <errno.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "extern.h"
56 
57 int fflag, rflag, rval;
58 char *fname;
59 
60 static void obsolete __P((char **));
61 static void usage __P((void));
62 
63 main(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;
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("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 	/*
134 	 * If displaying in reverse, don't permit follow option, and convert
135 	 * style values.
136 	 */
137 	if (rflag) {
138 		if (fflag)
139 			usage();
140 		if (style == FBYTES)
141 			style = RBYTES;
142 		if (style == FLINES)
143 			style = RLINES;
144 	}
145 
146 	/*
147 	 * If style not specified, the default is the whole file for -r, and
148 	 * the last 10 lines if not -r.
149 	 */
150 	if (style == NOTSET)
151 		if (rflag) {
152 			off = 0;
153 			style = REVERSE;
154 		} else {
155 			off = 10;
156 			style = RLINES;
157 		}
158 
159 	if (fname = *argv) {
160 		if ((fp = fopen(fname, "r")) == NULL)
161 			ierr();
162 	} else {
163 		fp = stdin;
164 		fname = "stdin";
165 	}
166 
167 	if (fstat(fileno(fp), &sb))
168 		ierr();
169 
170 	/*
171 	 * Determine if input is a pipe.  4.4BSD will set the SOCKET
172 	 * bit in the st_mode field for pipes.  Fix this then.
173 	 */
174 	if (lseek(fileno(fp), 0, SEEK_CUR) == -1 && errno == ESPIPE) {
175 		errno = 0;
176 		fflag = 0;		/* POSIX.2 requires this. */
177 	}
178 
179 	if (rflag)
180 		reverse(fp, style, off, &sb);
181 	else
182 		forward(fp, style, off, &sb);
183 	exit(rval);
184 }
185 
186 /*
187  * Convert the obsolete argument form into something that getopt can handle.
188  * This means that anything of the form [+-][0-9][0-9]*[lbc][fr] that isn't
189  * the option argument for a -b, -c or -n option gets converted.
190  */
191 static void
192 obsolete(argv)
193 	char **argv;
194 {
195 	register char *ap, *p, *t;
196 	int len;
197 	char *start;
198 
199 	while (ap = *++argv) {
200 		/* Return if "--" or not an option of any form. */
201 		if (ap[0] != '-') {
202 			if (ap[0] != '+')
203 				return;
204 		} else if (ap[1] == '-')
205 			return;
206 
207 		switch(*++ap) {
208 		/* Old-style option. */
209 		case '0': case '1': case '2': case '3': case '4':
210 		case '5': case '6': case '7': case '8': case '9':
211 
212 			/* Malloc space for dash, new option and argument. */
213 			len = strlen(*argv);
214 			if ((start = p = malloc(len + 3)) == NULL)
215 				err("%s", strerror(errno));
216 			*p++ = '-';
217 
218 			/*
219 			 * Go to the end of the option argument.  Save off any
220 			 * trailing options (-3lf) and translate any trailing
221 			 * output style characters.
222 			 */
223 			t = *argv + len - 1;
224 			if (*t == 'f' || *t == 'r') {
225 				*p++ = *t;
226 				*t-- = '\0';
227 			}
228 			switch(*t) {
229 			case 'b':
230 				*p++ = 'b';
231 				*t = '\0';
232 				break;
233 			case 'c':
234 				*p++ = 'c';
235 				*t = '\0';
236 				break;
237 			case 'l':
238 				*t = '\0';
239 				/* FALLTHROUGH */
240 			case '0': case '1': case '2': case '3': case '4':
241 			case '5': case '6': case '7': case '8': case '9':
242 				*p++ = 'n';
243 				break;
244 			default:
245 				err("illegal option -- %s", *argv);
246 			}
247 			*p++ = *argv[0];
248 			(void)strcpy(p, ap);
249 			*argv = start;
250 			continue;
251 
252 		/*
253 		 * Options w/ arguments, skip the argument and continue
254 		 * with the next option.
255 		 */
256 		case 'b':
257 		case 'c':
258 		case 'n':
259 			if (!ap[1])
260 				++argv;
261 			/* FALLTHROUGH */
262 		/* Options w/o arguments, continue with the next option. */
263 		case 'f':
264 		case 'r':
265 			continue;
266 
267 		/* Illegal option, return and let getopt handle it. */
268 		default:
269 			return;
270 		}
271 	}
272 }
273 
274 static void
275 usage()
276 {
277 	(void)fprintf(stderr,
278 	    "usage: tail [-f | -r] [-b # | -c # | -n #] [file]\n");
279 	exit(1);
280 }
281