moth/packages/mcp/src/puzzles.cgi.c

174 lines
3.5 KiB
C
Raw Normal View History

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "common.h"
2010-09-14 18:04:33 -06:00
int
longcmp(long *a, long *b)
{
if (*a < *b) return -1;
if (*a > *b) return 1;
return 0;
}
2010-09-14 18:04:33 -06:00
#define PUZZLES_MAX 100
/** Keeps track of the most points yet awarded in each category */
int ncats = 0;
struct {
char cat[CAT_MAX];
long points;
2010-09-14 18:04:33 -06:00
} points_by_cat[PUZZLES_MAX];
2010-09-16 12:20:44 -06:00
size_t
read_until_char(FILE *f, char *buf, size_t buflen, char delim)
{
2010-09-16 12:20:44 -06:00
size_t i = 0;
while (1) {
2010-09-16 12:20:44 -06:00
int c = fgetc(f);
if ((EOF == c) || delim == c) {
if (buf) {
buf[i] = '\0';
2010-09-14 18:04:33 -06:00
}
2010-09-16 12:20:44 -06:00
return i;
}
2010-09-16 12:20:44 -06:00
if (i + 1 < buflen) {
buf[i] = c;
}
2010-09-16 12:20:44 -06:00
i += 1;
}
}
int
main(int argc, char *argv[])
{
int i;
DIR *srv;
if (-1 == cgi_init(argv)) {
return 0;
}
2010-09-16 12:20:44 -06:00
{
2010-09-24 22:28:40 -06:00
FILE *f = fopen(state_path("puzzles.db"), "r");
2010-09-16 12:20:44 -06:00
char cat[CAT_MAX];
char points_str[11];
long points;
int i;
while (f && (! feof(f))) {
read_until_char(f, NULL, 0, ' ');
read_until_char(f, cat, sizeof(cat), ' ');
read_until_char(f, points_str, sizeof(points_str), '\n');
points = atol(points_str);
for (i = 0; i < ncats; i += 1) {
if (0 == strcmp(cat, points_by_cat[i].cat)) break;
}
if (i == ncats) {
if (PUZZLES_MAX == ncats) {
continue;
}
strncpy(points_by_cat[i].cat, cat, sizeof(points_by_cat[i].cat));
points_by_cat[i].points = 0;
ncats += 1;
}
if (points > points_by_cat[i].points) {
points_by_cat[i].points = points;
}
}
if (f) fclose(f);
}
2010-09-23 18:23:00 -06:00
srv = opendir(package_path(""));
if (NULL == srv) {
cgi_error("Cannot opendir(\"/srv\")");
}
cgi_head("Open puzzles");
printf("<dl>\n");
/* For each file in /srv/ ... */
while (1) {
struct dirent *e = readdir(srv);
2010-09-14 18:04:33 -06:00
char *cat;
DIR *puzzles;
long catpoints[PUZZLES_MAX];
size_t ncatpoints = 0;
if (! e) break;
2010-09-14 18:04:33 -06:00
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/ */
2010-09-23 18:23:00 -06:00
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) {
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) {
2011-03-08 23:06:21 -07:00
printf(" <a href=\"/%s/%ld/\">%ld</a>\n",
cat, catpoints[i], catpoints[i]);
if (catpoints[i] > maxpoints) break;
}
printf(" </dd>\n");
}
}
closedir(srv);
printf("</dl>\n");
cgi_foot();
return 0;
}