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 From 66911157cfad3ccb5e24c11c77d3b0ff3252abd3 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sat, 1 Mar 2008 12:00:59 +0100 Subject: Move all #define's except PROGRAM_NAME to inotail.h --- inotail.c | 6 ------ inotail.h | 17 +++++++++++------ 2 files changed, 11 insertions(+), 12 deletions(-) (limited to 'inotail.h') diff --git a/inotail.c b/inotail.c index 24c1404..c19ed4f 100644 --- a/inotail.c +++ b/inotail.c @@ -35,20 +35,14 @@ #include "inotify.h" #include "inotify-syscalls.h" - #include "inotail.h" #define PROGRAM_NAME "inotail" -#define DEFAULT_BUFFER_SIZE 4096 -/* inotify event buffer length for one file */ -#define INOTIFY_BUFLEN (4 * sizeof(struct inotify_event)) /* Print header with filename before tailing the file? */ static char verbose = 0; - /* Tailing relative to begin or end of file */ static char from_begin = 0; - /* Number of ignored files */ static int n_ignored = 0; diff --git a/inotail.h b/inotail.h index aa2a0fa..ff77b26 100644 --- a/inotail.h +++ b/inotail.h @@ -8,10 +8,15 @@ #define _INOTAIL_H #include +#include "inotify.h" /* Number of items to tail. */ #define DEFAULT_N_LINES 10 +#define DEFAULT_BUFFER_SIZE 4096 +/* inotify event buffer length for one file */ +#define INOTIFY_BUFLEN (4 * sizeof(struct inotify_event)) + /* tail modes */ typedef enum { M_LINES, M_BYTES } mode_t; @@ -34,16 +39,16 @@ struct file_struct { #define is_digit(c) ((c) >= '0' && (c) <= '9') -#ifdef DEBUG -# define dprintf(fmt, args...) fprintf(stderr, fmt, ##args) -#else -# define dprintf(fmt, args...) -#endif /* DEBUG */ - #ifdef __GNUC__ # define unlikely(x) __builtin_expect(!!(x), 0) #else # define unlikely(x) (x) #endif /* __GNUC__ */ +#ifdef DEBUG +# define dprintf(fmt, args...) fprintf(stderr, fmt, ##args) +#else +# define dprintf(fmt, args...) +#endif /* DEBUG */ + #endif /* _INOTAIL_H */ -- cgit v1.2.3-54-g00ecf From 164d300c2d4d88f2bd787a0c56dc34b3ebb47073 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sat, 10 May 2008 11:09:02 +0200 Subject: Resolve some merge conflicts. --- inotail.c | 4 ++-- inotail.h | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'inotail.h') diff --git a/inotail.c b/inotail.c index 4195e13..d708f1f 100644 --- a/inotail.c +++ b/inotail.c @@ -57,7 +57,7 @@ static const struct option long_opts[] = { { NULL, 0, NULL, 0 } }; -static void *emalloc(size_t size) +static void *emalloc(const size_t size) { void *ret = malloc(size); @@ -698,7 +698,7 @@ int main(int argc, char **argv) int i, c, ret = 0; int n_files; unsigned long n_units = DEFAULT_N_LINES; - mode_t mode = M_LINES; + char mode = M_LINES; char forever = 0; char **filenames; struct file_struct *files = NULL; diff --git a/inotail.h b/inotail.h index ea680cb..4c1ca4d 100644 --- a/inotail.h +++ b/inotail.h @@ -12,12 +12,11 @@ #define DEFAULT_N_LINES 10 /* Number of items to tail. */ -#define DEFAULT_BUFFER_SIZE 4096 /* inotify event buffer length for one file */ #define INOTIFY_BUFLEN (4 * sizeof(struct inotify_event)) /* tail modes */ -typedef enum { M_LINES, M_BYTES } mode_t; +enum tail_mode { M_LINES, M_BYTES }; /* Every tailed file is represented as a file_struct */ struct file_struct { -- cgit v1.2.3-54-g00ecf