Index: src/checkin.c
==================================================================
--- src/checkin.c
+++ src/checkin.c
@@ -507,11 +507,11 @@
     blob_read_from_file(&text, zFile);
   }else{
     char zIn[300];
     blob_reset(&text);
     while( fgets(zIn, sizeof(zIn), stdin)!=0 ){
-      char *zUtf8 = fossil_mbcs_to_utf8(zIn);
+      char *zUtf8 = fossil_console_to_utf8(zIn);
       if( zUtf8[0]=='.' && (zUtf8[1]==0 || zUtf8[1]=='\r' || zUtf8[1]=='\n') ){
         fossil_mbcs_free(zUtf8);
         break;
       }
       blob_append(&text, zIn, -1);

Index: src/file.c
==================================================================
--- src/file.c
+++ src/file.c
@@ -34,10 +34,11 @@
 ** On Windows, include the Platform SDK header file.
 */
 #ifdef _WIN32
 # include <direct.h>
 # include <windows.h>
+# include <tchar.h>
 #endif
 
 /*
 ** The file status information from the most recent stat() call.
 **
@@ -69,12 +70,12 @@
   }else{
     return stat(zFilename, buf);
   }
 #else
   int rc = 0;
-  wchar_t *zMbcs = fossil_utf8_to_unicode(zFilename);
-  rc = _wstati64(zMbcs, buf);
+  TCHAR *zMbcs = fossil_utf8_to_mbcs(zFilename);
+  rc = _tstati64(zMbcs, buf);
   fossil_mbcs_free(zMbcs);
   return rc;
 #endif
 }
 
@@ -300,12 +301,12 @@
 /*
 ** Wrapper around the access() system call.
 */
 int file_access(const char *zFilename, int flags){
 #ifdef _WIN32
-  wchar_t *zMbcs = fossil_utf8_to_unicode(zFilename);
-  int rc = _waccess(zMbcs, flags);
+  TCHAR *zMbcs = fossil_utf8_to_mbcs(zFilename);
+  int rc = _taccess(zMbcs, flags);
   fossil_mbcs_free(zMbcs);
 #else
   int rc = access(zFilename, flags);
 #endif
   return rc;
@@ -395,12 +396,12 @@
 /*
 ** Delete a file.
 */
 void file_delete(const char *zFilename){
 #ifdef _WIN32
-  wchar_t *z = fossil_utf8_to_unicode(zFilename);
-  _wunlink(z);
+  TCHAR *z = fossil_utf8_to_mbcs(zFilename);
+  _tunlink(z);
   fossil_mbcs_free(z);
 #else
   unlink(zFilename);
 #endif
 }
@@ -419,12 +420,12 @@
     file_delete(zName);
   }
   if( rc!=1 ){
 #if defined(_WIN32)
     int rc;
-    wchar_t *zMbcs = fossil_utf8_to_unicode(zName);
-    rc = _wmkdir(zMbcs);
+    TCHAR *zMbcs = fossil_utf8_to_mbcs(zName);
+    rc = _tmkdir(zMbcs);
     fossil_mbcs_free(zMbcs);
     return rc;
 #else
     return mkdir(zName, 0755);
 #endif
@@ -579,15 +580,15 @@
 void file_getcwd(char *zBuf, int nBuf){
 #ifdef _WIN32
   char *zPwdUtf8;
   int nPwd;
   int i;
-  wchar_t zPwd[2000];
-  if( _wgetcwd(zPwd, sizeof(zPwd)/sizeof(zPwd[0])-1)==0 ){
+  TCHAR zPwd[2000];
+  if( _tgetcwd(zPwd, sizeof(zPwd)/sizeof(zPwd[0])-1)==0 ){
     fossil_fatal("cannot find the current working directory.");
   }
-  zPwdUtf8 = fossil_unicode_to_utf8(zPwd);
+  zPwdUtf8 = fossil_mbcs_to_utf8(zPwd);
   nPwd = strlen(zPwdUtf8);
   if( nPwd > nBuf-1 ){
     fossil_fatal("pwd too big: max %d\n", nBuf-1);
   }
   for(i=0; zPwdUtf8[i]; i++) if( zPwdUtf8[i]=='\\' ) zPwdUtf8[i] = '/';
@@ -937,14 +938,14 @@
   unsigned int i, j;
   const char *zDir = ".";
   int cnt = 0;
 
 #if defined(_WIN32)
-  wchar_t zTmpPath[MAX_PATH];
+  TCHAR zTmpPath[MAX_PATH];
 
-  if( GetTempPathW(MAX_PATH, zTmpPath) ){
-    azDirs[0] = fossil_unicode_to_utf8(zTmpPath);
+  if( GetTempPath(MAX_PATH, zTmpPath) ){
+    azDirs[0] = fossil_mbcs_to_utf8(zTmpPath);
   }
 
   azDirs[1] = fossil_getenv("TEMP");
   azDirs[2] = fossil_getenv("TMP");
 #endif
@@ -1004,115 +1005,102 @@
   blob_reset(&onDisk);
   return rc==0;
 }
 
 /*
-** Portable unicode implementation of opendir()
+** Portable unicode implementation of opendir() for win32
 */
 #if INTERFACE
 
-#if defined(_WIN32)
-# include <dirent.h>
-# define FOSSIL_DIR _WDIR
-# define fossil_dirent _wdirent
-# define fossil_opendir _wopendir
-# define fossil_readdir _wreaddir
-# define fossil_closedir _wclosedir
-#else
-# include <dirent.h>
-# define FOSSIL_DIR DIR
-# define fossil_dirent dirent
-# define fossil_opendir opendir
-# define fossil_readdir readdir
-# define fossil_closedir closedir
+#include <dirent.h>
+#if defined(_WIN32) && defined(UNICODE)
+#  define dirent _wdirent
+#  define opendir _wopendir
+#  define readdir _wreaddir
+#  define closedir _wclosedir
+#  define DIR _WDIR
 #endif
 
 #endif /* INTERFACE */
-
-
 
 /**************************************************************************
 ** The following routines translate between MBCS and UTF8 on windows.
 ** Since everything is always UTF8 on unix, these routines are no-ops
 ** there.
 */
 
 /*
-** Translate MBCS to UTF8.  Return a pointer to the translated text.
-** Call fossil_mbcs_free() to deallocate any memory used to store the
-** returned pointer when done.
+** Translate unicode/mbcs to UTF8.  Return a pointer to the translated
+** text. Call fossil_mbcs_free() to deallocate any memory used to store
+** the returned pointer when done.
+*/
+char *fossil_mbcs_to_utf8(const void *zMbcs){
+#ifdef _WIN32
+#ifdef UNICODE
+  int nByte = WideCharToMultiByte(CP_UTF8, 0, zMbcs, -1, 0, 0, 0, 0);
+  char *zUtf = sqlite3_malloc( nByte );
+  if( zUtf==0 ){
+    return 0;
+  }
+  WideCharToMultiByte(CP_UTF8, 0, zMbcs, -1, zUtf, nByte, 0, 0);
+  return zUtf;
+#else
+  extern char *sqlite3_win32_mbcs_to_utf8(const char*);
+  return sqlite3_win32_mbcs_to_utf8(zMbcs);
+#endif
+#else
+  return (char*)zMbcs;  /* No-op on unix */
+#endif
+}
+
+/*
+** Translate mbcs to UTF8.  Return a pointer to the translated text.
+** Call fossil_mbcs_free() to deallocate any memory used to store
+** the returned pointer when done.
 */
-char *fossil_mbcs_to_utf8(const char *zMbcs){
+char *fossil_console_to_utf8(const void *zMbcs){
 #ifdef _WIN32
   extern char *sqlite3_win32_mbcs_to_utf8(const char*);
   return sqlite3_win32_mbcs_to_utf8(zMbcs);
 #else
   return (char*)zMbcs;  /* No-op on unix */
 #endif
 }
 
 /*
-** Translate Unicode to UTF8.  Return a pointer to the translated text.
-** Call fossil_mbcs_free() to deallocate any memory used to store the
-** returned pointer when done.
+** Translate UTF8 to unicode/mbcs for use in system calls.  Return a
+** pointer to the translated text..  Call fossil_mbcs_free() to
+** deallocate any memory used to store the returned pointer when done.
 */
-char *fossil_unicode_to_utf8(const void *zUnicode){
+void *fossil_utf8_to_mbcs(const char *zUtf8){
 #ifdef _WIN32
-  int nByte = WideCharToMultiByte(CP_UTF8, 0, zUnicode, -1, 0, 0, 0, 0);
-  char *zUtf = sqlite3_malloc( nByte );
-  if( zUtf==0 ){
-    return 0;
-  }
-  WideCharToMultiByte(CP_UTF8, 0, zUnicode, -1, zUtf, nByte, 0, 0);
-  return zUtf;
-#else
-  return (char *)zUnicode;  /* No-op on unix */
-#endif
-}
-
-/*
-** Translate UTF8 to MBCS for use in system calls.  Return a pointer to the
-** translated text..  Call fossil_mbcs_free() to deallocate any memory
-** used to store the returned pointer when done.
-*/
-char *fossil_utf8_to_mbcs(const char *zUtf8){
-#ifdef _WIN32
-  extern char *sqlite3_win32_utf8_to_mbcs(const char*);
-  return sqlite3_win32_utf8_to_mbcs(zUtf8);
-#else
-  return (char*)zUtf8;  /* No-op on unix */
-#endif
-}
-
-/*
-** Translate UTF8 to unicode for use in system calls.  Return a pointer to the
-** translated text..  Call fossil_mbcs_free() to deallocate any memory
-** used to store the returned pointer when done.
-*/
-void *fossil_utf8_to_unicode(const char *zUtf8){
-#ifdef _WIN32
+#ifdef UNICODE
   int nByte = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, 0, 0);
   wchar_t *zUnicode = sqlite3_malloc( nByte * 2 );
   if( zUnicode==0 ){
     return 0;
   }
   MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zUnicode, nByte);
   return zUnicode;
 #else
-  return (void *)zUtf8;  /* No-op on unix */
+  extern char *sqlite3_win32_utf8_to_mbcs(const char*);
+  return sqlite3_win32_utf8_to_mbcs(zUtf8);
+#endif
+#else
+  return (char*)zUtf8;  /* No-op on unix */
 #endif
 }
 
 /*
 ** Return the value of an environment variable as UTF8.
 */
 char *fossil_getenv(const char *zName){
 #ifdef _WIN32
-  wchar_t *uName = fossil_utf8_to_unicode(zName);
-  void *zValue = _wgetenv(uName);
+  TCHAR *uName = fossil_utf8_to_mbcs(zName);
+  void *zValue = _tgetenv(uName);
   fossil_mbcs_free(uName);
-  if( zValue ) zValue = fossil_unicode_to_utf8(zValue);
+  if( zValue ) zValue = fossil_mbcs_to_utf8(zValue);
 #else
   char *zValue = getenv(zName);
 #endif
   return zValue;
 }
@@ -1197,15 +1185,15 @@
 /*
 ** Like fopen() but always takes a UTF8 argument.
 */
 FILE *fossil_fopen(const char *zName, const char *zMode){
 #ifdef _WIN32
-  wchar_t *uMode = fossil_utf8_to_unicode(zMode);
-  wchar_t *uName = fossil_utf8_to_unicode(zName);
-  FILE *f = _wfopen(uName, uMode);
+  TCHAR *uMode = fossil_utf8_to_mbcs(zMode);
+  TCHAR *uName = fossil_utf8_to_mbcs(zName);
+  FILE *f = _tfopen(uName, uMode);
   fossil_mbcs_free(uName);
   fossil_mbcs_free(uMode);
 #else
   FILE *f = fopen(zName, zMode);
 #endif
   return f;
 }

Index: src/main.c
==================================================================
--- src/main.c
+++ src/main.c
@@ -197,10 +197,13 @@
   const char *azAuxVal[MX_AUX];  /* Value of each aux() or option() value */
   const char **azAuxOpt[MX_AUX]; /* Options of each option() value */
   int anAuxCols[MX_AUX];         /* Number of columns for option() values */
 
   int allowSymlinks;             /* Cached "allow-symlinks" option */
+#ifdef _WIN32
+  int isNT;
+#endif
 
 #ifdef FOSSIL_ENABLE_JSON
   struct FossilJsonBits {
     int isJsonMode;            /* True if running in JSON mode, else
                                   false. This changes how errors are
@@ -470,24 +473,20 @@
   char **newArgv;           /* New expanded g.argv under construction */
   char const * zFileName;   /* input file name */
   FILE * zInFile;           /* input FILE */
   int foundBom = -1;        /* -1= not searched yet, 0 = no; 1=yes */
 #ifdef _WIN32
-  wchar_t buf[MAX_PATH];
+  TCHAR buf[MAX_PATH];
 #endif
 
   g.argc = argc;
   g.argv = argv;
 #ifdef _WIN32
   parse_windows_command_line(&g.argc, &g.argv);
-  GetModuleFileNameW(NULL, buf, MAX_PATH);
-  g.argv[0] = fossil_unicode_to_utf8(buf);
-#ifdef UNICODE
-  for(i=1; i<g.argc; i++) g.argv[i] = fossil_unicode_to_utf8(g.argv[i]);
-#else
+  GetModuleFileName(NULL, buf, MAX_PATH);
+  g.argv[0] = fossil_mbcs_to_utf8(buf);
   for(i=1; i<g.argc; i++) g.argv[i] = fossil_mbcs_to_utf8(g.argv[i]);
-#endif
 #endif
   for(i=1; i<g.argc-1; i++){
     z = g.argv[i];
     if( z[0]!='-' ) continue;
     z++;
@@ -530,11 +529,11 @@
     if((n>1) && ('\r'==z[n-2])){
       if(n==2) continue /*empty line*/;
       z[n-2] = 0;
     }
     if (!foundBom) {
-      z = fossil_mbcs_to_utf8(z);
+      z = fossil_console_to_utf8(z);
     }
     newArgv[j++] = z;
     if( z[0]=='-' ){
       for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
       if( z[k] ){
@@ -557,13 +556,21 @@
 int main(int argc, char **argv)
 {
   const char *zCmdName = "unknown";
   int idx;
   int rc;
+#ifdef _WIN32
+  OSVERSIONINFOA sInfo;
+#endif
 
   sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
   memset(&g, 0, sizeof(g));
+#ifdef _WIN32
+  sInfo.dwOSVersionInfoSize = sizeof(sInfo);
+  GetVersionExA(&sInfo);
+  g.isNT = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT;
+#endif
   g.now = time(0);
 #ifdef FOSSIL_ENABLE_JSON
 #if defined(NDEBUG)
   g.json.errorDetailParanoia = 2 /* FIXME: make configurable
                                     One problem we have here is that this
@@ -831,23 +838,30 @@
 ** This function implements a cross-platform "system()" interface.
 */
 int fossil_system(const char *zOrigCmd){
   int rc;
 #if defined(_WIN32)
-  /* On windows, we have to put double-quotes around the entire command.
+  /* On windows NT, we have to put double-quotes around the entire command.
   ** Who knows why - this is just the way windows works.
   */
-  char *zNewCmd = mprintf("\"%s\"", zOrigCmd);
-  wchar_t *zUnicode = fossil_utf8_to_unicode(zNewCmd);
+  char *zNewCmd;
+  TCHAR *zMbcs;
+
+  if (g.isNT) {
+    zNewCmd = mprintf("\"%s\"", zOrigCmd);
+  } else {
+    zNewCmd = mprintf("%s", zOrigCmd);
+  }
+  zMbcs = fossil_utf8_to_mbcs(zNewCmd);
   if( g.fSystemTrace ) {
     char *zOut = mprintf("SYSTEM: %s\n", zNewCmd);
     fossil_puts(zOut, 1);
     fossil_free(zOut);
   }
-  rc = _wsystem(zUnicode);
-  fossil_mbcs_free(zUnicode);
-  free(zNewCmd);
+  rc = _tsystem(zMbcs);
+  fossil_mbcs_free(zMbcs);
+  fossil_free(zNewCmd);
 #else
   /* On unix, evaluate the command directly.
   */
   if( g.fSystemTrace ) fprintf(stderr, "SYSTEM: %s\n", zOrigCmd);
   rc = system(zOrigCmd);

Index: src/makemake.tcl
==================================================================
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -477,11 +477,11 @@
 endif
 
 #### We add the -static option here so that we can build a static
 #    executable that will run in a chroot jail.
 #
-LIB = -static
+LIB = -lunicows -static
 
 # OpenSSL: Add the necessary libraries required, if enabled.
 ifdef FOSSIL_ENABLE_SSL
 LIB += -lssl -lcrypto -lgdi32
 endif
@@ -904,11 +904,11 @@
 INCL   = -I. -I$(SRCDIR) -I$B\win\include -I$(MSCDIR)\extra\include -I$(ZINCDIR)
 
 CFLAGS = -nologo -MT -O2
 BCC    = $(CC) $(CFLAGS)
 TCC    = $(CC) -c $(CFLAGS) -DUNICODE -D_UNICODE $(MSCDEF) $(SSL) $(INCL)
-LIBS   = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
+LIBS   = unicows.lib $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
 }
 regsub -all {[-]D} $SQLITE_OPTIONS {/D} MSC_SQLITE_OPTIONS
 writeln "SQLITE_OPTIONS = $MSC_SQLITE_OPTIONS\n"
 writeln -nonewline "SRC   = "

Index: src/popen.c
==================================================================
--- src/popen.c
+++ src/popen.c
@@ -65,17 +65,17 @@
 ** and stderr channels for that process to use.
 **
 ** Return the number of errors.
 */
 static int win32_create_child_process(
-  wchar_t *zCmd,       /* The command that the child process will run */
+  TCHAR *zCmd,         /* The command that the child process will run */
   HANDLE hIn,          /* Standard input */
   HANDLE hOut,         /* Standard output */
   HANDLE hErr,         /* Standard error */
   DWORD *pChildPid     /* OUT: Child process handle */
 ){
-  STARTUPINFOW si;
+  STARTUPINFO si;
   PROCESS_INFORMATION pi;
   BOOL rc;
 
   memset(&si, 0, sizeof(si));
   si.cb = sizeof(si);
@@ -84,11 +84,11 @@
   si.hStdInput  = hIn;
   SetHandleInformation(hOut, HANDLE_FLAG_INHERIT, TRUE);
   si.hStdOutput = hOut;
   SetHandleInformation(hErr, HANDLE_FLAG_INHERIT, TRUE);
   si.hStdError  = hErr;
-  rc = CreateProcessW(
+  rc = CreateProcess(
      NULL,  /* Application Name */
      zCmd,  /* Command-line */
      NULL,  /* Process attributes */
      NULL,  /* Thread attributes */
      TRUE,  /* Inherit Handles */
@@ -139,11 +139,11 @@
   if( !CreatePipe(&hStdinRd, &hStdinWr, &saAttr, 4096) ){
     win32_fatal_error("cannot create pipe for stdin");
   }
   SetHandleInformation( hStdinWr, HANDLE_FLAG_INHERIT, FALSE);
   
-  win32_create_child_process(fossil_utf8_to_unicode(zCmd),
+  win32_create_child_process(fossil_utf8_to_mbcs(zCmd),
                              hStdinRd, hStdoutWr, hStderr,&childPid);
   *pChildPid = childPid;
   *pfdIn = _open_osfhandle(PTR_TO_INT(hStdoutRd), 0);
   fd = _open_osfhandle(PTR_TO_INT(hStdinWr), 0);
   *ppOut = _fdopen(fd, "w");

Index: src/rebuild.c
==================================================================
--- src/rebuild.c
+++ src/rebuild.c
@@ -608,11 +608,11 @@
     if( activateWal ){
       db_multi_exec("PRAGMA journal_mode=WAL;");
     }
   }
   if( showStats ){
-    static struct { int idx; const char *zLabel; } aStat[] = {
+    static const struct { int idx; const char *zLabel; } aStat[] = {
        { CFTYPE_ANY,       "Artifacts:" },
        { CFTYPE_MANIFEST,  "Manifests:" },
        { CFTYPE_CLUSTER,   "Clusters:" },
        { CFTYPE_CONTROL,   "Tags:" },
        { CFTYPE_WIKI,      "Wikis:" },
@@ -817,28 +817,28 @@
 /*
 ** Recursively read all files from the directory zPath and install
 ** every file read as a new artifact in the repository.
 */
 void recon_read_dir(char *zPath){
-  FOSSIL_DIR *d;
-  struct fossil_dirent *pEntry;
+  DIR *d;
+  struct dirent *pEntry;
   Blob aContent; /* content of the just read artifact */
   static int nFileRead = 0;
-  void *zUnicodePath;
+  void *zMbcsPath;
   char *zUtf8Name;
 
-  zUnicodePath = fossil_utf8_to_unicode(zPath);
-  d = fossil_opendir(zUnicodePath);
+  zMbcsPath = fossil_utf8_to_mbcs(zPath);
+  d = opendir(zMbcsPath);
   if( d ){
-    while( (pEntry=fossil_readdir(d))!=0 ){
+    while( (pEntry=readdir(d))!=0 ){
       Blob path;
       char *zSubpath;
 
       if( pEntry->d_name[0]=='.' ){
         continue;
       }
-      zUtf8Name = fossil_unicode_to_utf8(pEntry->d_name);
+      zUtf8Name = fossil_mbcs_to_utf8(pEntry->d_name);
       zSubpath = mprintf("%s/%s", zPath, zUtf8Name);
       fossil_mbcs_free(zUtf8Name);
       if( file_isdir(zSubpath)==1 ){
         recon_read_dir(zSubpath);
       }
@@ -853,16 +853,16 @@
       blob_reset(&aContent);
       free(zSubpath);
       fossil_print("\r%d", ++nFileRead);
       fflush(stdout);
     }
-    fossil_closedir(d);
+    closedir(d);
   }else {
     fossil_panic("encountered error %d while trying to open \"%s\".",
                   errno, g.argv[3]);
   }
-  fossil_mbcs_free(zUnicodePath);
+  fossil_mbcs_free(zMbcsPath);
 }
 
 /*
 ** COMMAND: reconstruct*
 **

Index: src/sqlite3.c
==================================================================
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -30844,12 +30844,14 @@
 ** API as long as we don't call it when running Win95/98/ME.  A call to
 ** this routine is used to determine if the host is Win95/98/ME or
 ** WinNT/2K/XP so that we will know whether or not we can safely call
 ** the LockFileEx() API.
 */
-#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
+#if !defined(SQLITE_WIN32_HAS_ANSI)
 # define isNT()  (1)
+#elif !defined(SQLITE_WIN32_HAS_WIDE)
+# define isNT()  (0)
 #else
   static int isNT(void){
     if( sqlite3_os_type==0 ){
       OSVERSIONINFOA sInfo;
       sInfo.dwOSVersionInfoSize = sizeof(sInfo);

Index: src/vfile.c
==================================================================
--- src/vfile.c
+++ src/vfile.c
@@ -376,14 +376,14 @@
 ** Any files or directories that match the glob pattern pIgnore are 
 ** excluded from the scan.  Name matching occurs after the first
 ** nPrefix characters are elided from the filename.
 */
 void vfile_scan(Blob *pPath, int nPrefix, int allFlag, Glob *pIgnore){
-  FOSSIL_DIR *d;
+  DIR *d;
   int origSize;
   const char *zDir;
-  struct fossil_dirent *pEntry;
+  struct dirent *pEntry;
   int skipAll = 0;
   static Stmt ins;
   static int depth = 0;
   void *zMbcs;
 
@@ -402,22 +402,22 @@
     );
   }
   depth++;
 
   zDir = blob_str(pPath);
-  zMbcs = fossil_utf8_to_unicode(zDir);
-  d = fossil_opendir(zMbcs);
+  zMbcs = fossil_utf8_to_mbcs(zDir);
+  d = opendir(zMbcs);
   if( d ){
-    while( (pEntry=fossil_readdir(d))!=0 ){
+    while( (pEntry=readdir(d))!=0 ){
       char *zPath;
       char *zUtf8;
       if( pEntry->d_name[0]=='.' ){
         if( !allFlag ) continue;
         if( pEntry->d_name[1]==0 ) continue;
         if( pEntry->d_name[1]=='.' && pEntry->d_name[2]==0 ) continue;
       }
-      zUtf8 = fossil_unicode_to_utf8(pEntry->d_name);
+      zUtf8 = fossil_mbcs_to_utf8(pEntry->d_name);
       blob_appendf(pPath, "/%s", zUtf8);
       fossil_mbcs_free(zUtf8);
       zPath = blob_str(pPath);
       if( glob_match(pIgnore, &zPath[nPrefix+1]) ){
         /* do nothing */
@@ -430,11 +430,11 @@
         db_step(&ins);
         db_reset(&ins);
       }
       blob_resize(pPath, origSize);
     }
-    fossil_closedir(d);
+    closedir(d);
   }
   fossil_mbcs_free(zMbcs);
 
   depth--;
   if( depth==0 ){

Index: src/winhttp.c
==================================================================
--- src/winhttp.c
+++ src/winhttp.c
@@ -19,11 +19,11 @@
 ** for windows. It also implements a Windows Service which allows the HTTP
 ** server to be run without any user logged on.
 */
 #include "config.h"
 #ifdef _WIN32
-/* This code is for win32 only */
+/* This code is for win32/unicode only */
 #include <windows.h>
 #include "winhttp.h"
 
 /*
 ** The HttpRequest structure holds information about each incoming
@@ -129,15 +129,10 @@
   file_delete(zRequestFName);
   file_delete(zReplyFName);
   free(p);
 }
 
-#if !defined(UNICODE)
-#  define fossil_unicode_to_utf8 fossil_mbcs_to_utf8
-#  define fossil_utf8_to_unicode fossil_utf8_to_mbcs
-#endif
-
 /*
 ** Start a listening socket and process incoming HTTP requests on
 ** that socket.
 */
 void win32_http_server(
@@ -199,11 +194,11 @@
     }
   }
   if( !GetTempPath(MAX_PATH, zTmpPath) ){
     fossil_fatal("unable to get path to the temporary directory.");
   }
-  zTempPrefix = mprintf("%sfossil_server_P%d_", fossil_unicode_to_utf8(zTmpPath), iPort);
+  zTempPrefix = mprintf("%sfossil_server_P%d_", fossil_mbcs_to_utf8(zTmpPath), iPort);
   fossil_print("Listening for HTTP requests on TCP port %d\n", iPort);
   if( zBrowser ){
     zBrowser = mprintf(zBrowser, iPort);
     fossil_print("Launch webbrowser: %s\n", zBrowser);
     fossil_system(zBrowser);
@@ -303,11 +298,11 @@
              0,
              NULL
            );
   }
   if( nMsg ){
-    zMsg = fossil_unicode_to_utf8(tmp);
+    zMsg = fossil_mbcs_to_utf8(tmp);
   }else{
     fossil_fatal("unable to get system error message.");
   }
   if( tmp ){
     LocalFree((HLOCAL) tmp);
@@ -561,10 +556,15 @@
 void cmd_win32_service(void){
   int n;
   const char *zMethod;
   const char *zSvcName = "Fossil-DSCM";    /* Default service name */
 
+#ifdef _WIN32
+  if( !g.isNT ) {
+    fossil_fatal("%s command not support on Windows 9x", g.argv[1]);
+  }
+#endif
   if( g.argc<3 ){
     usage("create|delete|show|start|stop ...");
   }
   zMethod = g.argv[2];
   n = strlen(zMethod);
@@ -630,22 +630,22 @@
     /* Create the service. */
     hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
     if( !hScm ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
     hSvc = CreateService(
              hScm,                                    /* Handle to the SCM */
-             fossil_utf8_to_unicode(zSvcName),           /* Name of the service */
-             fossil_utf8_to_unicode(zDisplay),           /* Display name */
+             fossil_utf8_to_mbcs(zSvcName),           /* Name of the service */
+             fossil_utf8_to_mbcs(zDisplay),           /* Display name */
              SERVICE_ALL_ACCESS,                      /* Desired access */
              SERVICE_WIN32_OWN_PROCESS,               /* Service type */
              dwStartType,                             /* Start type */
              SERVICE_ERROR_NORMAL,                    /* Error control */
-             fossil_utf8_to_unicode(blob_str(&binPath)), /* Binary path */
+             fossil_utf8_to_mbcs(blob_str(&binPath)), /* Binary path */
              NULL,                                    /* Load ordering group */
              NULL,                                    /* Tag value */
              NULL,                                    /* Service dependencies */
-             fossil_utf8_to_unicode(zUsername),          /* Service account */
-             fossil_utf8_to_unicode(zPassword)           /* Account password */
+             fossil_utf8_to_mbcs(zUsername),          /* Service account */
+             fossil_utf8_to_mbcs(zPassword)           /* Account password */
            );
     if( !hSvc ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
     /* Set the service description. */
     ChangeServiceConfig2(hSvc, SERVICE_CONFIG_DESCRIPTION, &svcDescr);
     fossil_print("Service '%s' successfully created.\n", zSvcName);
@@ -664,11 +664,11 @@
     }else if( g.argc>4 ){
       fossil_fatal("to much arguments for delete method.");
     }
     hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
     if( !hScm ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
-    hSvc = OpenService(hScm, fossil_utf8_to_unicode(zSvcName), SERVICE_ALL_ACCESS);
+    hSvc = OpenService(hScm, fossil_utf8_to_mbcs(zSvcName), SERVICE_ALL_ACCESS);
     if( !hSvc ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
     QueryServiceStatus(hSvc, &sstat);
     if( sstat.dwCurrentState!=SERVICE_STOPPED ){
       fossil_print("Stopping service '%s'", zSvcName);
       if( sstat.dwCurrentState!=SERVICE_STOP_PENDING ){
@@ -732,11 +732,11 @@
     }else if( g.argc>4 ){
       fossil_fatal("to much arguments for show method.");
     }
     hScm = OpenSCManager(NULL, NULL, GENERIC_READ);
     if( !hScm ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
-    hSvc = OpenService(hScm, fossil_utf8_to_unicode(zSvcName), GENERIC_READ);
+    hSvc = OpenService(hScm, fossil_utf8_to_mbcs(zSvcName), GENERIC_READ);
     if( !hSvc ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
     /* Get the service configuration */
     bStatus = QueryServiceConfig(hSvc, NULL, 0, &nRequired);
     if( !bStatus && GetLastError()!=ERROR_INSUFFICIENT_BUFFER ){
       fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
@@ -784,19 +784,19 @@
       case SERVICE_PAUSED:           zSvcState = zSvcStates[6]; break;
     }
     /* Print service information to terminal */
     fossil_print("Service name .......: %s\n", zSvcName);
     fossil_print("Display name .......: %s\n",
-                 fossil_unicode_to_utf8(pSvcConfig->lpDisplayName));
+                 fossil_mbcs_to_utf8(pSvcConfig->lpDisplayName));
     fossil_print("Service description : %s\n",
-                 fossil_unicode_to_utf8(pSvcDescr->lpDescription));
+                 fossil_mbcs_to_utf8(pSvcDescr->lpDescription));
     fossil_print("Service type .......: %s.\n", zSvcType);
     fossil_print("Service start type .: %s.\n", zSvcStartType);
     fossil_print("Binary path name ...: %s\n",
-                 fossil_unicode_to_utf8(pSvcConfig->lpBinaryPathName));
+                 fossil_mbcs_to_utf8(pSvcConfig->lpBinaryPathName));
     fossil_print("Service username ...: %s\n",
-                 fossil_unicode_to_utf8(pSvcConfig->lpServiceStartName));
+                 fossil_mbcs_to_utf8(pSvcConfig->lpServiceStartName));
     fossil_print("Current state ......: %s.\n", zSvcState);
     /* Cleanup */
     fossil_free(pSvcConfig);
     fossil_free(pSvcDescr);
     CloseServiceHandle(hSvc);
@@ -814,11 +814,11 @@
     }else if( g.argc>4 ){
       fossil_fatal("to much arguments for start method.");
     }
     hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
     if( !hScm ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
-    hSvc = OpenService(hScm, fossil_utf8_to_unicode(zSvcName), SERVICE_ALL_ACCESS);
+    hSvc = OpenService(hScm, fossil_utf8_to_mbcs(zSvcName), SERVICE_ALL_ACCESS);
     if( !hSvc ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
     QueryServiceStatus(hSvc, &sstat);
     if( sstat.dwCurrentState!=SERVICE_RUNNING ){
       fossil_print("Starting service '%s'", zSvcName);
       if( sstat.dwCurrentState!=SERVICE_START_PENDING ){
@@ -850,11 +850,11 @@
     }else if( g.argc>4 ){
       fossil_fatal("to much arguments for stop method.");
     }
     hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
     if( !hScm ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
-    hSvc = OpenService(hScm, fossil_utf8_to_unicode(zSvcName), SERVICE_ALL_ACCESS);
+    hSvc = OpenService(hScm, fossil_utf8_to_mbcs(zSvcName), SERVICE_ALL_ACCESS);
     if( !hSvc ) fossil_fatal(zErrFmt, zSvcName, win32_get_last_errmsg());
     QueryServiceStatus(hSvc, &sstat);
     if( sstat.dwCurrentState!=SERVICE_STOPPED ){
       fossil_print("Stopping service '%s'", zSvcName);
       if( sstat.dwCurrentState!=SERVICE_STOP_PENDING ){

Index: win/Makefile.mingw
==================================================================
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -149,11 +149,11 @@
 endif
 
 #### We add the -static option here so that we can build a static
 #    executable that will run in a chroot jail.
 #
-LIB = -static
+LIB = -lunicows -static
 
 # OpenSSL: Add the necessary libraries required, if enabled.
 ifdef FOSSIL_ENABLE_SSL
 LIB += -lssl -lcrypto -lgdi32
 endif

Index: win/Makefile.msc
==================================================================
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -35,11 +35,11 @@
 INCL   = -I. -I$(SRCDIR) -I$B\win\include -I$(MSCDIR)\extra\include -I$(ZINCDIR)
 
 CFLAGS = -nologo -MT -O2
 BCC    = $(CC) $(CFLAGS)
 TCC    = $(CC) -c $(CFLAGS) -DUNICODE -D_UNICODE $(MSCDEF) $(SSL) $(INCL)
-LIBS   = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
+LIBS   = unicows.lib $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
 
 SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
 
 SRC   = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c glob_.c graph_.c gzip_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c json_.c json_artifact_.c json_branch_.c json_config_.c json_diff_.c json_dir_.c json_finfo_.c json_login_.c json_query_.c json_report_.c json_tag_.c json_timeline_.c json_user_.c json_wiki_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c path_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c wysiwyg_.c xfer_.c xfersetup_.c zip_.c 

Index: win/include/dirent.h
==================================================================
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -379,12 +379,13 @@
 #ifdef UNICODE
 #  undef dirent
 #  undef opendir
 #  undef readdir
 #  undef closedir
+#  undef rewinddir
 #  undef DIR
 #endif
 
 #ifdef __cplusplus
 }
 #endif
 #endif /*DIRENT_H*/