--- trr_update.c.orig 2007-01-31 16:59:45.000000000 +0100 +++ trr_update.c 2007-01-31 15:15:55.000000000 +0100 @@ -21,6 +21,10 @@ #include #include #include +#include +#include + +#include "utils.h" #if defined(HAVE_STRING_H) #include @@ -44,10 +48,13 @@ #endif /* HAVE_SYS_FCNTL_H */ #endif /* HAVE_FCNTL_H */ -main(int argc, char **argv){ - char scorefile[256], lockfile[256], datestr[64]; - char line[256], savedline[256]; - const char *user, *scores, *step, *times, *ttime, *token; +int main(int argc, char **argv){ + char *scorefile = NULL, *lockfile = NULL, *line = NULL, *savedline = NULL; + char *user = NULL, *scores = NULL, *step = NULL, *times = NULL, + *ttime = NULL, *token = NULL; + size_t len=0; + char datestr[64]; + FILE *fd, *tmpf; int score, tmpscore, i, myself, inserted; long datev; @@ -59,12 +66,16 @@ signal(SIGTERM, SIG_IGN); umask(18); - strcpy(scorefile, RECORD_DIR); + + if (argc < 7){ + fprintf(stderr, "too few arguments\n"); + exit(1); + } + + my_asprintf (&scorefile, "%s%s", RECORD_DIR, argv[1]); /* create a new record file */ if (argc == 2){ - strcat(scorefile, argv[1]); - if ((fd = fopen(scorefile, "w")) == NULL){ perror(scorefile); exit(1); @@ -73,10 +84,9 @@ exit(0); } - /* upfate high score file */ - strcat(scorefile, argv[1]); - strcpy(lockfile, scorefile); - strcat(lockfile, ".lock"); + /* update high score file */ + my_asprintf(&lockfile, "%s.lock", scorefile); + user = argv[2]; scores = argv[3]; score = atoi(argv[3]); @@ -110,13 +120,19 @@ inserted = 0; /* sorting ... */ - while (fgets(line, 256, fd)){ + while (my_getline(&line, &len, fd) != -1){ myself = 0; - strcpy(savedline, line); - token = (char*)strtok(line, " \t"); - if (! strcmp(user, token)) + savedline = strdup(line); + token = strtok(line, " \t"); + if (token && !strcmp(user, token)) myself = 1; - token = (char*)strtok(NULL, " \t"); + + token = strtok(NULL, " \t"); + if (!token){ + perror("strok"); + exit(1); + } + tmpscore = atoi(token); if ((! inserted) && (tmpscore <= score)){ inserted = 1; @@ -146,12 +162,18 @@ unlink(lockfile); exit(1); } - while (fgets(line, 256, tmpf)) + while (my_getline(&line, &len, tmpf) != -1) fputs(line, fd); fclose(tmpf); fclose(fd); + free(line); + free(lockfile); + free(scorefile); + free(savedline); + free(token); + /* release lock */ unlink(lockfile); return 0;