mirror of https://github.com/dirtbags/moth.git
Create new fgrepx common function
It's also slightly quicker.
This commit is contained in:
parent
de5a53c226
commit
c9c24d9a8b
|
@ -7,20 +7,6 @@
|
|||
char const *tokenlog = "/var/lib/ctf/tokend/tokens.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
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -75,32 +61,10 @@ main(int argc, char *argv[])
|
|||
cgi_page("No such team", "");
|
||||
}
|
||||
|
||||
/* Does the token exist? */
|
||||
{
|
||||
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) {
|
||||
if (! fgrepx(token, claimlog)) {
|
||||
cgi_page("Invalid token",
|
||||
"<p>Sorry, that token's no good.</p>");
|
||||
}
|
||||
}
|
||||
|
||||
/* If the token's unclaimed, award points and log the claim */
|
||||
{
|
||||
|
|
38
src/common.c
38
src/common.c
|
@ -7,6 +7,44 @@
|
|||
#include <time.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
|
||||
team_exists(char *teamhash)
|
||||
{
|
||||
|
|
|
@ -6,5 +6,6 @@
|
|||
|
||||
int team_exists(char *teamhash);
|
||||
int award_points(char *teamhash, char *category, int point);
|
||||
int fgrepx(char const *needle, char const *filename);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue