Lock game an position file during writing
[xboard.git] / winboard / winboard.c
index 07e4bd2..c8bf0b2 100644 (file)
@@ -73,6 +73,7 @@
 #include <richedit.h>\r
 #include <mmsystem.h>\r
 #include <ctype.h>\r
+#include <io.h>\r
 \r
 #if __GNUC__\r
 #include <errno.h>\r
@@ -3842,11 +3843,11 @@ HDCDrawPosition(HDC hdc, BOOLEAN repaint, Board board)
         boardRect.bottom - boardRect.top,\r
         tmphdc, boardRect.left, boardRect.top, SRCCOPY);\r
   if(saveDiagFlag) { \r
-    BITMAP b; int i, j=0, m, w, wb, fac=0; char pData[1000000]; \r
+    BITMAP b; int i, j=0, m, w, wb, fac=0; char *pData; \r
     BITMAPINFOHEADER bih; int color[16], nrColors=0;\r
 \r
     GetObject(bufferBitmap, sizeof(b), &b);\r
-    if(b.bmWidthBytes*b.bmHeight <= 990000) {\r
+    if(pData = malloc(b.bmWidthBytes*b.bmHeight + 10000)) {\r
        bih.biSize = sizeof(BITMAPINFOHEADER);\r
        bih.biWidth = b.bmWidth;\r
        bih.biHeight = b.bmHeight;\r
@@ -3910,6 +3911,7 @@ HDCDrawPosition(HDC hdc, BOOLEAN repaint, Board board)
        // write bitmap data\r
        for(i=0; i<wb*(b.bmHeight - boardRect.top + OUTER_MARGIN); i++) \r
                fputc(pData[i], diagFile);\r
+       free(pData);\r
      }\r
   }\r
 \r
@@ -3946,11 +3948,8 @@ SaveDiagram(f)
 {\r
     saveDiagFlag = 1; diagFile = f;\r
     HDCDrawPosition(NULL, TRUE, NULL);\r
-\r
     saveDiagFlag = 0;\r
 \r
-//    if(f != NULL) fprintf(f, "Sorry, but this feature is still in preparation\n");\r
-    \r
     fclose(f);\r
     return TRUE;\r
 }\r
@@ -5097,7 +5096,7 @@ WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 \r
     case IDM_Engine2Options:\r
       savedHwnd = hwnd;\r
-      if(WaitForSecond(SettingsMenuIfReady)) break;\r
+      if(WaitForEngine(&second, SettingsMenuIfReady)) break;\r
       EngineOptionsPopup(hwnd, &second);\r
       break;\r
 \r
@@ -8115,7 +8114,8 @@ DisplayMessage(char *str1, char *str2)
     if (len > remain) len = remain;\r
     strncat(messageText, str2, len);\r
   }\r
-  messageText[MESSAGE_TEXT_MAX - 1] = NULLCHAR;\r
+  messageText[MESSAGE_TEXT_MAX - 1] = NULLCHAR;
+  safeStrCpy(lastMsg, messageText, MSG_SIZ);
 \r
   if (hwndMain == NULL || IsIconic(hwndMain)) return;\r
 \r
@@ -9785,3 +9785,19 @@ SettingsPopUp(ChessProgramState *cps)
 {     // [HGM] wrapper needed because handles must not be passed through back-end\r
       EngineOptionsPopup(savedHwnd, cps);\r
 }\r
+\r
+int flock(int fid, int code)\r
+{\r
+    HANDLE hFile = (HANDLE) _get_osfhandle(fid);\r
+    OVERLAPPED ov;\r
+    ov.hEvent = NULL;\r
+    ov.Offset = 0;\r
+    ov.OffsetHigh = 0;\r
+    switch(code) {\r
+      case 1: LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, 1024, 0, &ov); break;   // LOCK_SH\r
+      case 2: LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, 1024, 0, &ov); break;   // LOCK_EX\r
+      case 3: UnlockFileEx(hFile, 0, 1024, 0, &ov); break; // LOCK_UN\r
+      default: return -1;\r
+    }\r
+    return 0;\r
+}\r