diff options
author | Eli Zaretskii <eliz@gnu.org> | 2013-08-05 20:09:28 +0300 |
---|---|---|
committer | Eli Zaretskii <eliz@gnu.org> | 2013-08-05 20:09:28 +0300 |
commit | 98a428c15ad48f8579b00b68aae6a89b34238b12 (patch) | |
tree | 3324c8c97a80d790dd8931114e3e830f41b24001 /lib-src/update-game-score.c | |
parent | 5c0e57feb7429f367ed402dbe4a0572a6528d0b5 (diff) | |
download | emacs-98a428c15ad48f8579b00b68aae6a89b34238b12.tar.gz emacs-98a428c15ad48f8579b00b68aae6a89b34238b12.tar.bz2 emacs-98a428c15ad48f8579b00b68aae6a89b34238b12.zip |
Fix bugs in update-game-score, on MS-Windows and elsewhere.
lib-src/update-game-score.c (read_score): Try reading a character before
probing the stream for EOF. Initialize score->score to zero,
before reading and accumulating the score.
(read_scores): Fix logic that determines which value to return.
Close the input stream when finished reading the scores (avoids
failures in overwriting the file with a new one on MS-Windows,
since a file that is open cannot be deleted).
lib-src/ntlib.h (rename): Don't undefine.
lib-src/ntlib.c (sys_rename): New function, needed for
update-game-score.
Diffstat (limited to 'lib-src/update-game-score.c')
-rw-r--r-- | lib-src/update-game-score.c | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c index 1699e305c8d..92c1663f658 100644 --- a/lib-src/update-game-score.c +++ b/lib-src/update-game-score.c @@ -228,10 +228,11 @@ static int read_score (FILE *f, struct score_entry *score) { int c; + if ((c = getc (f)) != EOF) + ungetc (c, f); if (feof (f)) return 1; - while ((c = getc (f)) != EOF - && isdigit (c)) + for (score->score = 0; (c = getc (f)) != EOF && isdigit (c); ) { score->score *= 10; score->score += (c-48); @@ -311,34 +312,38 @@ read_score (FILE *f, struct score_entry *score) static int read_scores (const char *filename, struct score_entry **scores, int *count) { - int readval, scorecount, cursize; + int readval = -1, scorecount, cursize; struct score_entry *ret; FILE *f = fopen (filename, "r"); + int retval = -1; if (!f) return -1; scorecount = 0; cursize = 16; ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize); - if (!ret) - return -1; - while ((readval = read_score (f, &ret[scorecount])) == 0) + if (ret) { - /* We encountered an error. */ - if (readval < 0) - return -1; - scorecount++; - if (scorecount >= cursize) + while ((readval = read_score (f, &ret[scorecount])) == 0) { - cursize *= 2; - ret = (struct score_entry *) - realloc (ret, (sizeof (struct score_entry) * cursize)); - if (!ret) - return -1; + scorecount++; + if (scorecount >= cursize) + { + cursize *= 2; + ret = (struct score_entry *) + realloc (ret, (sizeof (struct score_entry) * cursize)); + if (!ret) + break; + } } } - *count = scorecount; - *scores = ret; - return 0; + if (readval > 0) + { + *count = scorecount; + *scores = ret; + retval = 0; + } + fclose (f); + return retval; } static int @@ -461,5 +466,4 @@ unlock_file (const char *filename, void *state) return ret; } - /* update-game-score.c ends here */ |