- Code specific to Windows was #ifdef'ed with _MSC_VER so it wasn't included with MinGW. So _MSC_VER is replaced with _WIN32.
- MinGW doesn't support _get_pgmptr() so it is necessary to directly call the Win32 function GetModuleFileName(). This implies including windows.h which in turn causes a name clash with the Win32 function SearchPath(). So the cc65 type SearchPath is renamed to SearchPaths.
 
 
 
-SearchPath*     IncSearchPath;          /* Standard include path */
-SearchPath*     BinSearchPath;          /* Binary include path */
+SearchPaths*    IncSearchPath;          /* Standard include path */
+SearchPaths*    BinSearchPath;          /* Binary include path */
 
 
 
     AddSubSearchPathFromEnv (IncSearchPath, "CC65_HOME", "asminc");
 
     /* Add some compiled-in search paths if defined at compile time. */
-#ifdef CA65_INC
+#if defined(CA65_INC) && !defined(_WIN32)
     AddSearchPath (IncSearchPath, STRINGIZE (CA65_INC));
 #endif
 
 
 
 
 
-extern SearchPath*      IncSearchPath;          /* Standard include path */
-extern SearchPath*      BinSearchPath;          /* Binary include path */
+extern SearchPaths*     IncSearchPath;          /* Standard include path */
+extern SearchPaths*     BinSearchPath;          /* Binary include path */
 
 
 
 
 
 
 
-SearchPath*     SysIncSearchPath;       /* System include path */
-SearchPath*     UsrIncSearchPath;       /* User include path */
+SearchPaths*    SysIncSearchPath;       /* System include path */
+SearchPaths*    UsrIncSearchPath;       /* User include path */
 
 
 
     AddSubSearchPathFromEnv (SysIncSearchPath, "CC65_HOME", "include");
 
     /* Add some compiled-in search paths if defined at compile time. */
-#ifdef CC65_INC
+#if defined(CC65_INC) && !defined(_WIN32)
     AddSearchPath (SysIncSearchPath, STRINGIZE (CC65_INC));
 #endif
 
 
 
 
 
-extern SearchPath*      SysIncSearchPath;       /* System include path */
-extern SearchPath*      UsrIncSearchPath;       /* User include path */
+extern SearchPaths*     SysIncSearchPath;       /* System include path */
+extern SearchPaths*     UsrIncSearchPath;       /* User include path */
 
 
 
 
 /* Check out if we have a spawn() function on the system, or if we must use
  * our own.
  */
-#if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__DJGPP__)
-#  define HAVE_SPAWN    1
+#if defined(_WIN32)
+#  define HAVE_SPAWN 1
 #else
-#  define NEED_SPAWN   1
+#  define NEED_SPAWN 1
 #endif
 #if defined(_MSC_VER)
 #  pragma warning(disable : 4996)
     }
 
     /* Call the program */
