Create new fgrepx common function

It's also slightly quicker.
This commit is contained in:
Neale Pickett 2010-09-10 23:07:40 -06:00
parent de5a53c226
commit c9c24d9a8b
3 changed files with 42 additions and 39 deletions

View File

@ -7,20 +7,6 @@
char const *tokenlog = "/var/lib/ctf/tokend/tokens.log"; char const *tokenlog = "/var/lib/ctf/tokend/tokens.log";
char const *claimlog = "/var/lib/ctf/tokend/claim.log"; char const *claimlog = "/var/lib/ctf/tokend/claim.log";
int
mystrcmp(char *a, char *b)
{
while (*a == *b) {
a += 1;
b += 1;
}
if (((*a == '\0') || (*a == '\n')) &&
((*b == '\0') || (*b == '\n'))) {
return 0;
}
return -1;
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -75,32 +61,10 @@ main(int argc, char *argv[])
cgi_page("No such team", ""); cgi_page("No such team", "");
} }
/* Does the token exist? */ if (! fgrepx(token, claimlog)) {
{
FILE *f;
int valid = 0;
f = fopen(claimlog, "r");
if (f) {
while (1) {
char line[100];
if (NULL == fgets(line, sizeof(line), f)) {
break;
}
if (0 == mystrcmp(line, token)) {
valid = 1;
break;
}
}
fclose(f);
}
if (! valid) {
cgi_page("Invalid token", cgi_page("Invalid token",
"<p>Sorry, that token's no good.</p>"); "<p>Sorry, that token's no good.</p>");
} }
}
/* If the token's unclaimed, award points and log the claim */ /* If the token's unclaimed, award points and log the claim */
{ {

View File

@ -7,6 +7,44 @@
#include <time.h> #include <time.h>
#include "common.h" #include "common.h"
#define EOL(c) ((EOF == (c)) || (0 == (c)) || ('\n' == (c)))
int
fgrepx(char const *needle, char const *filename)
{
FILE *f;
int found = 0;
char const *p = needle;
f = fopen(filename, "r");
if (f) {
while (1) {
int c = fgetc(f);
/* This list of cases would have looked so much nicer in OCaml. I
apologize. */
if (EOL(c) && (0 == *p)) {
found = 1;
break;
} else if (EOF == c) {
break;
} else if ((0 == p) || (*p != c)) {
p = needle;
do {
c = fgetc(f);
} while (! EOL(c));
} else if ('\n' == c) {
p = needle;
} else {
p += 1;
}
}
fclose(f);
}
return found;
}
int int
team_exists(char *teamhash) team_exists(char *teamhash)
{ {

View File

@ -6,5 +6,6 @@
int team_exists(char *teamhash); int team_exists(char *teamhash);
int award_points(char *teamhash, char *category, int point); int award_points(char *teamhash, char *category, int point);
int fgrepx(char const *needle, char const *filename);
#endif #endif