Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / tail / tail.c
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  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)tail.c   8.1 (Berkeley) 6/6/93
38  * $FreeBSD: src/usr.bin/tail/tail.c,v 1.6.2.2 2001/12/19 20:29:31 iedowse Exp $
39  * $DragonFly: src/usr.bin/tail/tail.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
40  */
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <err.h>
50 #include "extern.h"
51
52 int Fflag, fflag, rflag, rval;
53 char *fname;
54
55 static void obsolete __P((char **));
56 static void usage __P((void));
57
58 int
59 main(argc, argv)
60         int argc;
61         char *argv[];
62 {
63         struct stat sb;
64         FILE *fp;
65         off_t off;
66         enum STYLE style;
67         int ch, first;
68         char *p;
69
70         /*
71          * Tail's options are weird.  First, -n10 is the same as -n-10, not
72          * -n+10.  Second, the number options are 1 based and not offsets,
73          * so -n+1 is the first line, and -c-1 is the last byte.  Third, the
74          * number options for the -r option specify the number of things that
75          * get displayed, not the starting point in the file.  The one major
76          * incompatibility in this version as compared to historical versions
77          * is that the 'r' option couldn't be modified by the -lbc options,
78          * i.e. it was always done in lines.  This version treats -rc as a
79          * number of characters in reverse order.  Finally, the default for
80          * -r is the entire file, not 10 lines.
81          */
82 #define ARG(units, forward, backward) {                                 \
83         if (style)                                                      \
84                 usage();                                                \
85         off = strtoll(optarg, &p, 10) * (units);                        \
86         if (*p)                                                         \
87                 errx(1, "illegal offset -- %s", optarg);                \
88         switch(optarg[0]) {                                             \
89         case '+':                                                       \
90                 if (off)                                                \
91                         off -= (units);                                 \
92                         style = (forward);                              \
93                 break;                                                  \
94         case '-':                                                       \
95                 off = -off;                                             \
96                 /* FALLTHROUGH */                                       \
97         default:                                                        \
98                 style = (backward);                                     \
99                 break;                                                  \
100         }                                                               \
101 }
102
103         obsolete(argv);
104         style = NOTSET;
105         while ((ch = getopt(argc, argv, "Fb:c:fn:r")) != -1)
106                 switch(ch) {
107                 case 'F':       /* -F is superset of (and implies) -f */
108                         Fflag = fflag = 1;
109                         break;
110                 case 'b':
111                         ARG(512, FBYTES, RBYTES);
112                         break;
113                 case 'c':
114                         ARG(1, FBYTES, RBYTES);
115                         break;
116                 case 'f':
117                         fflag = 1;
118                         break;
119                 case 'n':
120                         ARG(1, FLINES, RLINES);
121                         break;
122                 case 'r':
123                         rflag = 1;
124                         break;
125                 case '?':
126                 default:
127                         usage();
128                 }
129         argc -= optind;
130         argv += optind;
131
132         if (fflag && argc > 1)
133                 errx(1, "-f option only appropriate for a single file");
134
135         /*
136          * If displaying in reverse, don't permit follow option, and convert
137          * style values.
138          */
139         if (rflag) {
140                 if (fflag)
141                         usage();
142                 if (style == FBYTES)
143                         style = RBYTES;
144                 else if (style == FLINES)
145                         style = RLINES;
146         }
147
148         /*
149          * If style not specified, the default is the whole file for -r, and
150          * the last 10 lines if not -r.
151          */
152         if (style == NOTSET) {
153                 if (rflag) {
154                         off = 0;
155                         style = REVERSE;
156                 } else {
157                         off = 10;
158                         style = RLINES;
159                 }
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][Ffr] that isn't
211  * the option argument for a -b, -c or -n option gets converted.
212  */
213 static void
214 obsolete(argv)
215         char *argv[];
216 {
217         char *ap, *p, *t;
218         size_t 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, "malloc");
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 == '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                                 errx(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 'f':
287                 case 'r':
288                         continue;
289
290                 /* Illegal option, return and let getopt handle it. */
291                 default:
292                         return;
293                 }
294         }
295 }
296
297 static void
298 usage()
299 {
300         (void)fprintf(stderr,
301             "usage: tail [-F | -f | -r] [-b # | -c # | -n #] [file ...]\n");
302         exit(1);
303 }