From 3699fc69e85357b6fb41d8469b96f5817ebc3cd9 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 31 Oct 2019 22:24:07 -0400 Subject: [PATCH] refactored fetch to have telemetry and added read functions --- inc/io.h | 2 ++ src/fetch.c | 26 +++++++++++--------------- src/lib/gapbuf.c | 9 +++------ src/lib/readfd.c | 12 ++++++++++++ src/lib/readfile.c | 21 +++++++++++++++++++++ 5 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 src/lib/readfd.c create mode 100644 src/lib/readfile.c diff --git a/inc/io.h b/inc/io.h index 82875e1..46e631f 100644 --- a/inc/io.h +++ b/inc/io.h @@ -3,3 +3,5 @@ */ void telem_send(char* fmt, ...); long writefd(int fd, char* data, long towrite); +long readfd(int fd, char* data, long toread); +char* readfile(char* path); diff --git a/src/fetch.c b/src/fetch.c index 15676aa..6bb2c7a 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -13,7 +13,6 @@ typedef struct { char* arg2; } Rule; -char* ARGV0; char* Matches[10]; #define FETCH_RULES @@ -152,13 +151,13 @@ bool exec(char* cmd) { if (pid == 0) { - runcmd(cmd); + runcmd(cmd); } else { - waitpid(pid, &status, 0); + waitpid(pid, &status, 0); ret = (status == 0); - } + } } return ret; } @@ -228,32 +227,29 @@ char* type2str(int type) int main(int argc, char** argv) { - int debug = 0; - OPTBEGIN { case 'd': debug = 1; } OPTEND; - + ARGV0 = *argv; if (!argc) usage(ARGV0); setenv("data", *argv, 1); - if (debug) - printf("Data: '%s'\n", *argv); + telem_send("FETCH(%s)\n", *argv); for (unsigned int i = 0; i < nelem(BuiltinRules); i++) { Rule* rule = BuiltinRules[i]; - if (debug) - printf("\nRuleset %d:\n", i); + telem_send("RULESET(%d)\n", i); + for (; rule->type != COMPLETE; rule++) { - if (debug) - printf(" %s '%s' '%s'\n", type2str(rule->type), rule->arg1, rule->arg2); + telem_send(" RULE(%s '%s' '%s')\n", type2str(rule->type), rule->arg1, rule->arg2); if (!apply_rule(rule)) { - if (debug) - printf(" FAIL\n"); break; } } if (rule->type == COMPLETE) + { + telem_send(" ACCEPT\n"); exit(0); + } } return 1; } diff --git a/src/lib/gapbuf.c b/src/lib/gapbuf.c index d1d046b..a5b915c 100644 --- a/src/lib/gapbuf.c +++ b/src/lib/gapbuf.c @@ -116,7 +116,7 @@ void gapbuf_load(GapBuf* buf, char* path) require(buf != NULL); require(path != NULL); /* load the contents from the file */ - int fd, nread; + int fd; struct stat sb = {0}; if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0)) { @@ -125,12 +125,9 @@ void gapbuf_load(GapBuf* buf, char* path) buf->bufsize = pagealign(sb.st_size); buf->bufstart = malloc(buf->bufsize); buf->bufend = buf->bufstart + buf->bufsize; - buf->gapstart = buf->bufstart; + buf->gapstart = buf->bufstart + sb.st_size; buf->gapend = buf->bufend; - - /* Read the file into the buffer */ - while (sb.st_size && (nread = read(fd, buf->gapstart, sb.st_size)) > 0) - buf->gapstart += nread, sb.st_size -= nread; + (void)readfd(fd, buf->bufstart, sb.st_size); } if (fd > 0) { diff --git a/src/lib/readfd.c b/src/lib/readfd.c new file mode 100644 index 0000000..5e4db43 --- /dev/null +++ b/src/lib/readfd.c @@ -0,0 +1,12 @@ +#include + +long readfd(int fd, char* data, long toread) +{ + long nread = 0; + while (toread && ((nread = read(fd, data, toread)) >= 0)) + { + data += nread; + toread -= nread; + } + return nread; +} diff --git a/src/lib/readfile.c b/src/lib/readfile.c new file mode 100644 index 0000000..4534f95 --- /dev/null +++ b/src/lib/readfile.c @@ -0,0 +1,21 @@ +#include +#include +#include + +char* readfile(char* path) +{ + int fd; + char* data = NULL; + struct stat sb = {0}; + if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0)) + { + data = malloc(sb.st_size+1); + (void)readfd(fd, data, sb.st_size); + data[sb.st_size] = '\0'; + } + if (fd > 0) + { + close(fd); + } + return data; +} -- 2.52.0