diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2006-10-05 00:51:54 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2006-10-05 00:51:54 +0200 |
commit | 95ba78a6cad807ba62901b9e29ce1bed4d18fc0e (patch) | |
tree | fb6ee6215eb1789422e0d7dcb27f9ce1234bfc51 | |
parent | b65331d6722fead2994d1db813b26a13c639002e (diff) |
Better file type checking and more meaningful return values
-rw-r--r-- | inotail.c | 15 |
1 files changed, 12 insertions, 3 deletions
@@ -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) |