Rewrite read_scores() with suggestions from caf. Failing to open the scores file no longer leaks memory, and a malformed record no longer adds an empty link to the global scores list.

git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@253 13b04d17-f746-0410-82c6-800466cd88b0
This commit is contained in:
Tim Cava
2013-07-02 14:42:36 +00:00
parent 4ff0b8d2d5
commit ed1ad22d70
2 changed files with 19 additions and 36 deletions

View File

@@ -11,7 +11,7 @@ int Acro_Init(IrcCommandDll **intp, Function_ptr *global_table)
add_module_proc(RAW_PROC, "acro", "PRIVMSG", NULL, 0, 0, acro_main, NULL); add_module_proc(RAW_PROC, "acro", "PRIVMSG", NULL, 0, 0, acro_main, NULL);
add_module_proc(COMMAND_PROC, "scores", "scores", NULL, 0, 0, put_scores, NULL); add_module_proc(COMMAND_PROC, "scores", "scores", NULL, 0, 0, put_scores, NULL);
gscores = read_scores(); read_scores();
if (!game) if (!game)
game = init_acro(game); game = init_acro(game);
put_it("BitchX Acromania dll v0.9b by By-Tor loaded..."); put_it("BitchX Acromania dll v0.9b by By-Tor loaded...");
@@ -338,43 +338,26 @@ vrec *take_vote(grec *acro, vrec *voters, prec *players, char *nick, char *host,
return voters; return voters;
} }
srec *read_scores() void read_scores(void)
{ {
srec *tmp, *tmp2; FILE *infile;
char buff[100], *p; srec *record;
FILE *sf; unsigned long score;
tmp = tmp2 = (srec *)new_malloc(sizeof(srec)); char nick[64];
memset(buff, 0, sizeof(buff));
sf = fopen(SCOREFILE, "r"); infile = fopen(SCOREFILE, "r");
if (!sf) if (infile == NULL)
return 0; return;
while (!feof(sf))
while (fscanf(infile, " %63[^ ,] , %lu", nick, &score) == 2)
{ {
if (fgets(buff, 51, sf)) record = new_malloc(sizeof(srec));
{ record->nick = m_strdup(nick);
if (tmp->nick) record->score = score;
tmp = tmp->next = (srec *)new_malloc(sizeof(srec)); add_to_list((List **)&gscores, (List *)record);
if (buff[strlen(buff)-1] == '\n')
buff[strlen(buff)-1] = 0;
if (!*buff)
break;
if ((p = (char *)strchr(buff, ',')))
*p++ = 0;
else if (!p)
{
fclose(sf);
return tmp2;
} }
tmp->nick = (char *)new_malloc(strlen(buff+1));
strcpy(tmp->nick, buff); fclose(infile);
if (p)
tmp->score = strtoul(p, NULL, 10);
}
else
break;
}
fclose(sf);
return tmp2;
} }
int write_scores(srec *tmp) int write_scores(srec *tmp)

View File

@@ -98,7 +98,7 @@ BUILT_IN_DLL(put_scores);
grec *init_acro(grec *); grec *init_acro(grec *);
void make_acro(grec *); void make_acro(grec *);
int valid_acro(grec *, char *); int valid_acro(grec *, char *);
srec *read_scores(void); void read_scores(void);
int write_scores(srec *); int write_scores(srec *);
prec *take_acro(grec *, prec *, char *, char *, char *); prec *take_acro(grec *, prec *, char *, char *, char *);
vrec *take_vote(grec *, vrec *, prec *, char *, char *, char *); vrec *take_vote(grec *, vrec *, prec *, char *, char *, char *);