* Copyright (c) 1991 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)tail.c 5.11 (Berkeley) %G%";
static void obsolete
__P((char **));
static void usage
__P((void));
* Tail's options are weird. First, -n10 is the same as -n-10, not
* -n+10. Second, the number options are 1 based and not offsets,
* so -n+1 is the first line, and -c-1 is the last byte. Third, the
* number options for the -r option specify the number of things that
* get displayed, not the starting point in the file. The one major
* incompatibility in this version as compared to historical versions
* is that the 'r' option couldn't be modified by the -lbc options,
* i.e. it was always done in lines. This version treats -rc as a
* number of characters in reverse order. Finally, the default for
* -r is the entire file, not 10 lines.
#define ARG(units, forward, backward) { \
off = strtol(optarg, &p, 10) * (units); \
err(1, "illegal offset -- %s", optarg); \
while ((ch
= getopt(argc
, argv
, "b:c:fn:r")) != EOF
)
ARG(512, FBYTES
, RBYTES
);
err(1, "-f option only appropriate for a single file");
* If displaying in reverse, don't permit follow option, and convert
else if (style
== FLINES
)
* If style not specified, the default is the whole file for -r, and
* the last 10 lines if not -r.
for (first
= 1; fname
= *argv
++;) {
if ((fp
= fopen(fname
, "r")) == NULL
||
fstat(fileno(fp
), &sb
)) {
(void)printf("%s==> %s <==\n",
first
? "" : "\n", fname
);
reverse(fp
, style
, off
, &sb
);
forward(fp
, style
, off
, &sb
);
if (fstat(fileno(stdin
), &sb
)) {
* Determine if input is a pipe. 4.4BSD will set the SOCKET
* bit in the st_mode field for pipes. Fix this then.
if (lseek(fileno(stdin
), (off_t
)0, SEEK_CUR
) == -1 &&
fflag
= 0; /* POSIX.2 requires this. */
reverse(stdin
, style
, off
, &sb
);
forward(stdin
, style
, off
, &sb
);
* Convert the obsolete argument form into something that getopt can handle.
* This means that anything of the form [+-][0-9][0-9]*[lbc][fr] that isn't
* the option argument for a -b, -c or -n option gets converted.
register char *ap
, *p
, *t
;
/* Return if "--" or not an option of any form. */
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
/* Malloc space for dash, new option and argument. */
if ((start
= p
= malloc(len
+ 3)) == NULL
)
err(1, "%s", strerror(errno
));
* Go to the end of the option argument. Save off any
* trailing options (-3lf) and translate any trailing
* output style characters.
if (*t
== 'f' || *t
== 'r') {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
err(1, "illegal option -- %s", *argv
);
* Options w/ arguments, skip the argument and continue
/* Options w/o arguments, continue with the next option. */
/* Illegal option, return and let getopt handle it. */
"usage: tail [-f | -r] [-b # | -c # | -n #] [file ...]\n");