/* tail -- output last part of file(s)
Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Can display any amount of data, unlike the Unix version, which uses
a fixed size buffer and therefore can only deliver a limited number
-b Tail by N 512-byte blocks.
-c, --bytes=N[bkm] Tail by N bytes
[or 512-byte blocks, kilobytes, or megabytes].
-f, --follow Loop forever trying to read more characters at the
end of the file, on the assumption that the file
is growing. Ignored if reading from a pipe.
Cannot be used if more than one file is given.
-N, -l, -n, --lines=N Tail by N lines.
-q, --quiet, --silent Never print filename headers.
-v, --verbose Always print filename headers.
If a number (N) starts with a `+', begin printing with the Nth item
from the start of each file, instead of from the end.
Reads from standard input if no files are given or when a filename of
By default, filename headers are printed only more than one file
By default, prints the last 10 lines (tail -n 10).
Original version by Paul Rubin <phr@ocf.berkeley.edu>.
Extensions by David MacKenzie <djm@ai.mit.edu>. */
#define ISDIGIT(c) (isascii ((c)) && isdigit ((c)))
#define ISDIGIT(c) (isdigit ((c)))
/* Number of items to tail. */
#define DEFAULT_NUMBER 10
/* Size of atomic reads. */
#define BUFSIZE (512 * 8)
/* Number of bytes per item we are printing.
/* If nonzero, read from end of file until killed. */
/* If nonzero, count from start of file instead of end. */
/* If nonzero, print filename headers. */
/* When to print the filename banners. */
multiple_files
, always
, never
void error (int status
, int errnum
, char *message
, ...);
/* The name this program was run with. */
/* Nonzero if we have ever read standard input. */
struct option long_options
[] =
{"follow", 0, NULL
, 'f'},
{"silent", 0, NULL
, 'q'},
{"verbose", 0, NULL
, 'v'},
enum header_mode header_mode
= multiple_files
;
/* If from_start, the number of items to skip before printing; otherwise,
the number of items at the end of the file to print. Initially, -1
means the value has not been set. */
int c
; /* Option character. */
forever
= from_start
= print_headers
= 0;
&& ((argv
[1][0] == '-' && ISDIGIT (argv
[1][1]))
|| (argv
[1][0] == '+' && (ISDIGIT (argv
[1][1]) || argv
[1][1] == 0))))
/* Old option syntax: a dash or plus, one or more digits (zero digits
are acceptable with a plus), and one or more option letters. */
for (number
= 0, ++argv
[1]; ISDIGIT (*argv
[1]); ++argv
[1])
number
= number
* 10 + *argv
[1] - '0';
/* Parse any appended option letters. */
error (0, 0, "unrecognized option `-%c'", *argv
[1]);
/* Make the options we just parsed invisible to getopt. */
while ((c
= getopt_long (argc
, argv
, "c:n:fqv", long_options
, (int *) 0))
error (1, 0, "invalid number `%s'", optarg
);
/* To start printing with item `number' from the start of the file, skip
`number' - 1 items. `tail +0' is actually meaningless, but for Unix
compatibility it's treated the same as `tail +1'. */
if (optind
< argc
- 1 && forever
)
error (1, 0, "cannot follow the ends of multiple files");
if (header_mode
== always
|| (header_mode
== multiple_files
&& optind
< argc
- 1))
exit_status
|= tail_file ("-", number
);
for (; optind
< argc
; ++optind
)
exit_status
|= tail_file (argv
[optind
], number
);
if (have_read_stdin
&& close (0) < 0)
error (1, errno
, "write error");
/* Display the last NUMBER units of file FILENAME.
"-" for FILENAME means the standard input.
Return 0 if successful, 1 if an error occurred. */
tail_file (filename
, number
)
if (!strcmp (filename
, "-"))
filename
= "standard input";
return tail (filename
, 0, number
);
fd
= open (filename
, O_RDONLY
);
errors
= tail (filename
, fd
, number
);
error (0, errno
, "%s", filename
);
static int first_file
= 1;
write (1, filename
, strlen (filename
));
/* Display the last NUMBER units of file FILENAME, open for reading
Return 0 if successful, 1 if an error occurred. */
tail (filename
, fd
, number
)
return tail_bytes (filename
, fd
, number
);
return tail_lines (filename
, fd
, number
);
/* Display the last part of file FILENAME, open for reading in FD,
Return 0 if successful, 1 if an error occurred. */
tail_bytes (filename
, fd
, number
)
/* Use fstat instead of checking for errno == ESPIPE because
lseek doesn't work on some special files but doesn't return an
error (0, errno
, "%s", filename
);
if (S_ISREG (stats
.st_mode
))
lseek (fd
, number
, SEEK_SET
);
else if (start_bytes (filename
, fd
, number
))
dump_remainder (filename
, fd
);
if (S_ISREG (stats
.st_mode
))
if (lseek (fd
, 0L, SEEK_END
) <= number
)
/* The file is shorter than we want, or just the right size, so
lseek (fd
, 0L, SEEK_SET
);
/* The file is longer than we want, so go back. */
lseek (fd
, -number
, SEEK_END
);
dump_remainder (filename
, fd
);
return pipe_bytes (filename
, fd
, number
);
/* Display the last part of file FILENAME, open for reading on FD,
Return 0 if successful, 1 if an error occurred. */
tail_lines (filename
, fd
, number
)
error (0, errno
, "%s", filename
);
if (start_lines (filename
, fd
, number
))
dump_remainder (filename
, fd
);
if (S_ISREG (stats
.st_mode
))
length
= lseek (fd
, 0L, SEEK_END
);
if (length
!= 0 && file_lines (filename
, fd
, number
, length
))
dump_remainder (filename
, fd
);
return pipe_lines (filename
, fd
, number
);
/* Print the last NUMBER lines from the end of file FD.
Go backward through the file, reading `BUFSIZE' bytes at a time (except
probably the first), until we hit the start of the file or have
POS starts out as the length of the file (the offset of the last
Return 0 if successful, 1 if an error occurred. */
file_lines (filename
, fd
, number
, pos
)
int i
; /* Index into `buffer' for scanning. */
/* Set `bytes_read' to the size of the last, probably partial, buffer;
0 < `bytes_read' <= `BUFSIZE'. */
bytes_read
= pos
% BUFSIZE
;
/* Make `pos' a multiple of `BUFSIZE' (0 if the file is short), so that all
reads will be on block boundaries, which might increase efficiency. */
lseek (fd
, pos
, SEEK_SET
);
bytes_read
= read (fd
, buffer
, bytes_read
);
error (0, errno
, "%s", filename
);
/* Count the incomplete line on files that don't end with a newline. */
if (bytes_read
&& buffer
[bytes_read
- 1] != '\n')
/* Scan backward, counting the newlines in this bufferfull. */
for (i
= bytes_read
- 1; i
>= 0; i
--)
/* Have we counted the requested number of newlines yet? */
if (buffer
[i
] == '\n' && number
-- == 0)
/* If this newline wasn't the last character in the buffer,
print the text after it. */
write (1, &buffer
[i
+ 1], bytes_read
- (i
+ 1));
/* Not enough newlines in that bufferfull. */
/* Not enough lines in the file; print the entire file. */
lseek (fd
, 0L, SEEK_SET
);
lseek (fd
, pos
, SEEK_SET
);
while ((bytes_read
= read (fd
, buffer
, BUFSIZE
)) > 0);
error (0, errno
, "%s", filename
);
/* Print the last NUMBER lines from the end of the standard input,
open for reading as pipe FD.
Buffer the text as a linked list of LBUFFERs, adding them as needed.
Return 0 if successful, 1 if an error occured. */
pipe_lines (filename
, fd
, number
)
typedef struct linebuffer LBUFFER
;
LBUFFER
*first
, *last
, *tmp
;
int i
; /* Index into buffers. */
int total_lines
= 0; /* Total number of newlines in all buffers. */
first
= last
= (LBUFFER
*) malloc (sizeof (LBUFFER
));
first
->nbytes
= first
->nlines
= 0;
tmp
= (LBUFFER
*) malloc (sizeof (LBUFFER
));
/* Input is always read into a fresh buffer. */
while ((tmp
->nbytes
= read (fd
, tmp
->buffer
, BUFSIZE
)) > 0)
/* Count the number of newlines just read. */
for (i
= 0; i
< tmp
->nbytes
; i
++)
if (tmp
->buffer
[i
] == '\n')
total_lines
+= tmp
->nlines
;
/* If there is enough room in the last buffer read, just append the new
one to it. This is because when reading from a pipe, `nbytes' can
if (tmp
->nbytes
+ last
->nbytes
< BUFSIZE
)
bcopy (tmp
->buffer
, &last
->buffer
[last
->nbytes
], tmp
->nbytes
);
last
->nbytes
+= tmp
->nbytes
;
last
->nlines
+= tmp
->nlines
;
/* If there's not enough room, link the new buffer onto the end of
the list, then either free up the oldest buffer for the next
read if that would leave enough lines, or else malloc a new one.
Some compaction mechanism is possible but probably not
if (total_lines
- first
->nlines
> number
)
total_lines
-= first
->nlines
;
tmp
= (LBUFFER
*) malloc (sizeof (LBUFFER
));
error (0, errno
, "%s", filename
);
/* This prevents a core dump when the pipe contains no newlines. */
/* Count the incomplete line on files that don't end with a newline. */
if (last
->buffer
[last
->nbytes
- 1] != '\n')
/* Run through the list, printing lines. First, skip over unneeded
for (tmp
= first
; total_lines
- tmp
->nlines
> number
; tmp
= tmp
->next
)
total_lines
-= tmp
->nlines
;
/* Find the correct beginning, then print the rest of the file. */
if (total_lines
> number
)
/* Skip `total_lines' - `number' newlines. We made sure that
`total_lines' - `number' <= `tmp->nlines'. */
for (i
= total_lines
- number
; i
; --i
)
write (1, &tmp
->buffer
[i
], tmp
->nbytes
- i
);
for (tmp
= tmp
->next
; tmp
; tmp
= tmp
->next
)
write (1, tmp
->buffer
, tmp
->nbytes
);
/* Print the last NUMBER characters from the end of pipe FD.
This is a stripped down version of pipe_lines.
Return 0 if successful, 1 if an error occurred. */
pipe_bytes (filename
, fd
, number
)
typedef struct charbuffer CBUFFER
;
CBUFFER
*first
, *last
, *tmp
;
int i
; /* Index into buffers. */
int total_bytes
= 0; /* Total characters in all buffers. */
first
= last
= (CBUFFER
*) malloc (sizeof (CBUFFER
));
tmp
= (CBUFFER
*) malloc (sizeof (CBUFFER
));
/* Input is always read into a fresh buffer. */
while ((tmp
->nbytes
= read (fd
, tmp
->buffer
, BUFSIZE
)) > 0)
total_bytes
+= tmp
->nbytes
;
/* If there is enough room in the last buffer read, just append the new
one to it. This is because when reading from a pipe, `nbytes' can
if (tmp
->nbytes
+ last
->nbytes
< BUFSIZE
)
bcopy (tmp
->buffer
, &last
->buffer
[last
->nbytes
], tmp
->nbytes
);
last
->nbytes
+= tmp
->nbytes
;
/* If there's not enough room, link the new buffer onto the end of
the list, then either free up the oldest buffer for the next
read if that would leave enough characters, or else malloc a new
one. Some compaction mechanism is possible but probably not
if (total_bytes
- first
->nbytes
> number
)
total_bytes
-= first
->nbytes
;
tmp
= (CBUFFER
*) malloc (sizeof (CBUFFER
));
error (0, errno
, "%s", filename
);
/* Run through the list, printing characters. First, skip over unneeded
for (tmp
= first
; total_bytes
- tmp
->nbytes
> number
; tmp
= tmp
->next
)
total_bytes
-= tmp
->nbytes
;
/* Find the correct beginning, then print the rest of the file.
We made sure that `total_bytes' - `number' <= `tmp->nbytes'. */
if (total_bytes
> number
)
i
= total_bytes
- number
;
write (1, &tmp
->buffer
[i
], tmp
->nbytes
- i
);
for (tmp
= tmp
->next
; tmp
; tmp
= tmp
->next
)
write (1, tmp
->buffer
, tmp
->nbytes
);
/* Skip NUMBER characters from the start of pipe FD, and print
any extra characters that were read beyond that.
Return 1 on error, 0 if ok. */
start_bytes (filename
, fd
, number
)
while (number
> 0 && (bytes_read
= read (fd
, buffer
, BUFSIZE
)) > 0)
error (0, errno
, "%s", filename
);
write (1, &buffer
[bytes_read
+ number
], -number
);
/* Skip NUMBER lines at the start of file or pipe FD, and print
any extra characters that were read beyond that.
Return 1 on error, 0 if ok. */
start_lines (filename
, fd
, number
)
while (number
&& (bytes_read
= read (fd
, buffer
, BUFSIZE
)) > 0)
while (bytes_to_skip
< bytes_read
)
if (buffer
[bytes_to_skip
++] == '\n' && --number
== 0)
error (0, errno
, "%s", filename
);
else if (bytes_to_skip
< bytes_read
)
write (1, &buffer
[bytes_to_skip
], bytes_read
- bytes_to_skip
);
/* Display file FILENAME from the current position in FD
to the end. If `forever' is nonzero, keep reading from the
end of the file until killed. */
dump_remainder (filename
, fd
)
while ((bytes_read
= read (fd
, buffer
, BUFSIZE
)) > 0)
write (1, buffer
, bytes_read
);
error (1, errno
, "%s", filename
);
int arglen
= strlen (str
);
/* Convert STR, a string of ASCII digits, into an unsigned integer.
Return -1 if STR does not represent a valid unsigned integer. */
for (value
= 0; ISDIGIT (*str
); ++str
)
value
= value
* 10 + *str
- '0';
return *str
? -1 : value
;
Usage: %s [-c [+]N[bkm]] [-n [+]N] [-fqv] [--bytes=[+]N[bkm]] [--lines=[+]N]\n\
[--follow] [--quiet] [--silent] [--verbose] [file...]\n\
%s [{-,+}Nbcfklmqv] [file...]\n", program_name
, program_name
);
/* error.c -- error handler for noninteractive utilities
Copyright (C) 1990, 1991 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#define VA_START(args, lastarg) va_start(args, lastarg)
/* Print the program name and error message MESSAGE, which is a printf-style
format string with optional args.
If ERRNUM is nonzero, print its corresponding system error message.
Exit with status STATUS if it is nonzero. */
error (int status
, int errnum
, char *message
, ...)
extern char *program_name
;
fprintf (stderr
, "%s: ", program_name
);
va_start (args
, message
);
vfprintf (stderr
, message
, args
);
fprintf (stderr
, ": %s", strerror (errnum
));