diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2007-06-15 21:57:40 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2007-06-15 21:57:40 +0200 |
commit | c93ac5bc511623f1a7b0d8d741bbe2a837d927f7 (patch) | |
tree | 39c4b2de6b69ca6c4437693b5f86ae67b88774bc | |
parent | 47455d3bea4b2f2ed96a1e86fafd8801fe148292 (diff) |
inotail.c: Simplify tail_pipe_lines()
-rw-r--r-- | inotail.c | 28 |
1 files changed, 13 insertions, 15 deletions
@@ -231,6 +231,7 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) struct line_buf *first, *last, *tmp; ssize_t rc; unsigned long total_lines = 0; + const char *p; if (n_lines == 0) return 0; @@ -241,8 +242,6 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) tmp = emalloc(sizeof(struct line_buf)); while (1) { - const char *p; - if ((rc = read(f->fd, tmp->buf, BUFFER_SIZE)) <= 0) { if (rc < 0 && (errno == EINTR || errno == EAGAIN)) continue; @@ -283,11 +282,11 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) if (rc < 0) { fprintf(stderr, "Error: Could not read from %s\n", pretty_name(f->name)); - goto err_out; + goto out; } if (last->n_bytes == 0) - goto err_out; + goto out; /* Count incomplete lines */ if (last->buf[last->n_bytes - 1] != '\n') { @@ -299,19 +298,18 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) for (tmp = first; total_lines - tmp->n_lines > n_lines; tmp = tmp->next) total_lines -= tmp->n_lines; - { - char const *p = tmp->buf; - if (total_lines > n_lines) { - size_t j; - for (j = total_lines - n_lines; j; --j) { - p = memchr(p, '\n', tmp->buf + tmp->n_bytes - p); - ++p; - } - } + p = tmp->buf; - write(STDOUT_FILENO, p, tmp->buf + tmp->n_bytes - p); + if (total_lines > n_lines) { + size_t j; + for (j = total_lines - n_lines; j; --j) { + p = memchr(p, '\n', tmp->buf + tmp->n_bytes - p); + ++p; + } } + write(STDOUT_FILENO, p, tmp->buf + tmp->n_bytes - p); + for (tmp = tmp->next; tmp; tmp = tmp->next) if (write(STDOUT_FILENO, tmp->buf, tmp->n_bytes) <= 0) { /* e.g. when writing to a pipe which gets closed */ @@ -322,7 +320,7 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) rc = 0; -err_out: +out: while (first) { tmp = first->next; free(first); |