puzzles.cgi now inspects map files

This commit is contained in:
Aaron J. McPhall 2012-05-30 12:19:07 -06:00
parent 11d139f50e
commit 7c1c2e4ec2
1 changed files with 43 additions and 61 deletions

View File

@ -52,7 +52,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i; int i;
DIR *srv; DIR *opt;
if (-1 == cgi_init(argv)) { if (-1 == cgi_init(argv)) {
return 0; return 0;
@ -90,60 +90,28 @@ main(int argc, char *argv[])
if (f) fclose(f); if (f) fclose(f);
} }
srv = opendir(package_path("")); opt = opendir(package_path(""));
if (NULL == srv) { if (NULL == opt) {
cgi_error("Cannot opendir(\"/srv\")"); cgi_error("Cannot opendir(\"/opt\")");
} }
cgi_head("Open puzzles"); cgi_head("Open puzzles");
printf("<dl>\n"); printf("<dl>\n");
/* For each file in /srv/ ... */ /* For each file in /opt/ ... */
while (1) { while (1) {
struct dirent *e = readdir(srv); struct dirent *e = readdir(opt);
char *cat; char *cat;
DIR *puzzles; DIR *puzzles;
long catpoints[PUZZLES_MAX]; long catpoints[PUZZLES_MAX];
long maxpoints = 0;
size_t ncatpoints = 0; size_t ncatpoints = 0;
if (! e) break; if (! e) break;
cat = e->d_name; cat = e->d_name;
if ('.' == cat[0]) continue; 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 */ /* Find the most points scored in this category */
for (i = 0; i < ncats; i += 1) { for (i = 0; i < ncats; i += 1) {
@ -153,18 +121,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(" <dt>%s</dt>\n", cat);
printf(" <dd>\n"); printf(" <dd>\n");
for (i = 0; i < ncatpoints; i += 1) {
printf(" <a href=\"/%s/%ld/\">%ld</a>\n", while (i < PUZZLES_MAX && (!feof(map))) {
cat, catpoints[i], catpoints[i]); char hash[20];
if (catpoints[i] > maxpoints) break; char points_str[20];
} long points;
printf(" </dd>\n");
} read_until_char(map, cat, sizeof(cat), ' ');
read_until_char(map, points_str, sizeof(points_str), '\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"); printf("</dl>\n");
cgi_foot(); cgi_foot();