xref: /freebsd/usr.bin/tail/tail.c (revision 48a1ef22849bc50fdfcdbccfd1d44b116d14f50a)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1991, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Edward Sze-Tyan Wang.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
38247e7cb1SJeroen Ruigrok van der Werven static const char copyright[] =
399b50d902SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\
409b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
419b50d902SRodney W. Grimes #endif /* not lint */
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #ifndef lint
44247e7cb1SJeroen Ruigrok van der Werven #if 0
459b50d902SRodney W. Grimes static char sccsid[] = "@(#)tail.c	8.1 (Berkeley) 6/6/93";
46247e7cb1SJeroen Ruigrok van der Werven #endif
47247e7cb1SJeroen Ruigrok van der Werven static const char rcsid[] =
48247e7cb1SJeroen Ruigrok van der Werven   "$FreeBSD$";
499b50d902SRodney W. Grimes #endif /* not lint */
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include <sys/types.h>
529b50d902SRodney W. Grimes #include <sys/stat.h>
539b50d902SRodney W. Grimes #include <errno.h>
549b50d902SRodney W. Grimes #include <unistd.h>
559b50d902SRodney W. Grimes #include <stdio.h>
569b50d902SRodney W. Grimes #include <stdlib.h>
579b50d902SRodney W. Grimes #include <string.h>
58c3fd5728SPeter Wemm #include <err.h>
599b50d902SRodney W. Grimes #include "extern.h"
609b50d902SRodney W. Grimes 
61eb1c9439SPeter Wemm int Fflag, fflag, rflag, rval;
629b50d902SRodney W. Grimes char *fname;
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes static void obsolete __P((char **));
659b50d902SRodney W. Grimes static void usage __P((void));
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes int
689b50d902SRodney W. Grimes main(argc, argv)
699b50d902SRodney W. Grimes 	int argc;
709b50d902SRodney W. Grimes 	char *argv[];
719b50d902SRodney W. Grimes {
729b50d902SRodney W. Grimes 	struct stat sb;
739b50d902SRodney W. Grimes 	FILE *fp;
749b50d902SRodney W. Grimes 	long off;
759b50d902SRodney W. Grimes 	enum STYLE style;
769b50d902SRodney W. Grimes 	int ch, first;
779b50d902SRodney W. Grimes 	char *p;
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes 	/*
809b50d902SRodney W. Grimes 	 * Tail's options are weird.  First, -n10 is the same as -n-10, not
819b50d902SRodney W. Grimes 	 * -n+10.  Second, the number options are 1 based and not offsets,
829b50d902SRodney W. Grimes 	 * so -n+1 is the first line, and -c-1 is the last byte.  Third, the
839b50d902SRodney W. Grimes 	 * number options for the -r option specify the number of things that
849b50d902SRodney W. Grimes 	 * get displayed, not the starting point in the file.  The one major
859b50d902SRodney W. Grimes 	 * incompatibility in this version as compared to historical versions
869b50d902SRodney W. Grimes 	 * is that the 'r' option couldn't be modified by the -lbc options,
879b50d902SRodney W. Grimes 	 * i.e. it was always done in lines.  This version treats -rc as a
889b50d902SRodney W. Grimes 	 * number of characters in reverse order.  Finally, the default for
899b50d902SRodney W. Grimes 	 * -r is the entire file, not 10 lines.
909b50d902SRodney W. Grimes 	 */
919b50d902SRodney W. Grimes #define	ARG(units, forward, backward) {					\
929b50d902SRodney W. Grimes 	if (style)							\
939b50d902SRodney W. Grimes 		usage();						\
949b50d902SRodney W. Grimes 	off = strtol(optarg, &p, 10) * (units);				\
959b50d902SRodney W. Grimes 	if (*p)								\
96ac551270SPeter Wemm 		errx(1, "illegal offset -- %s", optarg);		\
979b50d902SRodney W. Grimes 	switch(optarg[0]) {						\
989b50d902SRodney W. Grimes 	case '+':							\
999b50d902SRodney W. Grimes 		if (off)						\
1009b50d902SRodney W. Grimes 			off -= (units);					\
1019b50d902SRodney W. Grimes 			style = (forward);				\
1029b50d902SRodney W. Grimes 		break;							\
1039b50d902SRodney W. Grimes 	case '-':							\
1049b50d902SRodney W. Grimes 		off = -off;						\
1059b50d902SRodney W. Grimes 		/* FALLTHROUGH */					\
1069b50d902SRodney W. Grimes 	default:							\
1079b50d902SRodney W. Grimes 		style = (backward);					\
1089b50d902SRodney W. Grimes 		break;							\
1099b50d902SRodney W. Grimes 	}								\
1109b50d902SRodney W. Grimes }
1119b50d902SRodney W. Grimes 
1129b50d902SRodney W. Grimes 	obsolete(argv);
1139b50d902SRodney W. Grimes 	style = NOTSET;
114eb1c9439SPeter Wemm 	while ((ch = getopt(argc, argv, "Fb:c:fn:r")) != -1)
1159b50d902SRodney W. Grimes 		switch(ch) {
116eb1c9439SPeter Wemm 		case 'F':	/* -F is superset of (and implies) -f */
117eb1c9439SPeter Wemm 			Fflag = fflag = 1;
118eb1c9439SPeter Wemm 			break;
1199b50d902SRodney W. Grimes 		case 'b':
1209b50d902SRodney W. Grimes 			ARG(512, FBYTES, RBYTES);
1219b50d902SRodney W. Grimes 			break;
1229b50d902SRodney W. Grimes 		case 'c':
1239b50d902SRodney W. Grimes 			ARG(1, FBYTES, RBYTES);
1249b50d902SRodney W. Grimes 			break;
1259b50d902SRodney W. Grimes 		case 'f':
1269b50d902SRodney W. Grimes 			fflag = 1;
1279b50d902SRodney W. Grimes 			break;
1289b50d902SRodney W. Grimes 		case 'n':
1299b50d902SRodney W. Grimes 			ARG(1, FLINES, RLINES);
1309b50d902SRodney W. Grimes 			break;
1319b50d902SRodney W. Grimes 		case 'r':
1329b50d902SRodney W. Grimes 			rflag = 1;
1339b50d902SRodney W. Grimes 			break;
1349b50d902SRodney W. Grimes 		case '?':
1359b50d902SRodney W. Grimes 		default:
1369b50d902SRodney W. Grimes 			usage();
1379b50d902SRodney W. Grimes 		}
1389b50d902SRodney W. Grimes 	argc -= optind;
1399b50d902SRodney W. Grimes 	argv += optind;
1409b50d902SRodney W. Grimes 
1419b50d902SRodney W. Grimes 	if (fflag && argc > 1)
142ac551270SPeter Wemm 		errx(1, "-f option only appropriate for a single file");
1439b50d902SRodney W. Grimes 
1449b50d902SRodney W. Grimes 	/*
1459b50d902SRodney W. Grimes 	 * If displaying in reverse, don't permit follow option, and convert
1469b50d902SRodney W. Grimes 	 * style values.
1479b50d902SRodney W. Grimes 	 */
1489b50d902SRodney W. Grimes 	if (rflag) {
1499b50d902SRodney W. Grimes 		if (fflag)
1509b50d902SRodney W. Grimes 			usage();
1519b50d902SRodney W. Grimes 		if (style == FBYTES)
1529b50d902SRodney W. Grimes 			style = RBYTES;
1539b50d902SRodney W. Grimes 		else if (style == FLINES)
1549b50d902SRodney W. Grimes 			style = RLINES;
1559b50d902SRodney W. Grimes 	}
1569b50d902SRodney W. Grimes 
1579b50d902SRodney W. Grimes 	/*
1589b50d902SRodney W. Grimes 	 * If style not specified, the default is the whole file for -r, and
1599b50d902SRodney W. Grimes 	 * the last 10 lines if not -r.
1609b50d902SRodney W. Grimes 	 */
1619ef5c48bSBill Fumerola 	if (style == NOTSET) {
1629b50d902SRodney W. Grimes 		if (rflag) {
1639b50d902SRodney W. Grimes 			off = 0;
1649b50d902SRodney W. Grimes 			style = REVERSE;
1659b50d902SRodney W. Grimes 		} else {
1669b50d902SRodney W. Grimes 			off = 10;
1679b50d902SRodney W. Grimes 			style = RLINES;
1689b50d902SRodney W. Grimes 		}
1699ef5c48bSBill Fumerola 	}
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes 	if (*argv)
1729b50d902SRodney W. Grimes 		for (first = 1; fname = *argv++;) {
1739b50d902SRodney W. Grimes 			if ((fp = fopen(fname, "r")) == NULL ||
1749b50d902SRodney W. Grimes 			    fstat(fileno(fp), &sb)) {
1759b50d902SRodney W. Grimes 				ierr();
1769b50d902SRodney W. Grimes 				continue;
1779b50d902SRodney W. Grimes 			}
1789b50d902SRodney W. Grimes 			if (argc > 1) {
1799b50d902SRodney W. Grimes 				(void)printf("%s==> %s <==\n",
1809b50d902SRodney W. Grimes 				    first ? "" : "\n", fname);
1819b50d902SRodney W. Grimes 				first = 0;
1829b50d902SRodney W. Grimes 				(void)fflush(stdout);
1839b50d902SRodney W. Grimes 			}
1849b50d902SRodney W. Grimes 
1859b50d902SRodney W. Grimes 			if (rflag)
1869b50d902SRodney W. Grimes 				reverse(fp, style, off, &sb);
1879b50d902SRodney W. Grimes 			else
1889b50d902SRodney W. Grimes 				forward(fp, style, off, &sb);
1899b50d902SRodney W. Grimes 			(void)fclose(fp);
1909b50d902SRodney W. Grimes 		}
1919b50d902SRodney W. Grimes 	else {
1929b50d902SRodney W. Grimes 		fname = "stdin";
1939b50d902SRodney W. Grimes 
1949b50d902SRodney W. Grimes 		if (fstat(fileno(stdin), &sb)) {
1959b50d902SRodney W. Grimes 			ierr();
1969b50d902SRodney W. Grimes 			exit(1);
1979b50d902SRodney W. Grimes 		}
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes 		/*
2009b50d902SRodney W. Grimes 		 * Determine if input is a pipe.  4.4BSD will set the SOCKET
2019b50d902SRodney W. Grimes 		 * bit in the st_mode field for pipes.  Fix this then.
2029b50d902SRodney W. Grimes 		 */
2039b50d902SRodney W. Grimes 		if (lseek(fileno(stdin), (off_t)0, SEEK_CUR) == -1 &&
2049b50d902SRodney W. Grimes 		    errno == ESPIPE) {
2059b50d902SRodney W. Grimes 			errno = 0;
2069b50d902SRodney W. Grimes 			fflag = 0;		/* POSIX.2 requires this. */
2079b50d902SRodney W. Grimes 		}
2089b50d902SRodney W. Grimes 
2099b50d902SRodney W. Grimes 		if (rflag)
2109b50d902SRodney W. Grimes 			reverse(stdin, style, off, &sb);
2119b50d902SRodney W. Grimes 		else
2129b50d902SRodney W. Grimes 			forward(stdin, style, off, &sb);
2139b50d902SRodney W. Grimes 	}
2149b50d902SRodney W. Grimes 	exit(rval);
2159b50d902SRodney W. Grimes }
2169b50d902SRodney W. Grimes 
2179b50d902SRodney W. Grimes /*
2189b50d902SRodney W. Grimes  * Convert the obsolete argument form into something that getopt can handle.
2199b50d902SRodney W. Grimes  * This means that anything of the form [+-][0-9][0-9]*[lbc][fr] that isn't
2209b50d902SRodney W. Grimes  * the option argument for a -b, -c or -n option gets converted.
2219b50d902SRodney W. Grimes  */
2229b50d902SRodney W. Grimes static void
2239b50d902SRodney W. Grimes obsolete(argv)
2249b50d902SRodney W. Grimes 	char *argv[];
2259b50d902SRodney W. Grimes {
22648a1ef22SJeroen Ruigrok van der Werven 	char *ap, *p, *t;
227beed3992SJeroen Ruigrok van der Werven 	size_t len;
2289b50d902SRodney W. Grimes 	char *start;
2299b50d902SRodney W. Grimes 
2309b50d902SRodney W. Grimes 	while (ap = *++argv) {
2319b50d902SRodney W. Grimes 		/* Return if "--" or not an option of any form. */
2329b50d902SRodney W. Grimes 		if (ap[0] != '-') {
2339b50d902SRodney W. Grimes 			if (ap[0] != '+')
2349b50d902SRodney W. Grimes 				return;
2359b50d902SRodney W. Grimes 		} else if (ap[1] == '-')
2369b50d902SRodney W. Grimes 			return;
2379b50d902SRodney W. Grimes 
2389b50d902SRodney W. Grimes 		switch(*++ap) {
2399b50d902SRodney W. Grimes 		/* Old-style option. */
2409b50d902SRodney W. Grimes 		case '0': case '1': case '2': case '3': case '4':
2419b50d902SRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
2429b50d902SRodney W. Grimes 
2439b50d902SRodney W. Grimes 			/* Malloc space for dash, new option and argument. */
2449b50d902SRodney W. Grimes 			len = strlen(*argv);
2459b50d902SRodney W. Grimes 			if ((start = p = malloc(len + 3)) == NULL)
246ac551270SPeter Wemm 				err(1, "malloc");
2479b50d902SRodney W. Grimes 			*p++ = '-';
2489b50d902SRodney W. Grimes 
2499b50d902SRodney W. Grimes 			/*
2509b50d902SRodney W. Grimes 			 * Go to the end of the option argument.  Save off any
2519b50d902SRodney W. Grimes 			 * trailing options (-3lf) and translate any trailing
2529b50d902SRodney W. Grimes 			 * output style characters.
2539b50d902SRodney W. Grimes 			 */
2549b50d902SRodney W. Grimes 			t = *argv + len - 1;
2559b50d902SRodney W. Grimes 			if (*t == 'f' || *t == 'r') {
2569b50d902SRodney W. Grimes 				*p++ = *t;
2579b50d902SRodney W. Grimes 				*t-- = '\0';
2589b50d902SRodney W. Grimes 			}
2599b50d902SRodney W. Grimes 			switch(*t) {
2609b50d902SRodney W. Grimes 			case 'b':
2619b50d902SRodney W. Grimes 				*p++ = 'b';
2629b50d902SRodney W. Grimes 				*t = '\0';
2639b50d902SRodney W. Grimes 				break;
2649b50d902SRodney W. Grimes 			case 'c':
2659b50d902SRodney W. Grimes 				*p++ = 'c';
2669b50d902SRodney W. Grimes 				*t = '\0';
2679b50d902SRodney W. Grimes 				break;
2689b50d902SRodney W. Grimes 			case 'l':
2699b50d902SRodney W. Grimes 				*t = '\0';
2709b50d902SRodney W. Grimes 				/* FALLTHROUGH */
2719b50d902SRodney W. Grimes 			case '0': case '1': case '2': case '3': case '4':
2729b50d902SRodney W. Grimes 			case '5': case '6': case '7': case '8': case '9':
2739b50d902SRodney W. Grimes 				*p++ = 'n';
2749b50d902SRodney W. Grimes 				break;
2759b50d902SRodney W. Grimes 			default:
276ac551270SPeter Wemm 				errx(1, "illegal option -- %s", *argv);
2779b50d902SRodney W. Grimes 			}
2789b50d902SRodney W. Grimes 			*p++ = *argv[0];
2799b50d902SRodney W. Grimes 			(void)strcpy(p, ap);
2809b50d902SRodney W. Grimes 			*argv = start;
2819b50d902SRodney W. Grimes 			continue;
2829b50d902SRodney W. Grimes 
2839b50d902SRodney W. Grimes 		/*
2849b50d902SRodney W. Grimes 		 * Options w/ arguments, skip the argument and continue
2859b50d902SRodney W. Grimes 		 * with the next option.
2869b50d902SRodney W. Grimes 		 */
2879b50d902SRodney W. Grimes 		case 'b':
2889b50d902SRodney W. Grimes 		case 'c':
2899b50d902SRodney W. Grimes 		case 'n':
2909b50d902SRodney W. Grimes 			if (!ap[1])
2919b50d902SRodney W. Grimes 				++argv;
2929b50d902SRodney W. Grimes 			/* FALLTHROUGH */
2939b50d902SRodney W. Grimes 		/* Options w/o arguments, continue with the next option. */
2949b50d902SRodney W. Grimes 		case 'f':
2959b50d902SRodney W. Grimes 		case 'r':
2969b50d902SRodney W. Grimes 			continue;
2979b50d902SRodney W. Grimes 
2989b50d902SRodney W. Grimes 		/* Illegal option, return and let getopt handle it. */
2999b50d902SRodney W. Grimes 		default:
3009b50d902SRodney W. Grimes 			return;
3019b50d902SRodney W. Grimes 		}
3029b50d902SRodney W. Grimes 	}
3039b50d902SRodney W. Grimes }
3049b50d902SRodney W. Grimes 
3059b50d902SRodney W. Grimes static void
3069b50d902SRodney W. Grimes usage()
3079b50d902SRodney W. Grimes {
3089b50d902SRodney W. Grimes 	(void)fprintf(stderr,
3099b50d902SRodney W. Grimes 	    "usage: tail [-f | -r] [-b # | -c # | -n #] [file ...]\n");
3109b50d902SRodney W. Grimes 	exit(1);
3119b50d902SRodney W. Grimes }
312