Merge branch 'master' of ssh://fozzie/home/neale/projects/ctf

This commit is contained in:
Neale Pickett 2012-05-30 18:04:32 -06:00
commit db655f4b91
1 changed files with 46 additions and 62 deletions

View File

@ -52,7 +52,7 @@ int
main(int argc, char *argv[])
{
int i;
DIR *srv;
DIR *opt;
if (-1 == cgi_init(argv)) {
return 0;
@ -66,9 +66,11 @@ main(int argc, char *argv[])
int i;
while (f && (! feof(f))) {
read_until_char(f, NULL, 0, ' ');
read_until_char(f, NULL, 0, ' ');
read_until_char(f, cat, sizeof(cat), ' ');
read_until_char(f, points_str, sizeof(points_str), '\n');
read_until_char(f, points_str, sizeof(points_str), ' ');
read_until_char(f, NULL, 0, '\n');
points = atol(points_str);
for (i = 0; i < ncats; i += 1) {
@ -90,60 +92,28 @@ main(int argc, char *argv[])
if (f) fclose(f);
}
srv = opendir(package_path(""));
if (NULL == srv) {
cgi_error("Cannot opendir(\"/srv\")");
opt = opendir(package_path(""));
if (NULL == opt) {
cgi_error("Cannot opendir(\"/opt\")");
}
cgi_head("Open puzzles");
printf("<dl>\n");
/* For each file in /srv/ ... */
/* For each file in /opt/ ... */
while (1) {
struct dirent *e = readdir(srv);
struct dirent *e = readdir(opt);
char *cat;
DIR *puzzles;
long catpoints[PUZZLES_MAX];
long maxpoints = 0;
size_t ncatpoints = 0;
if (! e) break;
cat = e->d_name;
if ('.' == cat[0]) continue;
/* We have to lstat anyway to see if it's a directory; may as
well just barge ahead and watch for errors. */
/* Open /srv/ctf/$cat/puzzles/ */
puzzles = opendir(package_path("%s/puzzles", cat));
if (NULL == puzzles) {
continue;
}
while (ncatpoints < PUZZLES_MAX) {
struct dirent *pe = readdir(puzzles);
long points;
char *p;
if (! pe) break;
/* Only do this if it's an int */
points = strtol(pe->d_name, &p, 10);
if (*p) continue;
catpoints[ncatpoints++] = points;
}
closedir(puzzles);
/* Sort points */
qsort(catpoints, ncatpoints, sizeof(*catpoints),
(int (*)(const void *, const void *))longcmp);
/* Print out point values up to one past the last solved puzzle in
this category */
{
long maxpoints = 0;
/* Find the most points scored in this category */
for (i = 0; i < ncats; i += 1) {
@ -153,18 +123,32 @@ main(int argc, char *argv[])
}
}
/* Read in category's map file, print html until point limit reached */
FILE *map = fopen(package_path("%s/map.txt", cat), "r");
if (map == NULL) continue;
printf(" <dt>%s</dt>\n", cat);
printf(" <dd>\n");
for (i = 0; i < ncatpoints; i += 1) {
printf(" <a href=\"/%s/%ld/\">%ld</a>\n",
cat, catpoints[i], catpoints[i]);
if (catpoints[i] > maxpoints) break;
}
printf(" </dd>\n");
}
while (i < PUZZLES_MAX && (!feof(map))) {
char hash[20];
char points_str[20];
long points;
read_until_char(map, points_str, sizeof(points_str), ' ');
read_until_char(map, cat, sizeof(cat), '\n');
points = atol(hash);
printf(" <a href=\"/%s/%ld/\">%ld</a>\n", cat, points, points);
if (points > maxpoints) break;
}
closedir(srv);
printf(" </dd>\n");
fclose(map);
}
closedir(opt);
printf("</dl>\n");
cgi_foot();