#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1991, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
static const char sccsid[] = "@(#)tail.c 8.1 (Berkeley) 6/6/93";
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
int Fflag, fflag, qflag, rflag, rval, no_files;
const char *fname;
file_info_t *files;
static void obsolete(char **);
static void usage(void);
int
main(int argc, char *argv[])
{
struct stat sb;
FILE *fp;
off_t off;
enum STYLE style;
int i, ch, first;
file_info_t *file;
char *p;
#define ARG(units, forward, backward) { \
if (style) \
usage(); \
off = strtoll(optarg, &p, 10) * (units); \
if (*p) \
errx(1, "illegal offset -- %s", optarg); \
switch(optarg[0]) { \
case '+': \
if (off) \
off -= (units); \
style = (forward); \
break; \
case '-': \
off = -off; \
\
default: \
style = (backward); \
break; \
} \
}
obsolete(argv);
style = NOTSET;
while ((ch = getopt(argc, argv, "Fb:c:fn:qr")) != -1)
switch(ch) {
case 'F':
Fflag = fflag = 1;
break;
case 'b':
ARG(512, FBYTES, RBYTES);
break;
case 'c':
ARG(1, FBYTES, RBYTES);
break;
case 'f':
fflag = 1;
break;
case 'n':
ARG(1, FLINES, RLINES);
break;
case 'q':
qflag = 1;
break;
case 'r':
rflag = 1;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
no_files = argc ? argc : 1;
if (rflag) {
if (fflag)
usage();
if (style == FBYTES)
style = RBYTES;
else if (style == FLINES)
style = RLINES;
}
if (style == NOTSET) {
if (rflag) {
off = 0;
style = REVERSE;
} else {
off = 10;
style = RLINES;
}
}
if (fflag && !argc) {
if (lseek(fileno(stdin), (off_t)0, SEEK_CUR) == -1 &&
errno == ESPIPE) {
errno = 0;
fflag = 0;
}
}
if (fflag) {
files = (struct file_info *) malloc(no_files * sizeof(struct file_info));
if (! files)
err(1, "Couldn't malloc space for file descriptors.");
for (file = files; (fname = argc ? *argv++ : "stdin"); file++) {
file->file_name = malloc(strlen(fname)+1);
if (! file->file_name)
errx(1, "Couldn't malloc space for file name.");
strncpy(file->file_name, fname, strlen(fname)+1);
file->fp = argc ? fopen(file->file_name, "r") : stdin;
if (file->fp == NULL ||
fstat(fileno(file->fp), &file->st)) {
file->fp = NULL;
ierr();
continue;
}
if (!argc)
break;
}
follow(files, style, off);
for (i = 0, file = files; i < no_files; i++, file++) {
free(file->file_name);
}
free(files);
} else if (*argv) {
for (first = 1; (fname = *argv++);) {
if ((fp = fopen(fname, "r")) == NULL ||
fstat(fileno(fp), &sb)) {
ierr();
continue;
}
if (argc > 1 && !qflag) {
(void)printf("%s==> %s <==\n",
first ? "" : "\n", fname);
first = 0;
(void)fflush(stdout);
}
#ifdef __APPLE__
if (S_IFDIR == (sb.st_mode & S_IFMT))
continue;
#endif
if (rflag)
reverse(fp, style, off, &sb);
else
forward(fp, style, off, &sb);
}
} else {
fname = "stdin";
if (fstat(fileno(stdin), &sb)) {
ierr();
exit(1);
}
if (rflag)
reverse(stdin, style, off, &sb);
else
forward(stdin, style, off, &sb);
}
exit(rval);
}
static void
obsolete(char *argv[])
{
char *ap, *p, *t;
size_t len;
char *start;
while ((ap = *++argv)) {
if (ap[0] != '-') {
if (ap[0] != '+')
return;
} else if (ap[1] == '-')
return;
switch(*++ap) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
len = strlen(*argv);
if ((start = p = malloc(len + 3)) == NULL)
err(1, "malloc");
*p++ = '-';
t = *argv + len - 1;
if (*t == 'F' || *t == 'f' || *t == 'r') {
*p++ = *t;
*t-- = '\0';
}
switch(*t) {
case 'b':
*p++ = 'b';
*t = '\0';
break;
case 'c':
*p++ = 'c';
*t = '\0';
break;
case 'l':
*t = '\0';
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
*p++ = 'n';
break;
default:
errx(1, "illegal option -- %s", *argv);
}
*p++ = *argv[0];
(void)strcpy(p, ap);
*argv = start;
continue;
case 'b':
case 'c':
case 'n':
if (!ap[1])
++argv;
case 'F':
case 'f':
case 'r':
continue;
default:
return;
}
}
}
static void
usage(void)
{
(void)fprintf(stderr,
"usage: tail [-F | -f | -r] [-q] [-b # | -c # | -n #]"
" [file ...]\n");
exit(1);
}