puzzles.cgi now inspects map files

This commit is contained in:
Aaron J. McPhall 2012-05-30 12:19:07 -06:00
parent 84a59868d5
commit 9ed4dd3d52
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;
@ -60,10 +60,10 @@ main(int argc, char *argv[])
{ {
FILE *f = fopen(state_path("puzzles.db"), "r"); FILE *f = fopen(state_path("puzzles.db"), "r");
char cat[CAT_MAX]; char cat[CAT_MAX];
char points_str[11]; char points_str[11];
long points; long points;
int i; int i;
while (f && (! feof(f))) { while (f && (! feof(f))) {
read_until_char(f, NULL, 0, ' '); read_until_char(f, NULL, 0, ' ');
@ -90,81 +90,63 @@ 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/ */ /* Find the most points scored in this category */
puzzles = opendir(package_path("%s/puzzles", cat)); for (i = 0; i < ncats; i += 1) {
if (NULL == puzzles) { if (0 == strcmp(cat, points_by_cat[i].cat)) {
continue; maxpoints = points_by_cat[i].points;
} break;
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) {
if (0 == strcmp(cat, points_by_cat[i].cat)) {
maxpoints = points_by_cat[i].points;
break;
}
} }
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");
} }
/* 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");
while (i < PUZZLES_MAX && (!feof(map))) {
char hash[20];
char points_str[20];
long points;
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;
}
printf(" </dd>\n");
fclose(map);
} }
closedir(srv); closedir(opt);
printf("</dl>\n"); printf("</dl>\n");
cgi_foot(); cgi_foot();