diff options
-rw-r--r-- | tailburst.c | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/tailburst.c b/tailburst.c index 4f4ce49..da72e38 100644 --- a/tailburst.c +++ b/tailburst.c @@ -7,6 +7,8 @@ #include <stdlib.h> #define BUFLEN 1024 +#define DEBUG 0 +#define pr_debug(fmt, args...) if (DEBUG) printf(fmt, ##args) struct getline_states { char *base; @@ -41,23 +43,27 @@ ssize_t fdgetline(int fd, struct getline_states *line) { if (str = strchr(line->cur, '\n')) { line->next = str+1; str[0] = '\0'; - return str - line->cur; + pr_debug("next already buffered, %p %p: %s\n", str, line->cur, line->cur); + return nread; } /* No such luck, move current to base and read what we can */ if (line->cur != line->base) { + pr_debug("moving cur: %p, base: %p, read: %zd, moving %zd\n", line->cur, line->base, line->read, line->cur - line->base); line->read -= line->cur - line->base; memmove(line->base, line->cur, line->read); line->cur = line->base; } while ((nread = read(fd, line->base + line->read, line->size - line->read)) > 0 ) { + pr_debug("read %d\n", nread); line->read += nread; /* Check if we have a full line now */ if (str = strchr(line->base, '\n')) { line->next = str+1; str[0] = '\0'; + pr_debug("found stuff, %p %p %p: %s\n", str, line->base, line->cur, line->cur); return str - line->base; } @@ -67,7 +73,9 @@ ssize_t fdgetline(int fd, struct getline_states *line) { if (line->base == NULL) return -ENOMEM; } + pr_debug("got here? %d %d\n", nread, errno); + line->next = line->cur; if (errno) return -errno; @@ -80,23 +88,24 @@ int main(int argc, char **argv) int flags, fd = 0, rc; size_t len = 0; ssize_t nread; - char buf[BUFLEN]; struct pollfd pollfd; struct getline_states line = { 0 }; + char *cur; /* make stream non-block */ - #if 0 flags = fcntl(fd, F_GETFL); flags |= O_NONBLOCK; fcntl(fd, F_SETFL, flags); - #endif pollfd.fd = fd; pollfd.events = POLLIN; - while ((nread = fdgetline(fd, &line)) > 0) { - if (poll(&pollfd, 1, 100) == 0) { - printf("%s\n", line.cur); - } + while (poll(&pollfd, 1, -1) > 0) { + cur = NULL; + while ((nread = fdgetline(fd, &line)) > 0) + cur = line.cur; + + if (cur) + printf("%s\n", cur); } return 0; |