* 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.5 (Berkeley) %G%";
static void obsolete
__P((char **));
static void usage
__P((void));
while ((ch
= getopt(argc
, argv
, "b:c:fn:r")) != EOF
)
off
= strtol(num
= optarg
, &p
, 10) * 512;
err("illegal offset -- %s", optarg
);
style
= *num
== '+' ? FBYTES
: RBYTES
;
off
= strtol(num
= optarg
, &p
, 10);
err("illegal offset -- %s", optarg
);
style
= *num
== '+' ? FBYTES
: RBYTES
;
off
= strtol(num
= optarg
, &p
, 10);
err("illegal offset -- %s", optarg
);
style
= *num
== '+' ? FLINES
: RLINES
;
* Don't permit follow option if displaying in reverse. An offset
* with an explicit leading minus is meaningless.
if (style
&& *num
== '-')
err("illegal offset for -r option -- %s", num
);
if ((fp
= fopen(fname
, "r")) == NULL
)
if (fstat(fileno(fp
), &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(fp
), 0L, SEEK_CUR
) == -1 && errno
== ESPIPE
) {
fflag
= 0; /* POSIX.2 requires this. */
* Tail's options are weird. First, -n10 is the same as -n-10, not
* -n+10. Second, the number options for the -r option specify the
* number of bytes/chars/lines that get displayed, not the offset from
* the beginning/end of the file. Finally, the default for -r is the
* entire file, not 10 lines.
reverse(fp
, style
, off
, &sb
);
forward(fp
, 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("%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("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");