-    Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
+    Status = spawnvp (P_WAIT, Cmd->Name, (const char* const *) Cmd->Args);
 
     /* Check the result code */
     if (Status < 0) {
 
 /*                                                                           */
 /*                                filestat.c                                 */
 /*                                                                           */
-/*                   Replacement for buggy Microsoft code                    */
+/*                       Replacement for Windows code                        */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#if defined(__WATCOMC__) && defined(__NT__)
-#define BUGGY_OS 1
+#if defined(_WIN32)
 #include <errno.h>
 #include <windows.h>
 #endif
 
 
 
-#if defined(BUGGY_OS)
+#if defined(_WIN32)
 
 
 
      * way to express a number > 32 bit (known to me) but is able to do
      * calculations with 64 bit integers, so we need to do it this way.
      */
-    static const ULARGE_INTEGER Offs = { 0xB6109100UL, 0x00000020UL };
+    static const ULARGE_INTEGER Offs = { { 0xB6109100UL, 0x00000020UL } };
     ULARGE_INTEGER V;
     V.LowPart  = T->dwLowDateTime;
     V.HighPart = T->dwHighDateTime;
 
 /*                                                                           */
 /*                                filestat.h                                 */
 /*                                                                           */
-/*                   Replacement for buggy Microsoft code                    */
+/*                       Replacement for Windows code                        */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
 
 /*                                                                           */
 /*                                filetime.c                                 */
 /*                                                                           */
-/*                   Replacement for buggy Microsoft code                    */
+/*                       Replacement for Windows code                        */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
 
 
 
-#if defined(__WATCOMC__) && defined(__NT__)
-#define BUGGY_OS 1
+#if defined(_WIN32)
 #include <errno.h>
 #include <windows.h>
 #else
-#if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__)
-/* The Windows compilers have the file in the wrong directory */
-#  include <sys/utime.h>
-#else
-#  include <sys/types.h>                /* FreeBSD needs this */
-#  include <utime.h>
-#endif
+#include <sys/types.h>                          /* FreeBSD needs this */
+#include <utime.h>
 #endif
 
 
 
 
 
-#if defined(BUGGY_OS)
+#if defined(_WIN32)
 
 
 
      * way to express a number > 32 bit (known to me) but is able to do
      * calculations with 64 bit integers, so we need to do it this way.
      */
-    static const ULARGE_INTEGER Offs = { 0xB6109100UL, 0x00000020UL };
+    static const ULARGE_INTEGER Offs = { { 0xB6109100UL, 0x00000020UL } };
     ULARGE_INTEGER V;
     V.QuadPart = ((unsigned __int64) T + Offs.QuadPart) * 10000000U;
     FT->dwLowDateTime  = V.LowPart;
 
 
 #endif
-
-
-
 
 /*                                                                           */
 /*                                filetime.h                                 */
 /*                                                                           */
-/*                   Replacement for buggy Microsoft code                    */
+/*                       Replacement for Windows code                        */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
 
 
 #include <stdlib.h>
 #include <string.h>
+#if defined(_WIN32)
+#  include <windows.h>
+#endif
 #if defined(_MSC_VER)
 /* Microsoft compiler */
 #  include <io.h>
 
 
 
-static void Add (SearchPath* P, const char* New)
+static void Add (SearchPaths* P, const char* New)
 /* Cleanup a new search path and add it to the list */
 {
     /* Add a clean copy of the path to the collection */
 
 
 
-SearchPath* NewSearchPath (void)
+SearchPaths* NewSearchPath (void)
 /* Create a new, empty search path list */
 {
     return NewCollection ();
 
 
 
-void AddSearchPath (SearchPath* P, const char* NewPath)
+void AddSearchPath (SearchPaths* P, const char* NewPath)
 /* Add a new search path to the end of an existing list */
 {
     /* Allow a NULL path */
 
 
 
-void AddSearchPathFromEnv (SearchPath* P, const char* EnvVar)
+void AddSearchPathFromEnv (SearchPaths* P, const char* EnvVar)
 /* Add a search path from an environment variable to the end of an existing
  * list.
  */
 
 
 
-void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* SubDir)
+void AddSubSearchPathFromEnv (SearchPaths* P, const char* EnvVar, const char* SubDir)
 /* Add a search path from an environment variable, adding a subdirectory to
  * the environment variable value.
  */
 
 
 
-void AddSubSearchPathFromWinBin (SearchPath* P, const char* SubDir)
+void AddSubSearchPathFromWinBin (SearchPaths* P, const char* SubDir)
 {
 /* Windows only:
  * Add a search path from the running binary, adding a subdirectory to
  * the parent directory of the directory containing the binary.
  */
-#if defined(_MSC_VER)
+#if defined(_WIN32)
 
     char Dir[_MAX_PATH];
     char* Ptr;
 
-    if (_get_pgmptr (&Ptr) != 0) {
+    if (GetModuleFileName (NULL, Dir, _MAX_PATH) == 0) {
         return;
     }
-    strcpy (Dir, Ptr);
 
     /* Remove binary name */
     Ptr = strrchr (Dir, '\\');
 }
 
 
-int PushSearchPath (SearchPath* P, const char* NewPath)
+int PushSearchPath (SearchPaths* P, const char* NewPath)
 /* Add a new search path to the head of an existing search path list, provided
  * that it's not already there. If the path is already at the first position,
  * return zero, otherwise return a non zero value.
 
 
 
-void PopSearchPath (SearchPath* P)
+void PopSearchPath (SearchPaths* P)
 /* Remove a search path from the head of an existing search path list */
 {
     /* Remove the path at position 0 */
 
 
 
-char* SearchFile (const SearchPath* P, const char* File)
+char* SearchFile (const SearchPaths* P, const char* File)
 /* Search for a file in a list of directories. Return a pointer to a malloced
  * area that contains the complete path, if found, return 0 otherwise.
  */
     SB_Done (&PathName);
     return Name;
 }
-
-
-
 
 #define  STRINGIZE(arg) _STRINGIZE(arg)
 
 /* A search path is a pointer to the list */
-typedef struct Collection SearchPath;
+typedef struct Collection SearchPaths;
 
 
 
 
 
 
-SearchPath* NewSearchPath (void);
+SearchPaths* NewSearchPath (void);
 /* Create a new, empty search path list */
 
-void AddSearchPath (SearchPath* P, const char* NewPath);
+void AddSearchPath (SearchPaths* P, const char* NewPath);
 /* Add a new search path to the end of an existing list */
 
-void AddSearchPathFromEnv (SearchPath* P, const char* EnvVar);
+void AddSearchPathFromEnv (SearchPaths* P, const char* EnvVar);
 /* Add a search path from an environment variable to the end of an existing
  * list.
  */
 
-void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* SubDir);
+void AddSubSearchPathFromEnv (SearchPaths* P, const char* EnvVar, const char* SubDir);
 /* Add a search path from an environment variable, adding a subdirectory to
  * the environment variable value.
  */
 
-void AddSubSearchPathFromWinBin (SearchPath* P, const char* SubDir);
+void AddSubSearchPathFromWinBin (SearchPaths* P, const char* SubDir);
 /* Windows only:
  * Add a search path from the running binary, adding a subdirectory to
  * the parent directory of the directory containing the binary.
  */
 
-int PushSearchPath (SearchPath* P, const char* NewPath);
+int PushSearchPath (SearchPaths* P, const char* NewPath);
 /* Add a new search path to the head of an existing search path list, provided
  * that it's not already there. If the path is already at the first position,
  * return zero, otherwise return a non zero value.
  */
 
-void PopSearchPath (SearchPath* P);
+void PopSearchPath (SearchPaths* P);
 /* Remove a search path from the head of an existing search path list */
 
-char* SearchFile (const SearchPath* P, const char* File);
+char* SearchFile (const SearchPaths* P, const char* File);
 /* Search for a file in a list of directories. Return a pointer to a malloced
  * area that contains the complete path, if found, return 0 otherwise.
  */
 
  * and precision to such a StrBuf, but *not* using %p would bring up a warning
  * about a wrong argument type each time. Maybe gcc will one day allow custom
  * format specifiers and we can change this ...
+ * However this cheat doesn't work with MinGW as there's no support for %m :-(
  */
+#if defined( __MINGW32__)
+#  pragma GCC diagnostic ignored "-Wformat"
+#endif
 
 
 
 
 
 
 
-SearchPath*      LibSearchPath;         /* Library path */
-SearchPath*      ObjSearchPath;         /* Object file path */
-SearchPath*      CfgSearchPath;         /* Config file path */
+SearchPaths*     LibSearchPath;         /* Library path */
+SearchPaths*     ObjSearchPath;         /* Object file path */
+SearchPaths*     CfgSearchPath;         /* Config file path */
 
-SearchPath*      LibDefaultPath;        /* Default Library path */
-SearchPath*      ObjDefaultPath;        /* Default Object file path */
-SearchPath*      CfgDefaultPath;        /* Default Config file path */
+SearchPaths*     LibDefaultPath;        /* Default Library path */
+SearchPaths*     ObjDefaultPath;        /* Default Object file path */
+SearchPaths*     CfgDefaultPath;        /* Default Config file path */
 
 
 
     AddSubSearchPathFromEnv (CfgDefaultPath, "CC65_HOME", "cfg");
 
     /* Add some compiled-in search paths if defined at compile time. */
-#if defined(LD65_LIB)
+#if defined(LD65_LIB) && !defined(_WIN32)
     AddSearchPath (LibDefaultPath, STRINGIZE (LD65_LIB));
 #endif
-#if defined(LD65_OBJ)
+#if defined(LD65_OBJ) && !defined(_WIN32)
     AddSearchPath (ObjDefaultPath, STRINGIZE (LD65_OBJ));
 #endif
-#if defined(LD65_CFG)
+#if defined(LD65_CFG) && !defined(_WIN32)
     AddSearchPath (CfgDefaultPath, STRINGIZE (LD65_CFG));
 #endif
 
 
 
 
 
-extern SearchPath*      LibSearchPath;          /* Library path */
-extern SearchPath*      ObjSearchPath;          /* Object file path */
-extern SearchPath*      CfgSearchPath;          /* Config file path */
+extern SearchPaths*     LibSearchPath;          /* Library path */
+extern SearchPaths*     ObjSearchPath;          /* Object file path */
+extern SearchPaths*     CfgSearchPath;          /* Config file path */
 
-extern SearchPath*      LibDefaultPath;         /* Default Library path */
-extern SearchPath*      ObjDefaultPath;         /* Default Object file path */
-extern SearchPath*      CfgDefaultPath;         /* Default Config file path */
+extern SearchPaths*     LibDefaultPath;         /* Default Library path */
+extern SearchPaths*     ObjDefaultPath;         /* Default Object file path */
+extern SearchPaths*     CfgDefaultPath;         /* Default Config file path */
 
 
 
 
 #include <string.h>
 #include <stdlib.h>
 #include <fcntl.h>
+#if defined(_WIN32)
+#  define O_INITIAL O_BINARY
+#else
+#  define O_INITIAL 0
+#endif
 #if defined(_MSC_VER)
 /* Microsoft compiler */
 #  include <io.h>
 #  pragma warning(disable : 4996)
-#  define O_INITIAL O_BINARY
 #else
 /* Anyone else */
 #  include <unistd.h>
-#  define O_INITIAL 0
 #endif
 
 /* common */