From b50204eabab7a2f5f21f12e5668f7c8d954aa84a Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 11 Jun 2007 20:08:11 +0200 Subject: inotail.c/.h: Clean up a bit --- inotail.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'inotail.h') diff --git a/inotail.h b/inotail.h index 836c1de..8e0f30a 100644 --- a/inotail.h +++ b/inotail.h @@ -7,8 +7,8 @@ #ifndef _INOTAIL_H #define _INOTAIL_H -/* Number of items to tail. */ -#define DEFAULT_N_LINES 10 +#define BUFFER_SIZE 4096 +#define DEFAULT_N_LINES 10 /* Number of items to tail. */ /* tail modes */ enum { M_LINES, M_BYTES }; @@ -22,6 +22,14 @@ struct file_struct { int i_watch; /* Inotify watch associated with file_struct */ }; +/* struct for linked list of buffers/lines in tail_pipe_lines */ +struct line_buf { + char buf[BUFFER_SIZE]; + size_t n_lines; + size_t n_bytes; + struct line_buf *next; +}; + #define IS_PIPELIKE(mode) \ (S_ISFIFO(mode) || S_ISSOCK(mode)) -- cgit v1.2.3-54-g00ecf From 21fc4aa4615803896f28bfc8e0009504effdd690 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 18 Sep 2007 11:50:38 +0200 Subject: inotail.c: Use BUFSIZ and post-increment For consistency reasons mostly :-) --- inotail.c | 22 ++++++++++------------ inotail.h | 3 +-- 2 files changed, 11 insertions(+), 14 deletions(-) (limited to 'inotail.h') diff --git a/inotail.c b/inotail.c index fdb2919..02cf462 100644 --- a/inotail.c +++ b/inotail.c @@ -94,7 +94,7 @@ static inline void setup_file(struct file_struct *f) { f->fd = f->i_watch = -1; f->size = 0; - f->blksize = BUFFER_SIZE; + f->blksize = BUFSIZ; f->ignore = 0; } @@ -256,7 +256,7 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) tmp = emalloc(sizeof(struct line_buf)); while (1) { - if ((rc = read(f->fd, tmp->buf, BUFFER_SIZE)) <= 0) { + if ((rc = read(f->fd, tmp->buf, BUFSIZ)) <= 0) { if (rc < 0 && (errno == EINTR || errno == EAGAIN)) continue; else @@ -269,15 +269,15 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) /* Count the lines in the current buffer */ while ((p = memchr(p, '\n', tmp->buf + rc - p))) { - ++p; - ++tmp->n_lines; + p++; + tmp->n_lines++; } total_lines += tmp->n_lines; /* Try to append to the previous buffer if there's enough free * space */ - if (tmp->n_bytes + last->n_bytes < BUFFER_SIZE) { + if (tmp->n_bytes + last->n_bytes < BUFSIZ) { memcpy(&last->buf[last->n_bytes], tmp->buf, tmp->n_bytes); last->n_bytes += tmp->n_bytes; last->n_lines += tmp->n_lines; @@ -304,8 +304,8 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) /* Count incomplete lines */ if (last->buf[last->n_bytes - 1] != '\n') { - ++last->n_lines; - ++total_lines; + last->n_lines++; + total_lines++; } /* Skip unneeded buffers */ @@ -315,10 +315,10 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) p = tmp->buf; if (total_lines > n_lines) { - size_t j; + unsigned long j; for (j = total_lines - n_lines; j; --j) { p = memchr(p, '\n', tmp->buf + tmp->n_bytes - p); - ++p; + p++; } } @@ -330,13 +330,11 @@ static ssize_t tail_pipe_lines(struct file_struct *f, unsigned long n_lines) for (tmp = tmp->next; tmp; tmp = tmp->next) if ((rc = write(STDOUT_FILENO, tmp->buf, tmp->n_bytes)) <= 0) { - /* e.g. when writing to a pipe which gets closed */ fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno)); goto out; } rc = 0; - out: while (first) { tmp = first->next; @@ -351,7 +349,7 @@ out: static ssize_t tail_pipe_bytes(struct file_struct *f, unsigned long n_bytes) { ssize_t rc; - char buf[BUFFER_SIZE]; + char buf[BUFSIZ]; /* We will just tail everything here */ while ((rc = read(f->fd, buf, f->blksize)) > 0) { diff --git a/inotail.h b/inotail.h index dcc948d..f9eabba 100644 --- a/inotail.h +++ b/inotail.h @@ -9,7 +9,6 @@ #include -#define BUFFER_SIZE 4096 #define DEFAULT_N_LINES 10 /* Number of items to tail. */ /* tail modes */ @@ -27,7 +26,7 @@ struct file_struct { /* struct for linked list of buffers/lines in tail_pipe_lines */ struct line_buf { - char buf[BUFFER_SIZE]; + char buf[BUFSIZ]; size_t n_lines; size_t n_bytes; struct line_buf *next; -- cgit v1.2.3-54-g00ecf From 8787c5e9f422ae6a9d3e3f5445301f753c3fe02d Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 18 Sep 2007 13:28:59 +0200 Subject: inotail.c: Implement tail_pipe_bytes() For now more or less a copy from coreutils --- inotail.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- inotail.h | 7 ++++++ 2 files changed, 77 insertions(+), 9 deletions(-) (limited to 'inotail.h') diff --git a/inotail.c b/inotail.c index 02cf462..cd96ec1 100644 --- a/inotail.c +++ b/inotail.c @@ -345,23 +345,84 @@ out: return rc; } -/* TODO: Implement me :) */ static ssize_t tail_pipe_bytes(struct file_struct *f, unsigned long n_bytes) { + struct char_buf *first, *last, *tmp; ssize_t rc; - char buf[BUFSIZ]; + unsigned long total_bytes = 0; + unsigned long i = 0; /* Index into buffer */ - /* We will just tail everything here */ - while ((rc = read(f->fd, buf, f->blksize)) > 0) { - if (write(STDOUT_FILENO, buf, (size_t) rc) <= 0) { - /* e.g. when writing to a pipe which gets closed */ + if (n_bytes == 0) + return 0; + + first = last = emalloc(sizeof(struct char_buf)); + first->n_bytes = 0; + first->next = NULL; + tmp = emalloc(sizeof(struct char_buf)); + + while(1) { + if ((rc = read(f->fd, tmp->buf, BUFSIZ)) <= 0) { + if (rc < 0 && (errno == EINTR || errno == EAGAIN)) + continue; + else + break; + } + total_bytes += rc; + tmp->n_bytes = rc; + tmp->next = NULL; + + /* Try to append to the previous buffer if there's enough free + * space + */ + if (tmp->n_bytes + last->n_bytes < BUFSIZ) { + memcpy(&last->buf[last->n_bytes], tmp->buf, tmp->n_bytes); + last->n_bytes += tmp->n_bytes; + } else { + last = last->next = tmp; + if (total_bytes - first->n_bytes > n_bytes) { + tmp = first; + total_bytes -= first->n_bytes; + first = first->next; + } else + tmp = emalloc(sizeof(struct char_buf)); + } + } + + free(tmp); + + if (rc < 0) { + fprintf(stderr, "Error: Could not read from %s\n", pretty_name(f->name)); + goto out; + } + + /* Skip unneeded buffers */ + for (tmp = first; total_bytes - tmp->n_bytes > n_bytes; tmp = tmp->next) + total_bytes -= tmp->n_bytes; + + if (total_bytes > n_bytes) + i = total_bytes - n_bytes; + + if ((rc = write(STDOUT_FILENO, &tmp->buf[i], tmp->n_bytes - i)) <= 0) { + /* e.g. when writing to a pipe which gets closed */ + fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno)); + goto out; + } + + for (tmp = tmp->next; tmp; tmp = tmp->next) + if ((rc = write(STDOUT_FILENO, tmp->buf, tmp->n_bytes)) <= 0) { fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno)); - rc = -1; - break; + goto out; } + + + rc = 0; +out: + while (first) { + tmp = first->next; + free(first); + first = tmp; } - free(buf); return rc; } diff --git a/inotail.h b/inotail.h index f9eabba..5029540 100644 --- a/inotail.h +++ b/inotail.h @@ -32,6 +32,13 @@ struct line_buf { struct line_buf *next; }; +/* struct for linked list of byte buffers in tail_pipe_bytes */ +struct char_buf { + char buf[BUFSIZ]; + size_t n_bytes; + struct char_buf *next; +}; + #define IS_PIPELIKE(mode) \ (S_ISFIFO(mode) || S_ISSOCK(mode)) -- cgit v1.2.3-54-g00ecf