From 4b51d764955cf09604fffadad9b9b88c72ab45db Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 12 Jun 2007 18:05:22 +0200 Subject: inotail.c: Cleanup Save us some lines --- inotail.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/inotail.c b/inotail.c index 075bb5d..68f75c0 100644 --- a/inotail.c +++ b/inotail.c @@ -81,10 +81,9 @@ static void usage(const int status) static inline void setup_file(struct file_struct *f) { - f->fd = -1; + f->fd = f->i_watch = -1; f->st_size = 0; f->ignore = 0; - f->i_watch = -1; } static void ignore_file(struct file_struct *f) @@ -209,10 +208,8 @@ static off_t bytes_to_offset(struct file_struct *f, unsigned long n_bytes) if (from_begin) { if (n_bytes > 0) offset = (off_t) n_bytes - 1; - } else { - if ((off_t) n_bytes < f->st_size) - offset = f->st_size - (off_t) n_bytes; - } + } else if ((off_t) n_bytes < f->st_size) + offset = f->st_size - (off_t) n_bytes; return offset; } @@ -382,19 +379,18 @@ static int watch_files(struct file_struct *files, int n_files) ssize_t len; int ev_idx = 0; - /* Keep trying in the case of EINTR (see below) */ + /* Keep trying in the case of EINTR */ for (;;) { len = read(ifd, buf, (n_files * INOTIFY_BUFLEN)); if (unlikely(len < 0)) { - if (errno == EINTR) { - /* Some form of signal, likely ^Z/fg's STOP and CONT interrupted the inotify read, retry */ + /* Some form of signal, likely ^Z/fg's STOP and CONT interrupted the inotify read, retry */ + if (errno == EINTR) continue; - } else { + else { fprintf(stderr, "Error: Could not read inotify events (%s)\n", strerror(errno)); exit(EXIT_FAILURE); } } - /* The read did succeed */ break; } -- cgit v1.2.3-54-g00ecf From 347124f77d9502a21652bff4868f7c53052a8fca Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 12 Jun 2007 18:32:18 +0200 Subject: inotail.c: Also handle EAGAIN in watch_files() --- inotail.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inotail.c b/inotail.c index 68f75c0..2790005 100644 --- a/inotail.c +++ b/inotail.c @@ -379,12 +379,12 @@ static int watch_files(struct file_struct *files, int n_files) ssize_t len; int ev_idx = 0; - /* Keep trying in the case of EINTR */ + /* Keep trying in the case of EINTR or EAGAIN*/ for (;;) { len = read(ifd, buf, (n_files * INOTIFY_BUFLEN)); if (unlikely(len < 0)) { /* Some form of signal, likely ^Z/fg's STOP and CONT interrupted the inotify read, retry */ - if (errno == EINTR) + if (errno == EINTR || errno == EAGAIN) continue; else { fprintf(stderr, "Error: Could not read inotify events (%s)\n", strerror(errno)); -- cgit v1.2.3-54-g00ecf From d440e6dd7cb3db1f0c7b6723c098c5c31c530919 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 15 Jun 2007 11:35:42 +0200 Subject: inotail.c: Simplify EINTR handling in watch_files() We don't need another loop, just continue the surrounding while() on EINTR/EAGAIN --- inotail.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/inotail.c b/inotail.c index 2790005..967a0fa 100644 --- a/inotail.c +++ b/inotail.c @@ -379,20 +379,15 @@ static int watch_files(struct file_struct *files, int n_files) ssize_t len; int ev_idx = 0; - /* Keep trying in the case of EINTR or EAGAIN*/ - for (;;) { - len = read(ifd, buf, (n_files * INOTIFY_BUFLEN)); - if (unlikely(len < 0)) { - /* Some form of signal, likely ^Z/fg's STOP and CONT interrupted the inotify read, retry */ - if (errno == EINTR || errno == EAGAIN) - continue; - else { - fprintf(stderr, "Error: Could not read inotify events (%s)\n", strerror(errno)); - exit(EXIT_FAILURE); - } + len = read(ifd, buf, (n_files * INOTIFY_BUFLEN)); + if (unlikely(len < 0)) { + /* Some form of signal, likely ^Z/fg's STOP and CONT interrupted the inotify read, retry */ + if (errno == EINTR || errno == EAGAIN) + continue; /* Keep trying */ + else { + fprintf(stderr, "Error: Could not read inotify events (%s)\n", strerror(errno)); + exit(EXIT_FAILURE); } - /* The read did succeed */ - break; } while (ev_idx < len) { -- cgit v1.2.3-54-g00ecf