1209e49b2SChris Love /*
2209e49b2SChris Love * Copyright (c) 1991, 1993
3209e49b2SChris Love * The Regents of the University of California. All rights reserved.
4209e49b2SChris Love *
5209e49b2SChris Love * This code is derived from software contributed to Berkeley by
6209e49b2SChris Love * Edward Sze-Tyan Wang.
7209e49b2SChris Love *
8209e49b2SChris Love * Redistribution and use in source and binary forms, with or without
9209e49b2SChris Love * modification, are permitted provided that the following conditions
10209e49b2SChris Love * are met:
11209e49b2SChris Love * 1. Redistributions of source code must retain the above copyright
12209e49b2SChris Love * notice, this list of conditions and the following disclaimer.
13209e49b2SChris Love * 2. Redistributions in binary form must reproduce the above copyright
14209e49b2SChris Love * notice, this list of conditions and the following disclaimer in the
15209e49b2SChris Love * documentation and/or other materials provided with the distribution.
16209e49b2SChris Love * 4. Neither the name of the University nor the names of its contributors
17209e49b2SChris Love * may be used to endorse or promote products derived from this software
18209e49b2SChris Love * without specific prior written permission.
19209e49b2SChris Love *
20209e49b2SChris Love * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21209e49b2SChris Love * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22209e49b2SChris Love * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23209e49b2SChris Love * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24209e49b2SChris Love * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25209e49b2SChris Love * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26209e49b2SChris Love * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27209e49b2SChris Love * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28209e49b2SChris Love * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29209e49b2SChris Love * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30209e49b2SChris Love * SUCH DAMAGE.
31209e49b2SChris Love */
32209e49b2SChris Love
33a02e855fSCody Peter Mello /*
34a02e855fSCody Peter Mello * Copyright 2017, Joyent, Inc.
35a02e855fSCody Peter Mello */
36a02e855fSCody Peter Mello
37209e49b2SChris Love #include <sys/types.h>
38209e49b2SChris Love #include <sys/stat.h>
39209e49b2SChris Love
40a02e855fSCody Peter Mello #include <ctype.h>
41209e49b2SChris Love #include <err.h>
42209e49b2SChris Love #include <errno.h>
43209e49b2SChris Love #include <stdio.h>
44209e49b2SChris Love #include <stdlib.h>
45209e49b2SChris Love #include <string.h>
46209e49b2SChris Love #include <unistd.h>
47209e49b2SChris Love
48209e49b2SChris Love #include "extern.h"
49209e49b2SChris Love
50209e49b2SChris Love int Fflag, fflag, qflag, rflag, rval, no_files;
51209e49b2SChris Love
52209e49b2SChris Love file_info_t *files;
53209e49b2SChris Love
54209e49b2SChris Love static void obsolete(char **);
55209e49b2SChris Love static void usage(void);
56209e49b2SChris Love
57209e49b2SChris Love int
main(int argc,char * argv[])58209e49b2SChris Love main(int argc, char *argv[])
59209e49b2SChris Love {
60209e49b2SChris Love struct stat sb;
61209e49b2SChris Love const char *fn;
62209e49b2SChris Love FILE *fp;
63209e49b2SChris Love off_t off;
64209e49b2SChris Love enum STYLE style;
65209e49b2SChris Love int i, ch, first;
66209e49b2SChris Love file_info_t *file;
67209e49b2SChris Love char *p;
68209e49b2SChris Love
69209e49b2SChris Love /*
70209e49b2SChris Love * Tail's options are weird. First, -n10 is the same as -n-10, not
71209e49b2SChris Love * -n+10. Second, the number options are 1 based and not offsets,
72209e49b2SChris Love * so -n+1 is the first line, and -c-1 is the last byte. Third, the
73209e49b2SChris Love * number options for the -r option specify the number of things that
74209e49b2SChris Love * get displayed, not the starting point in the file. The one major
75209e49b2SChris Love * incompatibility in this version as compared to historical versions
76209e49b2SChris Love * is that the 'r' option couldn't be modified by the -lbc options,
77209e49b2SChris Love * i.e. it was always done in lines. This version treats -rc as a
78209e49b2SChris Love * number of characters in reverse order. Finally, the default for
79209e49b2SChris Love * -r is the entire file, not 10 lines.
80209e49b2SChris Love */
81209e49b2SChris Love #define ARG(units, forward, backward) { \
82209e49b2SChris Love if (style) \
83209e49b2SChris Love usage(); \
84209e49b2SChris Love off = strtoll(optarg, &p, 10) * (units); \
85209e49b2SChris Love if (*p) \
86209e49b2SChris Love errx(1, "illegal offset -- %s", optarg); \
87209e49b2SChris Love switch (optarg[0]) { \
88209e49b2SChris Love case '+': \
89209e49b2SChris Love if (off) \
90209e49b2SChris Love off -= (units); \
91*339cc970SToomas Soome style = (forward); \
92209e49b2SChris Love break; \
93209e49b2SChris Love case '-': \
94209e49b2SChris Love off = -off; \
95209e49b2SChris Love /* FALLTHROUGH */ \
96209e49b2SChris Love default: \
97209e49b2SChris Love style = (backward); \
98209e49b2SChris Love break; \
99209e49b2SChris Love } \
100209e49b2SChris Love }
101209e49b2SChris Love
102209e49b2SChris Love obsolete(argv);
103209e49b2SChris Love style = NOTSET;
104209e49b2SChris Love off = 0;
105209e49b2SChris Love while ((ch = getopt(argc, argv, "Fb:c:fn:qr")) != -1)
106209e49b2SChris Love switch (ch) {
107209e49b2SChris Love case 'F': /* -F is superset of (and implies) -f */
108209e49b2SChris Love Fflag = fflag = 1;
109209e49b2SChris Love break;
110209e49b2SChris Love case 'b':
111209e49b2SChris Love ARG(512, FBYTES, RBYTES);
112209e49b2SChris Love break;
113209e49b2SChris Love case 'c':
114209e49b2SChris Love ARG(1, FBYTES, RBYTES);
115209e49b2SChris Love break;
116209e49b2SChris Love case 'f':
117209e49b2SChris Love fflag = 1;
118209e49b2SChris Love break;
119209e49b2SChris Love case 'n':
120209e49b2SChris Love ARG(1, FLINES, RLINES);
121209e49b2SChris Love break;
122209e49b2SChris Love case 'q':
123209e49b2SChris Love qflag = 1;
124209e49b2SChris Love break;
125209e49b2SChris Love case 'r':
126209e49b2SChris Love rflag = 1;
127209e49b2SChris Love break;
128209e49b2SChris Love case '?':
129209e49b2SChris Love default:
130209e49b2SChris Love usage();
131209e49b2SChris Love }
132209e49b2SChris Love argc -= optind;
133209e49b2SChris Love argv += optind;
134209e49b2SChris Love
135209e49b2SChris Love no_files = argc ? argc : 1;
136209e49b2SChris Love
137209e49b2SChris Love /*
138209e49b2SChris Love * If displaying in reverse, don't permit follow option, and convert
139209e49b2SChris Love * style values.
140209e49b2SChris Love */
141209e49b2SChris Love if (rflag) {
142209e49b2SChris Love if (fflag)
143209e49b2SChris Love usage();
144209e49b2SChris Love if (style == FBYTES)
145209e49b2SChris Love style = RBYTES;
146209e49b2SChris Love else if (style == FLINES)
147209e49b2SChris Love style = RLINES;
148209e49b2SChris Love }
149209e49b2SChris Love
150209e49b2SChris Love /*
151209e49b2SChris Love * If style not specified, the default is the whole file for -r, and
152209e49b2SChris Love * the last 10 lines if not -r.
153209e49b2SChris Love */
154209e49b2SChris Love if (style == NOTSET) {
155209e49b2SChris Love if (rflag) {
156209e49b2SChris Love off = 0;
157209e49b2SChris Love style = REVERSE;
158209e49b2SChris Love } else {
159209e49b2SChris Love off = 10;
160209e49b2SChris Love style = RLINES;
161209e49b2SChris Love }
162209e49b2SChris Love }
163209e49b2SChris Love
164209e49b2SChris Love if (*argv && fflag) {
165209e49b2SChris Love files = (struct file_info *)malloc(no_files *
166209e49b2SChris Love sizeof (struct file_info));
167209e49b2SChris Love if (!files)
168209e49b2SChris Love err(1, "Couldn't malloc space for file descriptors.");
169209e49b2SChris Love
170209e49b2SChris Love for (file = files; (fn = *argv++); file++) {
171209e49b2SChris Love file->file_name = strdup(fn);
172209e49b2SChris Love if (! file->file_name)
173209e49b2SChris Love errx(1, "Couldn't malloc space for file name.");
174209e49b2SChris Love if ((file->fp = fopen(file->file_name, "r")) == NULL ||
175209e49b2SChris Love fstat(fileno(file->fp), &file->st)) {
176209e49b2SChris Love if (file->fp != NULL) {
177209e49b2SChris Love (void) fclose(file->fp);
178209e49b2SChris Love file->fp = NULL;
179209e49b2SChris Love }
180209e49b2SChris Love if (!Fflag || errno != ENOENT)
181209e49b2SChris Love ierr(file->file_name);
182209e49b2SChris Love }
183209e49b2SChris Love }
184209e49b2SChris Love follow(files, style, off);
185209e49b2SChris Love for (i = 0, file = files; i < no_files; i++, file++) {
186209e49b2SChris Love free(file->file_name);
187209e49b2SChris Love }
188209e49b2SChris Love free(files);
189209e49b2SChris Love } else if (*argv) {
190209e49b2SChris Love for (first = 1; (fn = *argv++); ) {
191209e49b2SChris Love if ((fp = fopen(fn, "r")) == NULL ||
192209e49b2SChris Love fstat(fileno(fp), &sb)) {
193209e49b2SChris Love ierr(fn);
194209e49b2SChris Love continue;
195209e49b2SChris Love }
196209e49b2SChris Love if (argc > 1 && !qflag) {
197209e49b2SChris Love (void) printf("%s==> %s <==\n",
198209e49b2SChris Love first ? "" : "\n", fn);
199209e49b2SChris Love first = 0;
200209e49b2SChris Love (void) fflush(stdout);
201209e49b2SChris Love }
202209e49b2SChris Love
203209e49b2SChris Love if (rflag)
204209e49b2SChris Love reverse(fp, fn, style, off, &sb);
205209e49b2SChris Love else
206209e49b2SChris Love forward(fp, fn, style, off, &sb);
207209e49b2SChris Love }
208209e49b2SChris Love } else {
209209e49b2SChris Love fn = "stdin";
210209e49b2SChris Love
211209e49b2SChris Love if (fstat(fileno(stdin), &sb)) {
212209e49b2SChris Love ierr(fn);
213209e49b2SChris Love exit(1);
214209e49b2SChris Love }
215209e49b2SChris Love
216209e49b2SChris Love /*
217209e49b2SChris Love * Determine if input is a pipe. 4.4BSD will set the SOCKET
218209e49b2SChris Love * bit in the st_mode field for pipes. Fix this then.
219209e49b2SChris Love */
220209e49b2SChris Love if (lseek(fileno(stdin), (off_t)0, SEEK_CUR) == -1 &&
221209e49b2SChris Love errno == ESPIPE) {
222209e49b2SChris Love errno = 0;
223209e49b2SChris Love fflag = 0; /* POSIX.2 requires this. */
224209e49b2SChris Love }
225209e49b2SChris Love
226209e49b2SChris Love if (rflag)
227209e49b2SChris Love reverse(stdin, fn, style, off, &sb);
228209e49b2SChris Love else
229209e49b2SChris Love forward(stdin, fn, style, off, &sb);
230209e49b2SChris Love }
231209e49b2SChris Love exit(rval);
232209e49b2SChris Love }
233209e49b2SChris Love
234a02e855fSCody Peter Mello static boolean_t
iscount(const char * ap)235a02e855fSCody Peter Mello iscount(const char *ap)
236a02e855fSCody Peter Mello {
237a02e855fSCody Peter Mello char c;
238a02e855fSCody Peter Mello
239a02e855fSCody Peter Mello if (ap == NULL) {
240a02e855fSCody Peter Mello return (B_FALSE);
241a02e855fSCody Peter Mello }
242a02e855fSCody Peter Mello
243a02e855fSCody Peter Mello c = ap[0];
244a02e855fSCody Peter Mello
245a02e855fSCody Peter Mello if (c == '+' || c == '-') {
246a02e855fSCody Peter Mello c = ap[1];
247a02e855fSCody Peter Mello }
248a02e855fSCody Peter Mello
249a02e855fSCody Peter Mello return (isdigit(c) ? B_TRUE : B_FALSE);
250a02e855fSCody Peter Mello }
251a02e855fSCody Peter Mello
252209e49b2SChris Love /*
253209e49b2SChris Love * Convert the obsolete argument form into something that getopt can handle.
254209e49b2SChris Love * This means that anything of the form [+-][0-9][0-9]*[lbc][Ffr] that isn't
255209e49b2SChris Love * the option argument for a -b, -c or -n option gets converted.
256209e49b2SChris Love */
257209e49b2SChris Love static void
obsolete(char * argv[])258209e49b2SChris Love obsolete(char *argv[])
259209e49b2SChris Love {
260209e49b2SChris Love char *ap, *p, *t;
261209e49b2SChris Love size_t len;
262209e49b2SChris Love char *start;
263209e49b2SChris Love
264209e49b2SChris Love while ((ap = *++argv)) {
265209e49b2SChris Love /* Return if "--" or not an option of any form. */
266209e49b2SChris Love if (ap[0] != '-') {
267209e49b2SChris Love if (ap[0] != '+')
268209e49b2SChris Love return;
269209e49b2SChris Love } else if (ap[1] == '-')
270209e49b2SChris Love return;
271209e49b2SChris Love
272209e49b2SChris Love switch (*++ap) {
273209e49b2SChris Love /* Old-style option. */
274209e49b2SChris Love case '0': case '1': case '2': case '3': case '4':
275209e49b2SChris Love case '5': case '6': case '7': case '8': case '9':
276209e49b2SChris Love
277209e49b2SChris Love /* Malloc space for dash, new option and argument. */
278209e49b2SChris Love len = strlen(*argv);
279209e49b2SChris Love if ((start = p = malloc(len + 3)) == NULL)
280209e49b2SChris Love err(1, "malloc");
281209e49b2SChris Love *p++ = '-';
282209e49b2SChris Love
283209e49b2SChris Love /*
284209e49b2SChris Love * Go to the end of the option argument. Save off any
285209e49b2SChris Love * trailing options (-3lf) and translate any trailing
286209e49b2SChris Love * output style characters.
287209e49b2SChris Love */
288209e49b2SChris Love t = *argv + len - 1;
289209e49b2SChris Love if (*t == 'F' || *t == 'f' || *t == 'r') {
290209e49b2SChris Love *p++ = *t;
291209e49b2SChris Love *t-- = '\0';
292209e49b2SChris Love }
293209e49b2SChris Love switch (*t) {
294209e49b2SChris Love case 'b':
295209e49b2SChris Love *p++ = 'b';
296209e49b2SChris Love *t = '\0';
297209e49b2SChris Love break;
298209e49b2SChris Love case 'c':
299209e49b2SChris Love *p++ = 'c';
300209e49b2SChris Love *t = '\0';
301209e49b2SChris Love break;
302209e49b2SChris Love case 'l':
303209e49b2SChris Love *t = '\0';
304209e49b2SChris Love /* FALLTHROUGH */
305209e49b2SChris Love case '0': case '1': case '2': case '3': case '4':
306209e49b2SChris Love case '5': case '6': case '7': case '8': case '9':
307209e49b2SChris Love *p++ = 'n';
308209e49b2SChris Love break;
309209e49b2SChris Love default:
310209e49b2SChris Love errx(1, "illegal option -- %s", *argv);
311209e49b2SChris Love }
312209e49b2SChris Love *p++ = *argv[0];
313209e49b2SChris Love (void) strcpy(p, ap);
314209e49b2SChris Love *argv = start;
315209e49b2SChris Love continue;
316209e49b2SChris Love
317209e49b2SChris Love /*
318209e49b2SChris Love * Legacy Solaris tail supports "+c" "-c", "+l", "-l",
319a02e855fSCody Peter Mello * "+b", and "-b" with a default value of 10. We need
320a02e855fSCody Peter Mello * to determine here whether or not a count has been
321a02e855fSCody Peter Mello * provided after the flag, and create a new, explicit
322a02e855fSCody Peter Mello * argument as appropriate. [+-]l isn't allowed to have
323a02e855fSCody Peter Mello * any numbers after it, but [+-][bc] can, potentially
324a02e855fSCody Peter Mello * in the next command-line argument. We therefore
325a02e855fSCody Peter Mello * handle them in two separate cases below.
326209e49b2SChris Love */
327a02e855fSCody Peter Mello case 'l':
328a02e855fSCody Peter Mello len = strlen(ap);
329a02e855fSCody Peter Mello start = NULL;
330a02e855fSCody Peter Mello
331a02e855fSCody Peter Mello if (len > 2) {
332a02e855fSCody Peter Mello errx(1, "illegal option -- %s", *argv);
333a02e855fSCody Peter Mello }
334a02e855fSCody Peter Mello
335a02e855fSCody Peter Mello /* The only characters following should be flags */
336a02e855fSCody Peter Mello if (len == 2 && !isalpha(ap[1])) {
337a02e855fSCody Peter Mello errx(1, "illegal option -- %s", *argv);
338a02e855fSCody Peter Mello }
339a02e855fSCody Peter Mello
340a02e855fSCody Peter Mello if (asprintf(&start, "-%sn%c10",
341a02e855fSCody Peter Mello ap + 1, *argv[0]) == -1) {
342a02e855fSCody Peter Mello err(1, "asprintf");
343a02e855fSCody Peter Mello }
344a02e855fSCody Peter Mello
345a02e855fSCody Peter Mello *argv = start;
346a02e855fSCody Peter Mello
347a02e855fSCody Peter Mello continue;
348209e49b2SChris Love case 'b':
349209e49b2SChris Love case 'c':
350a02e855fSCody Peter Mello len = strlen(ap);
351a02e855fSCody Peter Mello start = NULL;
352a02e855fSCody Peter Mello
353a02e855fSCody Peter Mello if (len == 1) {
354a02e855fSCody Peter Mello /*
355a02e855fSCody Peter Mello * The option is just the flag name. Check if
356a02e855fSCody Peter Mello * the next argument is a count, so we know
357a02e855fSCody Peter Mello * whether we need to default to 10.
358a02e855fSCody Peter Mello */
359a02e855fSCody Peter Mello if (iscount(argv[1])) {
360a02e855fSCody Peter Mello ++argv;
361a02e855fSCody Peter Mello continue;
362a02e855fSCody Peter Mello } else {
363a02e855fSCody Peter Mello if (asprintf(&start,
364a02e855fSCody Peter Mello "-%c%c10", ap[0], *argv[0]) == -1) {
365a02e855fSCody Peter Mello err(1, "asprintf");
366a02e855fSCody Peter Mello }
367a02e855fSCody Peter Mello }
368a02e855fSCody Peter Mello } else {
369a02e855fSCody Peter Mello /*
370a02e855fSCody Peter Mello * The option has characters following the c/b.
371a02e855fSCody Peter Mello * If the characters following the option are a
372a02e855fSCody Peter Mello * count, then we use those. This invocation is
373a02e855fSCody Peter Mello * only allowed when '-' is used.
374a02e855fSCody Peter Mello *
375a02e855fSCody Peter Mello * Otherwise, we need to honor the following
376a02e855fSCody Peter Mello * flags, and default to 10.
377a02e855fSCody Peter Mello */
378a02e855fSCody Peter Mello if (iscount(ap + 1)) {
379a02e855fSCody Peter Mello if (*argv[0] != '-') {
380a02e855fSCody Peter Mello errx(1, "illegal option -- %s",
381a02e855fSCody Peter Mello *argv);
382a02e855fSCody Peter Mello }
383a02e855fSCody Peter Mello
384a02e855fSCody Peter Mello if (asprintf(&start, "-%c%s",
385a02e855fSCody Peter Mello ap[0], ap + 1) == -1) {
386a02e855fSCody Peter Mello err(1, "asprintf");
387a02e855fSCody Peter Mello }
388a02e855fSCody Peter Mello } else {
389a02e855fSCody Peter Mello if (asprintf(&start, "-%s%c%c10",
390a02e855fSCody Peter Mello ap + 1, ap[0], *argv[0]) == -1) {
391a02e855fSCody Peter Mello err(1, "asprintf");
392a02e855fSCody Peter Mello }
393a02e855fSCody Peter Mello }
394209e49b2SChris Love }
395a02e855fSCody Peter Mello
396209e49b2SChris Love *argv = start;
397209e49b2SChris Love
398209e49b2SChris Love continue;
399209e49b2SChris Love /*
400209e49b2SChris Love * Options w/ arguments, skip the argument and continue
401209e49b2SChris Love * with the next option.
402209e49b2SChris Love */
403209e49b2SChris Love case 'n':
404209e49b2SChris Love if (!ap[1])
405209e49b2SChris Love ++argv;
406209e49b2SChris Love /* FALLTHROUGH */
407209e49b2SChris Love /* Options w/o arguments, continue with the next option. */
408209e49b2SChris Love case 'F':
409209e49b2SChris Love case 'f':
410209e49b2SChris Love case 'r':
411209e49b2SChris Love continue;
412209e49b2SChris Love
413209e49b2SChris Love /* Illegal option, return and let getopt handle it. */
414209e49b2SChris Love default:
415209e49b2SChris Love return;
416209e49b2SChris Love }
417209e49b2SChris Love }
418209e49b2SChris Love }
419209e49b2SChris Love
420209e49b2SChris Love static void
usage(void)421209e49b2SChris Love usage(void)
422209e49b2SChris Love {
423209e49b2SChris Love (void) fprintf(stderr,
424209e49b2SChris Love "usage: tail [-F | -f | -r] [-q] [-b # | -c # | -n #]"
425209e49b2SChris Love " [file ...]\n");
426209e49b2SChris Love exit(1);
427209e49b2SChris Love }
428