From 335ae486e4470591f629a30282e2605ec06d8a22 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 24 Oct 2006 22:55:41 +0200 Subject: inotail.c: Introduce relative tailing The global variable 'relative' states whether we're tailing relative to the end (R_END, default) or the begin of the file. Also do some minor comment reformatting. --- inotail.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'inotail.c') diff --git a/inotail.c b/inotail.c index 52b1e91..d118e8b 100644 --- a/inotail.c +++ b/inotail.c @@ -48,6 +48,9 @@ /* Print header with filename before tailing the file? */ static char verbose = 0; +/* Tailing relative to begin or end of file */ +static char relative = R_END; + /* Command line options */ static const struct option long_opts[] = { {"bytes", required_argument, NULL, 'c'}, @@ -100,12 +103,12 @@ static off_t lines_to_offset(struct file_struct *f, unsigned int n_lines) memset(&buf, 0, sizeof(buf)); - /* We also count the last \n */ - n_lines += 1; + if (relative == R_END) + n_lines += 1; /* We also count the last \n */ while (offset > 0 && n_lines > 0) { int i, rc; - int block_size = BUFFER_SIZE; /* Size of the current block we're reading */ + int block_size = BUFFER_SIZE; /* Size of the current block we're reading */ if (offset < BUFFER_SIZE) block_size = offset; @@ -136,7 +139,7 @@ static off_t lines_to_offset(struct file_struct *f, unsigned int n_lines) return offset; } -static int bytes_to_offset(struct file_struct *f, unsigned int n_bytes) +static off_t bytes_to_offset(struct file_struct *f, unsigned int n_bytes) { int ret = f->st_size - n_bytes; @@ -194,7 +197,7 @@ static int tail_file(struct file_struct *f, unsigned int n_units, char mode) if (mode == M_LINES) offset = lines_to_offset(f, n_units); - else /* Bytewise tail, n_lines is number of bytes here */ + else offset = bytes_to_offset(f, n_units); /* We only get negative offsets on errors */ @@ -329,7 +332,7 @@ int main(int argc, char **argv) int i, c, ret = 0; int n_files = 0; int n_units = DEFAULT_N_LINES; - char forever = 0, mode = M_LINES, relative = R_END; + char forever = 0, mode = M_LINES; char **filenames; struct file_struct *files; -- cgit v1.2.3-54-g00ecf