emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r113708: Fix bugs in update-game-score, on MS-Window


From: Eli Zaretskii
Subject: [Emacs-diffs] trunk r113708: Fix bugs in update-game-score, on MS-Windows and elsewhere.
Date: Mon, 05 Aug 2013 17:10:12 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 113708
revision-id: address@hidden
parent: address@hidden
committer: Eli Zaretskii <address@hidden>
branch nick: trunk
timestamp: Mon 2013-08-05 20:09:28 +0300
message:
  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.
modified:
  lib-src/ChangeLog              changelog-20091113204419-o5vbwnq5f7feedwu-1608
  lib-src/ntlib.c                ntlib.c-20091113204419-o5vbwnq5f7feedwu-803
  lib-src/ntlib.h                ntlib.h-20091113204419-o5vbwnq5f7feedwu-971
  lib-src/update-game-score.c    
updategamescore.c-20091113204419-o5vbwnq5f7feedwu-2389
=== modified file 'lib-src/ChangeLog'
--- a/lib-src/ChangeLog 2013-08-04 17:52:25 +0000
+++ b/lib-src/ChangeLog 2013-08-05 17:09:28 +0000
@@ -1,3 +1,18 @@
+2013-08-05  Eli Zaretskii  <address@hidden>
+
+       * 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).
+
+       * ntlib.h (rename): Don't undefine.
+
+       * ntlib.c (sys_rename): New function, needed for
+       update-game-score.
+
 2013-08-04  Eli Zaretskii  <address@hidden>
 
        * ntlib.h: Include fcntl.h.

=== modified file 'lib-src/ntlib.c'
--- a/lib-src/ntlib.c   2013-08-04 17:52:25 +0000
+++ b/lib-src/ntlib.c   2013-08-05 17:09:28 +0000
@@ -424,14 +424,14 @@
 }
 
 /* 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 @@
   /* 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;
+}

=== modified file 'lib-src/ntlib.h'
--- a/lib-src/ntlib.h   2013-08-04 17:52:25 +0000
+++ b/lib-src/ntlib.h   2013-08-05 17:09:28 +0000
@@ -69,7 +69,6 @@
 #define pipe    _pipe
 #undef read
 #define read    _read
-#undef rename
 #undef rmdir
 #define rmdir   _rmdir
 #undef unlink

=== modified file 'lib-src/update-game-score.c'
--- a/lib-src/update-game-score.c       2013-08-04 16:56:56 +0000
+++ b/lib-src/update-game-score.c       2013-08-05 17:09:28 +0000
@@ -228,10 +228,11 @@
 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 @@
 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 @@
   return ret;
 }
 
-
 /* update-game-score.c ends here */


reply via email to

[Prev in Thread] Current Thread [Next in Thread]