*/
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);
char* arg2;
} Rule;
-char* ARGV0;
char* Matches[10];
#define FETCH_RULES
{
if (pid == 0)
{
- runcmd(cmd);
+ runcmd(cmd);
}
else
{
- waitpid(pid, &status, 0);
+ waitpid(pid, &status, 0);
ret = (status == 0);
- }
+ }
}
return ret;
}
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;
}
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))
{
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)
{
--- /dev/null
+#include <io.h>
+
+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;
+}
--- /dev/null
+#include <io.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+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;
+}