Some minor fixes
[bpt/emacs.git] / src / unexw32.c
index d57378b..f70cdd7 100644 (file)
@@ -1,5 +1,5 @@
 /* unexec for GNU Emacs on Windows NT.
-   Copyright (C) 1994, 2001-201 Free Software Foundation, Inc.
+   Copyright (C) 1994, 2001-2014 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -22,6 +22,9 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 #include "unexec.h"
+#include "lisp.h"
+#include "w32common.h"
+#include "w32.h"
 
 #include <stdio.h>
 #include <fcntl.h>
@@ -82,12 +85,12 @@ DWORD_PTR  extra_bss_size_static = 0;
 
 PIMAGE_SECTION_HEADER heap_section;
 
-#ifdef HAVE_NTGUI
-extern HINSTANCE hinst;
-HINSTANCE hprevinst = NULL;
-LPSTR lpCmdLine = "";
-int nCmdShow = 0;
-#endif /* HAVE_NTGUI */
+/* MinGW64 doesn't add a leading underscore to external symbols,
+   whereas configure.ac sets up LD_SWITCH_SYSTEM_TEMACS to force the
+   entry point at __start, with two underscores.  */
+#ifdef __MINGW64__
+#define _start __start
+#endif
 
 /* Startup code for running on NT.  When we are running as the dumped
    version, we need to bootstrap our heap and .bss section into our
@@ -118,21 +121,14 @@ _start (void)
   /* Prevent Emacs from being locked up (eg. in batch mode) when
      accessing devices that aren't mounted (eg. removable media drives).  */
   SetErrorMode (SEM_FAILCRITICALERRORS);
-
-  /* Invoke the NT CRT startup routine now that our housecleaning
-     is finished.  */
-#ifdef HAVE_NTGUI
-  /* determine WinMain args like crt0.c does */
-  hinst = GetModuleHandle (NULL);
-  lpCmdLine = GetCommandLine ();
-  nCmdShow = SW_SHOWDEFAULT;
-#endif
   mainCRTStartup ();
 }
 
 
 /* File handling.  */
 
+/* Implementation note: this and the next functions work with ANSI
+   codepage encoded file names!  */
 int
 open_input_file (file_data *p_file, char *filename)
 {
@@ -141,8 +137,8 @@ open_input_file (file_data *p_file, char *filename)
   void  *file_base;
   unsigned long size, upper_size;
 
-  file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
-                    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+  file = CreateFileA (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
+                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
   if (file == INVALID_HANDLE_VALUE)
     return FALSE;
 
@@ -172,8 +168,16 @@ open_output_file (file_data *p_file, char *filename, unsigned long size)
   HANDLE file_mapping;
   void  *file_base;
 
-  file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
-                    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
+  /* We delete any existing FILENAME because loadup.el will create a
+     hard link to it under the name emacs-XX.YY.ZZ.nn.exe.  Evidently,
+     overwriting a file on Unix breaks any hard links to it, but that
+     doesn't happen on Windows.  If we don't delete the file before
+     creating it, all the emacs-XX.YY.ZZ.nn.exe end up being hard
+     links to the same file, which defeats the purpose of these hard
+     links: being able to run previous builds.  */
+  DeleteFileA (filename);
+  file = CreateFileA (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
+                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
   if (file == INVALID_HANDLE_VALUE)
     return FALSE;
 
@@ -727,23 +731,30 @@ void
 unexec (const char *new_name, const char *old_name)
 {
   file_data in_file, out_file;
-  char out_filename[MAX_PATH], in_filename[MAX_PATH];
+  char out_filename[MAX_PATH], in_filename[MAX_PATH], new_name_a[MAX_PATH];
   unsigned long size;
   char *p;
   char *q;
 
   /* Ignore old_name, and get our actual location from the OS.  */
-  if (!GetModuleFileName (NULL, in_filename, MAX_PATH))
+  if (!GetModuleFileNameA (NULL, in_filename, MAX_PATH))
     abort ();
-  dostounix_filename (in_filename);
+
+  /* Can't use dostounix_filename here, since that needs its file name
+     argument encoded in UTF-8.  */
+  for (p = in_filename; *p; p = CharNextA (p))
+    if (*p == '\\')
+      *p = '/';
+
   strcpy (out_filename, in_filename);
+  filename_to_ansi (new_name, new_name_a);
 
   /* Change the base of the output filename to match the requested name.  */
   if ((p = strrchr (out_filename, '/')) == NULL)
     abort ();
   /* The filenames have already been expanded, and will be in Unix
      format, so it is safe to expect an absolute name.  */
-  if ((q = strrchr (new_name, '/')) == NULL)
+  if ((q = strrchr (new_name_a, '/')) == NULL)
     abort ();
   strcpy (p, q);