From 95ba78a6cad807ba62901b9e29ce1bed4d18fc0e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 5 Oct 2006 00:51:54 +0200 Subject: Better file type checking and more meaningful return values --- inotail.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'inotail.c') diff --git a/inotail.c b/inotail.c index d3a0246..cd956d3 100644 --- a/inotail.c +++ b/inotail.c @@ -41,6 +41,10 @@ #define PROGRAM_NAME "inotail" #define BUFFER_SIZE 4096 +/* (ino)tail works on these file types */ +#define IS_TAILABLE(mode) \ + (S_ISREG(mode) || S_ISFIFO(mode) || S_ISSOCK(mode) || S_ISCHR(mode)) + /* Print header with filename before tailing the file? */ static char verbose = 0; @@ -150,7 +154,7 @@ static int tail_pipe(struct file_struct *f) while ((rc = read(f->fd, &buf, BUFFER_SIZE)) > 0) write(STDOUT_FILENO, buf, (size_t) rc); - return 0; + return rc; } static int tail_file(struct file_struct *f, int n_lines, char mode) @@ -176,8 +180,13 @@ static int tail_file(struct file_struct *f, int n_lines, char mode) return -1; } + if (!IS_TAILABLE(finfo.st_mode)) { + fprintf(stderr, "Error: '%s' of unsupported file type\n", f->name); + return -1; + } + /* We cannot seek on these */ - if (!S_ISREG(finfo.st_mode) || f->fd == STDIN_FILENO) + if (S_ISFIFO(finfo.st_mode) || S_ISSOCK(finfo.st_mode) || f->fd == STDIN_FILENO) return tail_pipe(f); f->st_size = finfo.st_size; @@ -307,7 +316,7 @@ static int watch_files(struct file_struct *f, int n_files) close(ifd); - return -1; + return n_ignored; } int main(int argc, char **argv) -- cgit v1.2.3-54-g00ecf