216 |
user_id = get_user_id (); |
user_id = get_user_id (); |
217 |
if (user_id == NULL) |
if (user_id == NULL) |
218 |
lose_syserr ("Couldn't determine user id"); |
lose_syserr ("Couldn't determine user id"); |
219 |
|
|
220 |
if (stat (scorefile, &buf) < 0) |
if (stat (scorefile, &buf) < 0) |
221 |
lose_syserr ("Failed to access scores file"); |
lose_syserr ("Failed to access scores file"); |
222 |
|
|
223 |
if (lock_file (scorefile, &lockstate) < 0) |
if (lock_file (scorefile, &lockstate) < 0) |
224 |
lose_syserr ("Failed to lock scores file"); |
lose_syserr ("Failed to lock scores file"); |
225 |
|
|
226 |
if (read_scores (scorefile, &scores, &scorecount) < 0) |
if (read_scores (scorefile, &scores, &scorecount) < 0) |
227 |
{ |
{ |
228 |
unlock_file (scorefile, lockstate); |
unlock_file (scorefile, lockstate); |
283 |
char *username = malloc (unamelen); |
char *username = malloc (unamelen); |
284 |
if (!username) |
if (!username) |
285 |
return -1; |
return -1; |
286 |
|
|
287 |
while ((c = getc (f)) != EOF |
while ((c = getc (f)) != EOF |
288 |
&& !isspace (c)) |
&& !isspace (c)) |
289 |
{ |
{ |
293 |
username[unameread] = c; |
username[unameread] = c; |
294 |
unameread++; |
unameread++; |
295 |
} |
} |
296 |
if (c == EOF) |
if (c == EOF) |
297 |
return -1; |
return -1; |
298 |
username[unameread] = '\0'; |
username[unameread] = '\0'; |
299 |
score->username = username; |
score->username = username; |
342 |
int readval, scorecount, cursize; |
int readval, scorecount, cursize; |
343 |
struct score_entry *ret; |
struct score_entry *ret; |
344 |
FILE *f = fopen (filename, "r"); |
FILE *f = fopen (filename, "r"); |
345 |
if (!f) |
if (!f) |
346 |
return -1; |
return -1; |
347 |
scorecount = 0; |
scorecount = 0; |
348 |
cursize = 16; |
cursize = 16; |
349 |
ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize); |
ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize); |
350 |
if (!ret) |
if (!ret) |
351 |
return -1; |
return -1; |
352 |
while ((readval = read_score (f, &ret[scorecount])) == 0) |
while ((readval = read_score (f, &ret[scorecount])) == 0) |
353 |
{ |
{ |
388 |
} |
} |
389 |
|
|
390 |
int |
int |
391 |
push_score (scores, count, newscore, username, newdata) |
push_score (scores, count, newscore, username, newdata) |
392 |
struct score_entry **scores; |
struct score_entry **scores; |
393 |
int *count; int newscore; |
int *count; int newscore; |
394 |
char *username; |
char *username; |
406 |
*scores = newscores; |
*scores = newscores; |
407 |
return 0; |
return 0; |
408 |
} |
} |
409 |
|
|
410 |
void |
void |
411 |
sort_scores (scores, count, reverse) |
sort_scores (scores, count, reverse) |
412 |
struct score_entry *scores; |
struct score_entry *scores; |
413 |
int count; |
int count; |
414 |
int reverse; |
int reverse; |
415 |
{ |
{ |
416 |
qsort (scores, count, sizeof (struct score_entry), |
qsort (scores, count, sizeof (struct score_entry), |
417 |
reverse ? score_compare_reverse : score_compare); |
reverse ? score_compare_reverse : score_compare); |
421 |
write_scores (filename, scores, count) |
write_scores (filename, scores, count) |
422 |
const char *filename; |
const char *filename; |
423 |
const struct score_entry * scores; |
const struct score_entry * scores; |
424 |
int count; |
int count; |
425 |
{ |
{ |
426 |
FILE *f; |
FILE *f; |
427 |
int i; |
int i; |
428 |
char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1); |
char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1); |
429 |
if (!tempfile) |
if (!tempfile) |
448 |
return -1; |
return -1; |
449 |
return 0; |
return 0; |
450 |
} |
} |
451 |
|
|
452 |
int |
int |
453 |
lock_file (filename, state) |
lock_file (filename, state) |
454 |
const char *filename; |
const char *filename; |
491 |
close (fd); |
close (fd); |
492 |
return 0; |
return 0; |
493 |
} |
} |
494 |
|
|
495 |
int |
int |
496 |
unlock_file (filename, state) |
unlock_file (filename, state) |
497 |
const char *filename; |
const char *filename; |