summaryrefslogtreecommitdiff
path: root/lib-src/ntlib.c
diff options
context:
space:
mode:
authorEli Zaretskii <eliz@gnu.org>2013-08-05 20:09:28 +0300
committerEli Zaretskii <eliz@gnu.org>2013-08-05 20:09:28 +0300
commit98a428c15ad48f8579b00b68aae6a89b34238b12 (patch)
tree3324c8c97a80d790dd8931114e3e830f41b24001 /lib-src/ntlib.c
parent5c0e57feb7429f367ed402dbe4a0572a6528d0b5 (diff)
downloademacs-98a428c15ad48f8579b00b68aae6a89b34238b12.tar.gz
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/ntlib.c')
-rw-r--r--lib-src/ntlib.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/lib-src/ntlib.c b/lib-src/ntlib.c
index 0d0642d1bf2..ab7d8b590df 100644
--- a/lib-src/ntlib.c
+++ b/lib-src/ntlib.c
@@ -424,14 +424,14 @@ lstat (const char * path, struct stat * buf)
}
/* Implementation of mkostemp for MS-Windows, to avoid race conditions
- when using mktemp.
+ when using mktemp. Copied from w32.c.
- Standard algorithm for generating a temporary file name seems to be
- use pid or tid with a letter on the front (in place of the 6 X's)
- and cycle through the letters to find a unique name. We extend
- that to allow any reasonable character as the first of the 6 X's,
- so that the number of simultaneously used temporary files will be
- greater. */
+ This is used only in update-game-score.c. It is overkill for that
+ use case, since update-game-score renames the temporary file into
+ the game score file, which isn't atomic on MS-Windows anyway, when
+ the game score already existed before running the program, which it
+ almost always does. But using a simpler implementation just to
+ make a point is uneconomical... */
int
mkostemp (char * template, int flags)
@@ -477,3 +477,17 @@ mkostemp (char * template, int flags)
/* Template is badly formed or else we can't generate a unique name. */
return -1;
}
+
+/* On Windows, you cannot rename into an existing file. */
+int
+sys_rename (const char *from, const char *to)
+{
+ int retval = rename (from, to);
+
+ if (retval < 0 && errno == EEXIST)
+ {
+ if (unlink (to) == 0)
+ retval = rename (from, to);
+ }
+ return retval;
+}