From 44e41c48e59ed30788e8d3817c6a2262e967b0c9 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 31 Oct 2012 16:01:36 -0600 Subject: [PATCH] make a stab at detecting fs layout --- packages/00common/src/claim.cgi.c | 1 + packages/00common/src/common.c | 27 +++++++++++++++++++++++++++ packages/00common/src/common.h | 1 + packages/00common/src/pointscli.c | 1 + packages/00common/src/puzzler.cgi.c | 1 + packages/00common/src/puzzles.cgi.c | 1 + 6 files changed, 32 insertions(+) diff --git a/packages/00common/src/claim.cgi.c b/packages/00common/src/claim.cgi.c index 8f8bd10..082962f 100644 --- a/packages/00common/src/claim.cgi.c +++ b/packages/00common/src/claim.cgi.c @@ -11,6 +11,7 @@ main(int argc, char *argv[]) if (-1 == cgi_init(argv)) { return 0; } + ctf_chdir(); /* Read in team and token */ while (1) { diff --git a/packages/00common/src/common.c b/packages/00common/src/common.c index 9f10dfa..a84899f 100644 --- a/packages/00common/src/common.c +++ b/packages/00common/src/common.c @@ -363,6 +363,33 @@ my_snprintf(char *buf, size_t buflen, char *fmt, ...) } } + +void +ctf_chdir() +{ + char const *ctf_base = getenv("CTF_BASE"); + int i; + + if (ctf_base) { + /* chdir to CTF_BASE */ + chdir(ctf_base); + } + + /* Keep going up one directory until there's a packages directory */ + for (i = 0; i < 5; i += 1) { + struct stat st; + + if ((0 == stat("packages", &st)) && + S_ISDIR(st.st_mode)) { + return; + } + chdir(".."); + } + fprintf(stderr, "Can not determine CTF_BASE directory: exiting.\n"); + exit(66); +} + + static char * mkpath(char const *type, char const *fmt, va_list ap) { diff --git a/packages/00common/src/common.h b/packages/00common/src/common.h index f5cb77f..fd48a86 100644 --- a/packages/00common/src/common.h +++ b/packages/00common/src/common.h @@ -23,6 +23,7 @@ void cgi_page(char *title, char *fmt, ...); void cgi_error(char *text); +void ctf_chdir(); int anchored_search(char const *filename, char const *needle, const char anchor); void urandom(char *buf, size_t buflen); int my_snprintf(char *buf, size_t buflen, char *fmt, ...); diff --git a/packages/00common/src/pointscli.c b/packages/00common/src/pointscli.c index 5d4185d..f0eace7 100644 --- a/packages/00common/src/pointscli.c +++ b/packages/00common/src/pointscli.c @@ -16,6 +16,7 @@ main(int argc, char *argv[]) "Usage: pointscli TEAM CATEGORY POINTS 'COMMENT'\n"); return EX_USAGE; } + ctf_chdir(); points = atoi(argv[3]); if (0 == points) { diff --git a/packages/00common/src/puzzler.cgi.c b/packages/00common/src/puzzler.cgi.c index e2c4697..b1a8163 100644 --- a/packages/00common/src/puzzler.cgi.c +++ b/packages/00common/src/puzzler.cgi.c @@ -15,6 +15,7 @@ main(int argc, char *argv[]) if (-1 == cgi_init(argv)) { return 0; } + ctf_chdir(); /* Read in team and answer */ while (1) { diff --git a/packages/00common/src/puzzles.cgi.c b/packages/00common/src/puzzles.cgi.c index 39403a8..d05b8c3 100644 --- a/packages/00common/src/puzzles.cgi.c +++ b/packages/00common/src/puzzles.cgi.c @@ -57,6 +57,7 @@ main(int argc, char *argv[]) if (-1 == cgi_init(argv)) { return 0; } + ctf_chdir(); { FILE *f = fopen(state_path("points.log"), "r");