diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/config.h kpathsea/config.h
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/config.h	Fri Feb 19 11:43:36 2016
+++ kpathsea/config.h	Tue Jun 28 09:18:31 2016
@@ -95,12 +95,13 @@
   but before "lib.h". FP.
 */
 #if defined (WIN32) || defined (_WIN32)
-#include <kpathsea/knj.h>
 #ifdef __MINGW32__
 #include <kpathsea/mingw32.h>
 #else
 #include <kpathsea/win32lib.h>
 #endif
+#include <kpathsea/types.h>
+#include <kpathsea/knj.h>
 #endif
 
 #if defined(WIN32) || defined(WRAP_SNPRINTF)

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/db.c kpathsea/db.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/db.c	Fri Feb 19 11:43:36 2016
+++ kpathsea/db.c	Tue Jun 28 00:25:44 2016
@@ -106,7 +106,7 @@
 
 #if defined(WIN32)
       for (pp = line; *pp; pp++) {
-        if (IS_KANJI(pp))
+        if (kpathsea_IS_KANJI(kpse, pp))
           pp++;
         else
           *pp = TRANSFORM(*pp);

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/elt-dirs.c kpathsea/elt-dirs.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/elt-dirs.c	Fri Feb 19 11:43:36 2016
+++ kpathsea/elt-dirs.c	Tue Jun 28 07:12:52 2016
@@ -133,7 +133,7 @@
 #if defined (WIN32)
   strcpy(dirname, FN_STRING(name));
   strcat(dirname, "/*.*");         /* "*.*" or "*" -- seems equivalent. */
-  get_wstring_from_fsyscp(dirname, dirnamew);
+  get_wstring_from_mbstring(kpse->File_system_codepage, dirname, dirnamew);
   hnd = FindFirstFileW(dirnamew, &find_file_data);
 
   if (hnd == INVALID_HANDLE_VALUE) {
@@ -158,7 +158,7 @@
       int links;
 
       /* Construct the potential subdirectory name.  */
-      potname = get_fsyscp_from_wstring(find_file_data.cFileName, potname=NULL);
+      potname = get_mbstring_from_wstring(kpse->File_system_codepage, find_file_data.cFileName, potname=NULL);
       fn_str_grow (&name, potname);
       free(potname);
 
@@ -368,7 +368,7 @@
   for (i = 0; elt[i]; i++) {
     if (elt[i] == '\\')
       elt[i] = '/';
-    else if (IS_KANJI(elt + i))
+    else if (kpathsea_IS_KANJI(kpse, elt + i))
       i++;
   }
 #endif

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/expand.c kpathsea/expand.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/expand.c	Fri Feb 19 11:43:36 2016
+++ kpathsea/expand.c	Tue Jun 28 00:29:10 2016
@@ -226,7 +226,7 @@
   for (p = zpath; *p; p++)
     if (*p == '\\')
       *p = '/';
-    else if (IS_KANJI(p))
+    else if (kpathsea_IS_KANJI(kpse, p))
       p++;
 
   ypath = zpath;
@@ -343,7 +343,7 @@
                 for (p+=2; *p!='}';++p);
         }
 #if defined(WIN32)
-        else if (IS_KANJI(p))
+        else if (kpathsea_IS_KANJI(kpse, p))
             p++;
 #endif
     }

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/find-suffix.c kpathsea/find-suffix.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/find-suffix.c	Fri Feb 19 11:43:36 2016
+++ kpathsea/find-suffix.c	Tue Jun 28 00:31:14 2016
@@ -47,10 +47,6 @@
   for (p = dot_pos + 1; *p; p++) {
     if (IS_DIR_SEP (*p))
       return NULL;
-#if defined(WIN32)
-    else if (IS_KANJI(p))
-      p++;
-#endif
   }
 
   return dot_pos + 1;

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/hash.c kpathsea/hash.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/hash.c	Fri Feb 19 11:43:36 2016
+++ kpathsea/hash.c	Tue Jun 28 16:42:44 2016
@@ -35,7 +35,7 @@
   /* Our keys aren't often anagrams of each other, so no point in
      weighting the characters.  */
   while (*key != 0)
-#if defined(WIN32)
+#if defined (WIN32) && defined (KPSE_COMPAT_API)
     if (IS_KANJI(key)) {
       n = (n + n + (unsigned)(*key++)) % table.size;
       n = (n + n + (unsigned)(*key++)) % table.size;

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/knj.c kpathsea/knj.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/knj.c	Sun Apr 10 10:35:11 2016
+++ kpathsea/knj.c	Tue Jun 28 09:11:44 2016
@@ -20,36 +20,18 @@
 #include <kpathsea/debug.h>
 #include <wchar.h>
 
-int is_cp932_system, file_system_codepage;
-
-int isknj(int c)
-{
-  c &= 0xff;
-  switch (is_cp932_system) {
-  case 932:
-    return((c>=0x81 && c<=0x9f) || (c>=0xe0 && c<=0xfc));
-  case 936:
-    return(c>=0x81 && c<=0xfe);
-  case 950:
-    return((c>=0xa1 && c<=0xc6) || (c>=0xc9 && c<=0xf9));
-  default:
-    return(0);
-  }
-}
-
-int isknj2(int c)
+/*
+  popen by file system codepage
+*/
+static int
+is_include_space(const char *s)
 {
-  c &= 0xff;
-  switch (is_cp932_system) {
-  case 932:
-    return(c>=0x40 && c<=0xfc && c!=0x7f);
-  case 936:
-    return(c>=0x40 && c<=0xfe && c!=0x7f);
-  case 950:
-    return((c>=0x40 && c<=0x7e) || (c>=0xa1 && c<=0xfe));
-  default:
-    return(0);
-  }
+    char *p;
+    p = strchr(s, ' ');
+    if(p) return 1;
+    p = strchr(s, '\t');
+    if(p) return 1;
+    return 0;
 }
 
 /*
@@ -96,27 +78,55 @@
   return mbstr;
 }
 
+int
+kpathsea_isknj(kpathsea kpse, int c)
+{
+  c &= 0xff;
+  switch (kpse->Is_cp932_system) {
+  case 932:
+    return((c>=0x81 && c<=0x9f) || (c>=0xe0 && c<=0xfc));
+  case 936:
+    return(c>=0x81 && c<=0xfe);
+  case 950:
+    return((c>=0xa1 && c<=0xc6) || (c>=0xc9 && c<=0xf9));
+  default:
+    return(0);
+  }
+}
+
+int
+kpathsea_isknj2(kpathsea kpse, int c)
+{
+  c &= 0xff;
+  switch (kpse->Is_cp932_system) {
+  case 932:
+    return(c>=0x40 && c<=0xfc && c!=0x7f);
+  case 936:
+    return(c>=0x40 && c<=0xfe && c!=0x7f);
+  case 950:
+    return((c>=0x40 && c<=0x7e) || (c>=0xa1 && c<=0xfe));
+  default:
+    return(0);
+  }
+}
+
 /*
   xfopen by file system codepage
 */
 FILE *
-fsyscp_xfopen (const char *filename, const char *mode)
+kpathsea_fsyscp_xfopen (kpathsea kpse, const char *filename, const char *mode)
 {
     FILE *f;
     wchar_t *fnamew, modew[4];
     int i;
-#if defined (KPSE_COMPAT_API)
-    kpathsea kpse;
-#endif
+
     assert(filename && mode);
 
-    fnamew = get_wstring_from_fsyscp(filename, fnamew=NULL);
+    fnamew = get_wstring_from_mbstring(kpse->File_system_codepage, filename, fnamew=NULL);
     for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */
     f = _wfopen(fnamew, modew);
     if (f == NULL)
         FATAL_PERROR(filename);
-#if defined (KPSE_COMPAT_API)
-    kpse = kpse_def;
     if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) {
         DEBUGF_START ();
         fprintf (stderr, "fsyscp_xfopen(%s [", filename);
@@ -128,7 +138,6 @@
 #endif
         DEBUGF_END ();
     }
-#endif
     free(fnamew);
 
     return f;
@@ -138,22 +147,18 @@
   fopen by file system codepage
 */
 FILE *
-fsyscp_fopen (const char *filename, const char *mode)
+kpathsea_fsyscp_fopen (kpathsea kpse, const char *filename, const char *mode)
 {
     FILE *f;
     wchar_t *fnamew, modew[4];
     int i;
-#if defined (KPSE_COMPAT_API)
-    kpathsea kpse;
-#endif
+
     assert(filename && mode);
 
-    fnamew = get_wstring_from_fsyscp(filename, fnamew=NULL);
+    fnamew = get_wstring_from_mbstring(kpse->File_system_codepage, filename, fnamew=NULL);
     for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */
     f = _wfopen(fnamew, modew);
-#if defined (KPSE_COMPAT_API)
     if (f != NULL) {
-        kpse = kpse_def;
         if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) {
             DEBUGF_START ();
             fprintf (stderr, "fsyscp_fopen(%s [", filename);
@@ -166,35 +171,19 @@
             DEBUGF_END ();
         }
     }
-#endif
     free(fnamew);
 
     return f;
 }
 
-/*
-  popen by file system codepage
-*/
-static int
-is_include_space(const char *s)
-{
-    char *p;
-    p = strchr(s, ' ');
-    if(p) return 1;
-    p = strchr(s, '\t');
-    if(p) return 1;
-    return 0;
-}
 
 FILE *
-fsyscp_popen (const char *command, const char *mode)
+kpathsea_fsyscp_popen (kpathsea kpse, const char *command, const char *mode)
 {
     FILE *f;
     wchar_t *commandw, modew[4];
     int i;
-#if defined (KPSE_COMPAT_API)
-    kpathsea kpse;
-#endif
+
     assert(command && mode);
 
     if (is_include_space (command)) {
@@ -208,16 +197,15 @@
             *q++ = *p++;
         *q++ = '\"';
         *q = '\0';
-        commandw = get_wstring_from_fsyscp(command2, commandw=NULL);
+        commandw = get_wstring_from_mbstring(kpse->File_system_codepage,command2, commandw=NULL);
         free (command2);
     } else {
-        commandw = get_wstring_from_fsyscp(command, commandw=NULL);
+        commandw = get_wstring_from_mbstring(kpse->File_system_codepage,command, commandw=NULL);
     }
     for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */
     f = _wpopen(commandw, modew);
-#if defined (KPSE_COMPAT_API)
+
     if (f != NULL) {
-        kpse = kpse_def;
         if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) {
             DEBUGF_START ();
             fprintf (stderr, "fsyscp_popen(%s [", command);
@@ -230,7 +218,7 @@
             DEBUGF_END ();
         }
     }
-#endif
+
     free (commandw);
 /* We use always binary mode on Windows */
     if(f) _setmode (fileno (f), _O_BINARY);
@@ -239,7 +227,7 @@
 }
 
 int
-get_command_line_args_utf8 (const_string enc, int *p_ac, char ***p_av)
+kpathsea_get_command_line_args_utf8 (kpathsea kpse, const_string enc, int *p_ac, char ***p_av)
 {
     int argc;
     string *argv;
@@ -258,8 +246,8 @@
       HANDLE hStderr;
       hStderr = GetStdHandle( STD_ERROR_HANDLE );
 #endif /* DEBUG */
-      file_system_codepage = CP_UTF8;
-      is_cp932_system = 0;
+      kpse->File_system_codepage = CP_UTF8;
+      kpse->Is_cp932_system = 0;
       argvw = CommandLineToArgvW(GetCommandLineW(), &argcw);
       argc = argcw;
       argv = xmalloc(sizeof(char *)*(argcw+1));
@@ -275,7 +263,7 @@
       argv[argcw] = NULL;
       *p_ac = argc;
       *p_av = argv;
-      return file_system_codepage;
+      return kpse->File_system_codepage;
     } else {
       return 0;
     }
@@ -285,7 +273,7 @@
   spawnvp by file system codepage
 */
 int
-fsyscp_spawnvp (int mode, const char *command, const char* const *argv)
+kpathsea_fsyscp_spawnvp (kpathsea kpse, int mode, const char *command, const char* const *argv)
 {
     int ret;
     wchar_t *commandw, **argvw, **pw;
@@ -296,11 +284,11 @@
     for (i = 0, p = argv; *p; p++)
       i++;
     argvw = xcalloc (i + 3, sizeof (wchar_t *));
-    commandw = get_wstring_from_fsyscp(command, commandw=NULL);
+    commandw = get_wstring_from_mbstring(kpse->File_system_codepage, command, commandw=NULL);
     p = argv;
     pw = argvw;
     while (*p) {
-      *pw = get_wstring_from_fsyscp(*p, *pw=NULL);
+      *pw = get_wstring_from_mbstring(kpse->File_system_codepage, *p, *pw=NULL);
       p++;
       pw++;
     }
@@ -323,7 +311,7 @@
   system by file system codepage
 */
 int
-fsyscp_system (const char *cmd)
+kpathsea_fsyscp_system (kpathsea kpse, const char *cmd)
 {
     const char *p;
     char  *q;
@@ -353,17 +341,15 @@
       *q++ = '"';
     *q = '\0';
     av[3] = NULL;
-    ret = fsyscp_spawnvp (_P_WAIT, av[0], (const char* const*) av);
+    ret = kpathsea_fsyscp_spawnvp (kpse, _P_WAIT, av[0], (const char* const*) av);
     free (av[0]);
     free (av[1]);
     free (av[2]);
     return ret;
 }
 
-static int getc_len;
-static int getc_buff[4];
-
-int win32_getc(FILE *fp)
+int
+kpathsea_win32_getc(kpathsea kpse, FILE *fp)
 {
     const int fd = fileno(fp);
     HANDLE hStdin;
@@ -371,17 +357,16 @@
     wchar_t wc[3];
     char mbc[5];
     int j;
-    static wchar_t wcbuf = L'\0';
 
-    if (!(fd == fileno(stdin) && _isatty(fd) && file_system_codepage == CP_UTF8))
+    if (!(fd == fileno(stdin) && _isatty(fd) && kpse->File_system_codepage == CP_UTF8))
         return getc(fp);
 
-    if (getc_len == 0)
+    if (kpse->getc_len == 0)
     {
         hStdin = GetStdHandle(STD_INPUT_HANDLE);
-        if (wcbuf) {
-            wc[0] = wcbuf;
-            wcbuf = L'\0';
+        if (kpse->wcbuf) {
+            wc[0] = kpse->wcbuf;
+            kpse->wcbuf = L'\0';
         }
         else if (ReadConsoleW(hStdin, wc, 1, &ret, NULL) == 0)
             return EOF;
@@ -391,7 +376,7 @@
             if (0xdc00<=wc[1] && wc[1]<0xe000) {
                 wc[2]=L'\0';
             } else {
-                wcbuf=wc[1];
+                kpse->wcbuf=wc[1];
                 wc[0]=0xfffd;    /* illegal surrogate pair */
                 wc[1]=L'\0';
             }
@@ -404,21 +389,22 @@
         get_utf8_from_wstring(wc,mbc);
         j=strlen(mbc)-1;
         while(j>=0) {
-            getc_buff[getc_len++]=(int)mbc[j--];
+            kpse->getc_buff[kpse->getc_len++]=(int)mbc[j--];
         }
     }
-    return getc_buff[--getc_len];
+    return kpse->getc_buff[--kpse->getc_len];
 }
 
-int win32_ungetc(int c, FILE *fp)
+int
+kpathsea_win32_ungetc(kpathsea kpse, int c, FILE *fp)
 {
     const int fd = fileno(fp);
 
-    if (!(fd == fileno(stdin) && _isatty(fd) && file_system_codepage == CP_UTF8))
+    if (!(fd == fileno(stdin) && _isatty(fd) && kpse->File_system_codepage == CP_UTF8))
         return ungetc(c, fp);
 
-    assert(getc_len < 4);
-    return getc_buff[getc_len++] = c;
+    assert(kpse->getc_len < 4);
+    return kpse->getc_buff[kpse->getc_len++] = c;
 }
 
 static int __win32_fputs(const char *str, HANDLE hStdout)
@@ -437,13 +423,14 @@
     return ret;
 }
 
-int win32_fputs(const char *str, FILE *fp)
+int
+kpathsea_win32_fputs(kpathsea kpse, const char *str, FILE *fp)
 {
     const int fd = fileno(fp);
     HANDLE hStdout;
 
     if (!((fd == fileno(stdout) || fd == fileno(stderr)) && _isatty(fd)
-        && file_system_codepage == CP_UTF8))
+        && kpse->File_system_codepage == CP_UTF8))
         return fputs(str, fp);
 
     hStdout = (fd == fileno(stdout)) ?
@@ -454,7 +441,8 @@
 
 #define MAX_PROMPT_STR_SIZE 8192
 
-int win32_vfprintf(FILE *fp, const char *format, va_list argp)
+int
+kpathsea_win32_vfprintf(kpathsea kpse, FILE *fp, const char *format, va_list argp)
 {
     const int fd = fileno(fp);
     HANDLE hStdout;
@@ -462,7 +450,7 @@
     int ret;
 
     if (!((fd == fileno(stdout) || fd == fileno(stderr)) && _isatty(fd)
-        && file_system_codepage == CP_UTF8))
+        && kpse->File_system_codepage == CP_UTF8))
         return vfprintf(fp, format, argp);
 
     hStdout = (fd == fileno(stdout)) ?
@@ -475,25 +463,25 @@
     return ret;
 }
 
-int win32_puts(const char *str)
+int
+kpathsea_win32_puts(kpathsea kpse, const char *str)
 {
-    if (win32_fputs(str, stdout)==EOF) {
+    if (kpathsea_win32_fputs(kpse, str, stdout)==EOF) {
         return EOF;
     }
     return puts("");
 }
 
-int win32_putc(int c, FILE *fp)
+int
+kpathsea_win32_putc(kpathsea kpse, int c, FILE *fp)
 {
     const int fd = fileno(fp);
     HANDLE hStdout;
     DWORD ret;
     wchar_t wstr[3];
-    static int len = 0;
-    static char buff[5], *str;
 
     if (!((fd == fileno(stdout) || fd == fileno(stderr)) && _isatty(fd)
-        && file_system_codepage == CP_UTF8))
+        && kpse->File_system_codepage == CP_UTF8))
         return putc(c, fp);
 
     hStdout = (fd == fileno(stdout)) ?
@@ -502,31 +490,158 @@
     c &= 0xff;
 
     if (c < 0x80) {
-        str = buff;
-        len = 1;
+        kpse->st_str = kpse->st_buff;
+        kpse->st_len = 1;
     }
     if (c < 0xc0) { /* ASCII or trailer */
-        *str++ = c;
-        len--;
-        if (len == 0) {
-            *str = '\0';
-            get_wstring_from_utf8(buff, wstr);
+        *(kpse->st_str)++ = c;
+        kpse->st_len--;
+        if (kpse->st_len == 0) {
+            *(kpse->st_str) = '\0';
+            get_wstring_from_utf8(kpse->st_buff, wstr);
             if (WriteConsoleW(hStdout, wstr, wcslen(wstr), &ret, NULL) == 0) {
-                len = 0;
+                kpse->st_len = 0;
                 return EOF;
             }
         }
-        else if (len < 0) return EOF;
+        else if (kpse->st_len < 0) return EOF;
         return c;
     }
-    else if (c < 0xc2) { len = 0; return EOF; }  /* illegal */
-    else if (c < 0xe0) len = 2;
-    else if (c < 0xf0) len = 3;
-    else if (c < 0xf5) len = 4;
-    else { len = 0; return EOF; }
-
-    str = buff;
-    *str++ = c;
-    len--;
+    else if (c < 0xc2) { kpse->st_len = 0; return EOF; }  /* illegal */
+    else if (c < 0xe0) kpse->st_len = 2;
+    else if (c < 0xf0) kpse->st_len = 3;
+    else if (c < 0xf5) kpse->st_len = 4;
+    else { kpse->st_len = 0; return EOF; }
+
+    kpse->st_str = kpse->st_buff;
+    *(kpse->st_str)++ = c;
+    kpse->st_len--;
     return c;
 }
+
+int
+kpathsea_IS_KANJI(kpathsea kpse, char *p)
+{
+  int ret;
+
+  ret = kpse->Is_cp932_system && kpathsea_isknj(kpse, *(p)) &&
+        kpathsea_isknj2(kpse, *(p+1));
+  return ret;
+}
+
+char *
+kpathsea_get_fsyscp_from_wstring(kpathsea kpse, const wchar_t *w,char *mb)
+{
+  return get_mbstring_from_wstring(kpse->File_system_codepage, w, mb);
+}
+
+wchar_t *
+kpathsea_get_wstring_from_fsyscp(kpathsea kpse, const char *mb,wchar_t *w)
+{
+  return get_wstring_from_mbstring(kpse->File_system_codepage, mb, w);
+}
+
+#if defined (KPSE_COMPAT_API)
+
+int
+isknj(int c)
+{
+  return kpathsea_isknj(kpse_def, c);
+}
+
+int
+isknj2(int c)
+{
+  return kpathsea_isknj2(kpse_def, c);
+}
+
+FILE *
+fsyscp_xfopen (const char *filename, const char *mode)
+{
+  return kpathsea_fsyscp_xfopen (kpse_def, filename, mode);
+}
+
+FILE *
+fsyscp_fopen (const char *filename, const char *mode)
+{
+  return kpathsea_fsyscp_fopen (kpse_def, filename, mode);
+}
+
+FILE *
+fsyscp_popen (const char *command, const char *mode)
+{
+  return kpathsea_fsyscp_popen (kpse_def, command, mode);
+}
+
+int
+get_command_line_args_utf8 (const_string enc, int *p_ac, char ***p_av)
+{
+  return kpathsea_get_command_line_args_utf8 (kpse_def, enc, p_ac, p_av);
+}
+
+int
+fsyscp_spawnvp (int mode, const char *command, const char* const *argv)
+{
+  return kpathsea_fsyscp_spawnvp (kpse_def, mode, command, argv);
+}
+
+int
+fsyscp_system (const char *cmd)
+{
+  return kpathsea_fsyscp_system (kpse_def, cmd);
+}
+
+int
+win32_getc(FILE *fp)
+{
+  return kpathsea_win32_getc(kpse_def, fp);
+}
+
+int
+win32_ungetc(int c, FILE *fp)
+{
+  return kpathsea_win32_ungetc(kpse_def, c, fp);
+}
+
+int
+win32_fputs(const char *str, FILE *fp)
+{
+  return kpathsea_win32_fputs(kpse_def, str, fp);
+}
+
+int
+win32_vfprintf(FILE *fp, const char *format, va_list argp)
+{
+  return kpathsea_win32_vfprintf(kpse_def, fp, format, argp);
+}
+
+int
+win32_puts(const char *str)
+{
+  return kpathsea_win32_puts(kpse_def, str);
+}
+
+int
+win32_putc(int c, FILE *fp)
+{
+  return kpathsea_win32_putc(kpse_def, c, fp);
+}
+
+int
+IS_KANJI(char *p)
+{
+  return kpathsea_IS_KANJI(kpse_def, p);
+}
+
+char *
+get_fsyscp_from_wstring(const wchar_t *w,char *mb)
+{
+  return kpathsea_get_fsyscp_from_wstring(kpse_def, w, mb);
+}
+
+wchar_t *
+get_wstring_from_fsyscp(const char *mb,wchar_t *w)
+{
+  return kpathsea_get_wstring_from_fsyscp(kpse_def, mb, w);
+}
+#endif
diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/knj.h kpathsea/knj.h
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/knj.h	Fri Feb 19 11:43:37 2016
+++ kpathsea/knj.h	Tue Jun 28 08:42:55 2016
@@ -23,14 +23,32 @@
 extern "C" {
 #endif
 
-extern KPSEDLL int is_cp932_system;
-extern KPSEDLL int file_system_codepage;
+extern KPSEDLL wchar_t* get_wstring_from_mbstring(int cp, const char *mbstr, wchar_t *wstr);
+extern KPSEDLL char* get_mbstring_from_wstring(int cp, const wchar_t *wstr, char *mbstr);
+extern KPSEDLL int kpathsea_isknj(kpathsea kpse, int c);
+extern KPSEDLL int kpathsea_isknj2(kpathsea kpse, int c);
+extern KPSEDLL FILE* kpathsea_fsyscp_xfopen(kpathsea kpse, const char *filename, const char *mode);
+extern KPSEDLL FILE* kpathsea_fsyscp_fopen(kpathsea kpse, const char *filename, const char *mode);
+extern KPSEDLL FILE* kpathsea_fsyscp_popen(kpathsea kpse, const char *command, const char *mode);
+extern KPSEDLL int kpathsea_fsyscp_spawnvp(kpathsea kpse, int mode, const char *command, const char* const *argv);
+extern KPSEDLL int kpathsea_fsyscp_system(kpathsea kpse, const char *cmd);
+extern KPSEDLL int kpathsea_get_command_line_args_utf8(kpathsea kpse, const char *enc, int *p_ac, char ***p_av);
+extern KPSEDLL int kpathsea_win32_getc(kpathsea kpse, FILE *fp);
+extern KPSEDLL int kpathsea_win32_ungetc(kpathsea kpse, int c, FILE *fp);
+extern KPSEDLL int kpathsea_win32_fputs(kpathsea kpse, const char *str, FILE *fp);
+extern KPSEDLL int kpathsea_win32_puts(kpathsea kpse, const char *str);
+extern KPSEDLL int kpathsea_win32_vfprintf(kpathsea kpse, FILE *fp, const char *format, va_list argp);
+extern KPSEDLL int kpathsea_win32_putc(kpathsea kpse, int c, FILE *fp);
+extern KPSEDLL int kpathsea_IS_KANJI(kpathsea kpse, char *p);
+extern KPSEDLL char *kpathsea_get_fsyscp_from_wstring(kpathsea kpse, const wchar_t *w,char *mb);
+extern KPSEDLL wchar_t *kpathsea_get_wstring_from_fsyscp(kpathsea kpse, const char *mb,wchar_t *w);
+
+#if defined (KPSE_COMPAT_API)
+#define is_cp932_system kpse_def->Is_cp932_system
+#define file_system_codepage kpse_def->File_system_codepage
 
 extern KPSEDLL int isknj(int c);
 extern KPSEDLL int isknj2(int c);
-
-extern KPSEDLL wchar_t* get_wstring_from_mbstring(int cp, const char *mbstr, wchar_t *wstr);
-extern KPSEDLL char* get_mbstring_from_wstring(int cp, const wchar_t *wstr, char *mbstr);
 extern KPSEDLL FILE* fsyscp_xfopen(const char *filename, const char *mode);
 extern KPSEDLL FILE* fsyscp_fopen(const char *filename, const char *mode);
 extern KPSEDLL FILE* fsyscp_popen(const char *command, const char *mode);
@@ -43,21 +61,17 @@
 extern KPSEDLL int win32_puts(const char *str);
 extern KPSEDLL int win32_vfprintf(FILE *fp, const char *format, va_list argp);
 extern KPSEDLL int win32_putc(int c, FILE *fp);
-
+extern KPSEDLL int IS_KANJI(char *p);
+extern KPSEDLL char *get_fsyscp_from_wstring(const wchar_t *w,char *mb);
+extern KPSEDLL wchar_t *get_wstring_from_fsyscp(const char *mb,wchar_t *w);
+#endif
 #ifdef __cplusplus
 }
 #endif
 
-/* True if P points to a 2-Byte Kanji (CP 932, SJIS) code.  */
-#define IS_KANJI(p) is_cp932_system && isknj(*(p)) && isknj2(*(p+1))
-
 /* Get wide string from multibyte string in UTF-8 */
 #define get_wstring_from_utf8(mb,w) get_wstring_from_mbstring(CP_UTF8,mb,w)
 /* Get multibyte string in UTF-8 from wide string */
 #define get_utf8_from_wstring(w,mb) get_mbstring_from_wstring(CP_UTF8,w,mb)
-/* Get wide string from multibyte string in file system codepage */
-#define get_wstring_from_fsyscp(mb,w) get_wstring_from_mbstring(file_system_codepage,mb,w)
-/* Get multibyte string in file system codepage from wide string */
-#define get_fsyscp_from_wstring(w,mb) get_mbstring_from_wstring(file_system_codepage,w,mb)
 
 #endif /* not KPATHSEA_KNJ_H */

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/kpsewhich.c kpathsea/kpsewhich.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/kpsewhich.c	Thu Apr 07 00:02:53 2016
+++ kpathsea/kpsewhich.c	Tue Jun 28 09:14:07 2016
@@ -30,13 +30,6 @@
 #include <kpathsea/variable.h>
 #include <kpathsea/version.h>
 
-#ifdef WIN32
-#undef fputs
-#undef puts
-#define fputs win32_fputs
-#define puts  win32_puts
-#endif
-
 /* For variable and path expansion.  (-expand-var, -expand-path,
    -show-path) */
 string var_to_expand = NULL;
@@ -272,25 +265,24 @@
    We don't reallocate the actual strings, just the list elements.
    Perhaps later we will implement wildcards or // or something.  */
 
+#if defined(WIN32)
 static string *
-subdir_match (str_list_type subdirs,  string *matches)
+kpathsea_subdir_match (kpathsea kpse, str_list_type subdirs,  string *matches)
 {
   string *ret = XTALLOC1 (string);
   unsigned len = 1;
   unsigned e;
   unsigned m;
-#if defined(WIN32)
   string p;
 
   for (e = 0; e < STR_LIST_LENGTH (subdirs); e++) {
     for (p = STR_LIST_ELT (subdirs, e); *p; p++) {
       if (*p == '\\')
         *p = '/';
-      else if (IS_KANJI(p))
+      else if (kpathsea_IS_KANJI(kpse, p))
         p++;
     }
   }
-#endif
 
   for (m = 0; matches[m]; m++) {
     unsigned loc;
@@ -322,6 +314,45 @@
   return ret;
 }
 
+#else /* WIN32 */
+static string *
+subdir_match (str_list_type subdirs,  string *matches)
+{
+  string *ret = XTALLOC1 (string);
+  unsigned len = 1;
+  unsigned e;
+  unsigned m;
+
+  for (m = 0; matches[m]; m++) {
+    unsigned loc;
+    string s = xstrdup (matches[m]);
+    for (loc = strlen (s); loc > 0 && !IS_DIR_SEP_CH (s[loc-1]); loc--)
+      ;
+    while (loc > 0 && IS_DIR_SEP_CH (s[loc-1])) {
+      loc--;
+    }
+    s[loc] = 0;  /* wipe out basename */
+
+    for (e = 0; e < STR_LIST_LENGTH (subdirs); e++) {
+      string subdir = STR_LIST_ELT (subdirs, e);
+      unsigned subdir_len = strlen (subdir);
+      while (subdir_len > 0 && IS_DIR_SEP_CH (subdir[subdir_len-1])) {
+        subdir_len--;
+        subdir[subdir_len] = 0; /* remove trailing slashes from subdir spec */
+      }
+      if (FILESTRCASEEQ (subdir, s + loc - subdir_len)) {
+        /* matched, save this one.  */
+        XRETALLOC (ret, len + 1, string);
+        ret[len-1] = matches[m];
+        len++;
+      }
+    }
+    free (s);
+  }
+  ret[len-1] = NULL;
+  return ret;
+}
+#endif /* WIN32 */
 
 
 /* Look up a single filename NAME.  Return 0 if success, 1 if failure.  */
@@ -395,7 +426,11 @@
 
   /* Filter by subdirectories, if specified.  */
   if (STR_LIST_LENGTH (subdir_paths) > 0) {
+#if defined(WIN32)
+    string *new_list = kpathsea_subdir_match (kpse, subdir_paths, ret_list);
+#else
     string *new_list = subdir_match (subdir_paths, ret_list);
+#endif /* WIN32 */
     free (ret_list);
     ret_list = new_list;
   }
@@ -403,7 +438,11 @@
   /* Print output.  */
   if (ret_list) {
     for (i = 0; ret_list[i]; i++)
+#ifdef WIN32
+      kpathsea_win32_puts (kpse, ret_list[i]);
+#else
       puts (ret_list[i]);
+#endif
     /* Save whether we found anything */
     ret = ret_list[0];
     free (ret_list);
@@ -452,10 +491,19 @@
 help_message (kpathsea kpse, string *argv)
 {
   printf ("Usage: %s [OPTION]... [FILENAME]...\n", argv[0]);
+#ifdef WIN32
+  kpathsea_win32_fputs (kpse, USAGE, stdout);
+#else
   fputs (USAGE, stdout);
+#endif
   putchar ('\n');
+#ifdef WIN32
+  kpathsea_win32_fputs (kpse, kpathsea_bug_address, stdout);
+  kpathsea_win32_fputs (kpse, "Kpathsea home page: http://tug.org/kpathsea/\n", stdout);
+#else
   fputs (kpathsea_bug_address, stdout);
   fputs ("Kpathsea home page: http://tug.org/kpathsea/\n", stdout);
+#endif
   exit (0);
 }
 
@@ -467,8 +515,13 @@
   /* Have to set this for init_format to work.  */
   kpathsea_set_program_name (kpse, argv[0], progname);
 
+#ifdef WIN32
+  kpathsea_win32_puts (kpse, kpathsea_version_string); 
+  kpathsea_win32_puts (kpse, "\nRecognized Kpathsea format names and their (abbreviations) and suffixes:");
+#else
   puts (kpathsea_version_string); 
   puts ("\nRecognized Kpathsea format names and their (abbreviations) and suffixes:");
+#endif
   for (f = 0; f < kpse_last_format; f++) {
     const_string *ext;
 
@@ -493,7 +546,11 @@
     putchar (':');
     for (ext = kpse->format_info[f].suffix; ext && *ext; ext++) {
       putchar (' ');
+#ifdef WIN32
+      kpathsea_win32_fputs (kpse, *ext, stdout);
+#else
       fputs (*ext, stdout);
+#endif
     }
 
     if (kpse->format_info[f].alt_suffix) {
@@ -502,7 +559,11 @@
     }
     for (ext = kpse->format_info[f].alt_suffix; ext && *ext; ext++) {
       putchar (' ');
+#ifdef WIN32
+      kpathsea_win32_fputs (kpse, *ext, stdout);
+#else
       fputs (*ext, stdout);
+#endif
     }
 
     printf ("  [variables: %s]\n", envvar_list);
@@ -511,8 +572,13 @@
             kpse->format_info[f].path_source, kpse->format_info[f].raw_path);
   }
 
+#ifdef WIN32
+  kpathsea_win32_fputs (kpse, "\nTo see paths after expansion, use --show-path=FMT.\n\n", stdout);
+  kpathsea_win32_fputs (kpse, kpathsea_bug_address, stdout);
+#else
   fputs ("\nTo see paths after expansion, use --show-path=FMT.\n\n", stdout);
   fputs (kpathsea_bug_address, stdout);
+#endif
   exit (0);
 }
 
@@ -630,11 +696,19 @@
       var_to_value = optarg;
 
     } else if (ARGUMENT_IS ("version")) {
+#ifdef WIN32
+      kpathsea_win32_puts (kpse, kpathsea_version_string);
+      kpathsea_win32_puts (kpse, "Copyright 2016 Karl Berry & Olaf Weber.\n\
+License LGPLv2.1+: GNU Lesser GPL version 2.1 or later <http://gnu.org/licenses/lgpl.html>\n\
+This is free software: you are free to change and redistribute it.\n\
+There is NO WARRANTY, to the extent permitted by law.\n");
+#else
       puts (kpathsea_version_string);
       puts ("Copyright 2016 Karl Berry & Olaf Weber.\n\
 License LGPLv2.1+: GNU Lesser GPL version 2.1 or later <http://gnu.org/licenses/lgpl.html>\n\
 This is free software: you are free to change and redistribute it.\n\
 There is NO WARRANTY, to the extent permitted by law.\n");
+#endif
       exit (0);
     }
 
@@ -644,7 +718,11 @@
   if (user_path && user_format_string) {
     fprintf (stderr, "-path (%s) and -format (%s) are mutually exclusive.\n",
              user_path, user_format_string);
+#ifdef WIN32
+    kpathsea_win32_fputs (kpse, "Try `kpsewhich --help' for more information.\n", stderr);
+#else
     fputs ("Try `kpsewhich --help' for more information.\n", stderr);
+#endif
     exit (1);
   }
 
@@ -652,8 +730,13 @@
       && !var_to_expand && !braces_to_expand && !path_to_expand
       && !path_to_show && !var_to_value
       && !safe_in_name && !safe_out_name) {
+#ifdef WIN32
+    kpathsea_win32_fputs (kpse, "Missing argument. Try `kpsewhich --help' for more information.\n",
+           stderr);
+#else
     fputs ("Missing argument. Try `kpsewhich --help' for more information.\n",
            stderr);
+#endif
     exit (1);
   }
 }
@@ -720,7 +803,7 @@
       enc = "utf-8";
     else
       enc = kpathsea_var_value (kpse, "command_line_encoding");
-    if (get_command_line_args_utf8(enc, &ac, &av)) {
+    if (kpathsea_get_command_line_args_utf8(kpse, enc, &ac, &av)) {
       optind = 0;
       read_command_line (kpse, ac, av);
       argv = av;
@@ -735,22 +818,38 @@
 
   /* Variable expansion.  */
   if (var_to_expand)
+#ifdef WIN32
+    kpathsea_win32_puts (kpse, kpathsea_var_expand (kpse, var_to_expand));
+#else
     puts (kpathsea_var_expand (kpse, var_to_expand));
+#endif
 
   /* Brace expansion. */
   if (braces_to_expand)
+#ifdef WIN32
+    kpathsea_win32_puts (kpse, kpathsea_brace_expand (kpse, braces_to_expand));
+#else
     puts (kpathsea_brace_expand (kpse, braces_to_expand));
+#endif
 
   /* Path expansion. */
   if (path_to_expand)
+#ifdef WIN32
+    kpathsea_win32_puts (kpse, kpathsea_path_expand (kpse, path_to_expand));
+#else
     puts (kpathsea_path_expand (kpse, path_to_expand));
+#endif
 
   /* Show a search path. */
   if (path_to_show) {
     if (user_format != kpse_last_format) {
       if (!kpse->format_info[user_format].type) /* needed if arg was numeric */
         kpathsea_init_format (kpse, user_format);
+#ifdef WIN32
+      kpathsea_win32_puts (kpse, kpse->format_info[user_format].path);
+#else
       puts (kpse->format_info[user_format].path);
+#endif
     } else {
       WARNING ("kpsewhich: Cannot show path for unknown file type");
     }
@@ -763,7 +862,11 @@
       unfound++;
       value = "";
     }
+#ifdef WIN32
+    kpathsea_win32_puts (kpse, value);
+#else
     puts (value);
+#endif
   }
 
   if (safe_in_name) {
diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/make-suffix.c kpathsea/make-suffix.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/make-suffix.c	Fri Feb 19 11:43:37 2016
+++ kpathsea/make-suffix.c	Tue Jun 28 08:27:27 2016
@@ -38,7 +38,7 @@
         dot_pos = NULL;
         break;
       }
-#if defined(WIN32)
+#if defined(WIN32) && defined(KPSE_COMPAT_API)
       else if (IS_KANJI(p))
         p++;
 #endif
@@ -59,8 +59,10 @@
   for (q = new_s; *q; q++) {
     if (*q == '\\')
       *q = '/';
+#if defined(KPSE_COMPAT_API)
     else if (IS_KANJI(q))
       q++;
+#endif
   }
 #endif
 
diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/path-elt.c kpathsea/path-elt.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/path-elt.c	Fri Feb 19 11:43:37 2016
+++ kpathsea/path-elt.c	Tue Jun 28 07:36:00 2016
@@ -56,7 +56,7 @@
     if (*p == '{') ++brace_level;
     else if (*p == '}') --brace_level;
 #if defined(WIN32)
-    else if (IS_KANJI(p))
+    else if (kpathsea_IS_KANJI(kpse, p))
         p++;
 #endif
     p++;

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/progname.c kpathsea/progname.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/progname.c	Fri Feb 19 11:43:37 2016
+++ kpathsea/progname.c	Tue Jun 28 08:03:00 2016
@@ -501,14 +501,14 @@
   }
 
 #if defined(WIN32)
-  if (!file_system_codepage)
-    file_system_codepage = AreFileApisANSI() ? GetACP() : GetOEMCP();
-  cp = file_system_codepage;
+  if (!kpse->File_system_codepage)
+    kpse->File_system_codepage = AreFileApisANSI() ? GetACP() : GetOEMCP();
+  cp = kpse->File_system_codepage;
   if (cp == 932 || cp == 936 || cp == 950) {
-    is_cp932_system = cp;
+    kpse->Is_cp932_system = cp;
   }
   else
-    is_cp932_system = 0;
+    kpse->Is_cp932_system = 0;
 
 #if defined(__MINGW32__)
   /* Set various info about user. Among many things,
@@ -585,7 +585,7 @@
     for (fp = path; fp && *fp; fp++)
         if (IS_DIR_SEP(*fp)) *fp = DIR_SEP;
 #else /* !__MINGW32__ */
-    if (getlongpath(path, short_path, PATH_MAX) == 0)
+    if (kpathsea_getlongpath(kpse, path, short_path, PATH_MAX) == 0)
         FATAL1("Can't get long name for %s.\n", short_path);
     if ((hnd = FindFirstFile(short_path, &ffd)) == INVALID_HANDLE_VALUE)
         FATAL1("The following path points to an invalid file : %s\n", path);

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/readable.c kpathsea/readable.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/readable.c	Fri Feb 19 11:43:37 2016
+++ kpathsea/readable.c	Tue Jun 28 07:37:48 2016
@@ -38,10 +38,10 @@
 #elif defined (WIN32)
 /* st must be an unsigned int under Windows */
 static boolean
-READABLE(const_string fn, unsigned int st)
+READABLE(kpathsea kpse, const_string fn, unsigned int st)
 {
   wchar_t *fnw;
-  fnw = get_wstring_from_fsyscp(fn, fnw=NULL);
+  fnw = get_wstring_from_mbstring(kpse->File_system_codepage, fn, fnw=NULL);
   if ((st = GetFileAttributesW(fnw)) != 0xFFFFFFFF) {
       /* succeeded */
       errno = 0;
@@ -82,7 +82,11 @@
 #endif
 
   kpathsea_normalize_path (kpse, name);
+#ifdef WIN32
+  if (READABLE (kpse, name, st)) {
+#else
   if (READABLE (name, st)) {
+#endif
       return name;
 #ifdef ENAMETOOLONG
   } else if (errno == ENAMETOOLONG) {
@@ -95,7 +99,7 @@
           if (c_len <= NAME_MAX)
               t = s;
 #if defined(WIN32)
-          if (IS_KANJI (s)) {
+          if (kpathsea_IS_KANJI (kpse, s)) {
               s++;
               c_len += 2;
               continue;
@@ -118,7 +122,11 @@
 
       /* Perhaps some other error will occur with the truncated name, so
          let's call access again.  */
+#ifdef WIN32
+      if (READABLE (kpse, name, st)) /* Success.  */
+#else
       if (READABLE (name, st)) /* Success.  */
+#endif
           return name;
 #endif /* ENAMETOOLONG */
   } else { /* Some other error.  */

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/tilde.c kpathsea/tilde.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/tilde.c	Fri Feb 19 11:43:38 2016
+++ kpathsea/tilde.c	Tue Jun 28 07:40:26 2016
@@ -88,7 +88,7 @@
       c = 2;
       while (!IS_DIR_SEP (name[c]) && name[c] != 0) {  /* find user name */
 #if defined(WIN32)
-        if (IS_KANJI(name+c))
+        if (kpathsea_IS_KANJI(kpse, name+c))
           c++;
 #endif
         c++;
@@ -130,7 +130,7 @@
       for (q = home; *q; q++) {
         if (IS_DIR_SEP (*q) && q[1] == 0)
           c++;
-        else if (IS_KANJI(q))
+        else if (kpathsea_IS_KANJI(kpse, q))
           q++;
       }
 #else
@@ -146,7 +146,7 @@
   for (p = expansion; *p; p++) {
     if (*p == '\\')
       *p = '/'; 
-    else if (IS_KANJI(p))
+    else if (kpathsea_IS_KANJI(kpse, p))
       p++;
   }
 #endif

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/types.h kpathsea/types.h
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/types.h	Fri Feb 19 11:43:38 2016
+++ kpathsea/types.h	Tue Jun 28 07:11:29 2016
@@ -292,6 +292,16 @@
     struct passwd the_passwd;
     int __system_allow_multiple_cmds;
 #endif /* WIN32 && !__MINGW32__ */
+#if defined(WIN32)
+    int Is_cp932_system;
+    int File_system_codepage;
+    int getc_len;
+    int getc_buff[4];
+    wchar_t wcbuf;
+    int st_len;
+    char st_buff[5];
+    char *st_str;
+#endif
 } kpathsea_instance;
 
 /* these come from kpathsea.c */

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/variable.c kpathsea/variable.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/variable.c	Fri Feb 19 11:43:38 2016
+++ kpathsea/variable.c	Tue Jun 28 07:41:19 2016
@@ -222,7 +222,7 @@
 
         while (*var_end && !IS_VAR_END_DELIMITER (*var_end)) {
 #if defined(WIN32)
-          if (IS_KANJI(var_end))
+          if (kpathsea_IS_KANJI(kpse, var_end))
             var_end++;
 #endif
           var_end++;

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/win32lib.c kpathsea/win32lib.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/win32lib.c	Thu Apr 07 00:02:53 2016
+++ kpathsea/win32lib.c	Tue Jun 28 09:12:27 2016
@@ -172,7 +172,7 @@
                1 if succeeded
 */
 
-int getlongpath(char *buff, char *input, int len)
+int kpathsea_getlongpath(kpathsea kpse, char *buff, char *input, int len)
 {
    HANDLE hnd;
    WIN32_FIND_DATA ffd;
@@ -202,7 +202,7 @@
       p += 2;
       r += 2;
       while(*p != '\\' && *p) {
-         if (IS_KANJI(p)) {
+         if (kpathsea_IS_KANJI(kpse, p)) {
             cnt++;
             if(cnt > len) return 0;
             *r++ = *p++;
@@ -216,7 +216,7 @@
       *r++ = '/';
       if(*p) p++;
       while(*p != '\\' && *p) {
-         if (IS_KANJI(p)) {
+         if (kpathsea_IS_KANJI(kpse, p)) {
             cnt++;
             if(cnt > len) return 0;
             *r++ = *p++;
@@ -244,7 +244,7 @@
    }
 
    for( ; *p; p++) {
-      if(IS_KANJI(p)) {
+      if(kpathsea_IS_KANJI(kpse, p)) {
          p++;
          continue;
       }
@@ -292,7 +292,7 @@
 /* Sync'ed with Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
 /* Adapted to fpTeX 0.4 by Fabrice Popineau <Fabrice.Popineau@supelec.fr> */
 
-char * get_home_directory()
+char * kpathsea_get_home_directory(kpathsea kpse)
 {
   char *p;
   char *home = getenv("HOME");
@@ -301,7 +301,7 @@
   if(home) {
     home = xstrdup(home);
     for(p = home; *p; p++) {
-      if(IS_KANJI(p)) {
+      if(kpathsea_IS_KANJI(kpse, p)) {
         p++;
         continue;
       }
@@ -360,7 +360,7 @@
 
    /* Ensure HOME and SHELL are defined. */
 
-   home = get_home_directory();
+   home = kpathsea_get_home_directory(kpse);
    if (home) {
       putenv(concat("HOME=", home));
    }
@@ -381,7 +381,7 @@
    }
 
    /* Set dir and shell from environment variables. */
-   strcpy (kpse->the_passwd.pw_dir, get_home_directory());
+   strcpy (kpse->the_passwd.pw_dir, kpathsea_get_home_directory(kpse));
    strcpy (kpse->the_passwd.pw_shell, getenv ("SHELL"));
 }
 
@@ -424,6 +424,16 @@
 }
 
 #if defined (KPSE_COMPAT_API)
+int getlongpath(char *buff, char *input, int len)
+{
+  return kpathsea_getlongpath(kpse_def, buff, input, len);
+}
+
+char * get_home_directory(void)
+{
+  return kpathsea_get_home_directory(kpse_def);
+}
+
 int 
 getuid (void) 
 { 
diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/win32lib.h kpathsea/win32lib.h
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/win32lib.h	Thu Apr 07 00:02:53 2016
+++ kpathsea/win32lib.h	Tue Jun 28 08:01:00 2016
@@ -275,8 +275,13 @@
 extern KPSEDLL int system(const char * cmd);
 
 extern KPSEDLL void texlive_gs_init(void);
+extern KPSEDLL int kpathsea_getlongpath (kpathsea kpse, char *output, char *input, int len);
+extern KPSEDLL char *kpathsea_get_home_directory (kpathsea kpse);
+
+#if defined (KPSE_COMPAT_API)
 extern KPSEDLL int getlongpath (char *output, char *input, int len);
 extern KPSEDLL char *get_home_directory (void);
+#endif
 
 #define off_t __int64
 #define xfseeko xfseek64

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/xbasename.c kpathsea/xbasename.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/xbasename.c	Fri Feb 19 11:43:38 2016
+++ kpathsea/xbasename.c	Tue Jun 28 08:23:13 2016
@@ -38,13 +38,13 @@
         unsigned limit;
 
         for (limit = 2; name[limit] && !IS_DIR_SEP (name[limit]); limit++)
-#if defined(WIN32)
+#if defined(WIN32) && defined (KPSE_COMPAT_API)
             if (IS_KANJI(name+limit)) limit++
 #endif
             ;
         if (name[limit++] && name[limit] && !IS_DIR_SEP (name[limit])) {
             for (; name[limit] && !IS_DIR_SEP (name[limit]); limit++)
-#if defined(WIN32)
+#if defined(WIN32) && defined (KPSE_COMPAT_API)
                 if (IS_KANJI(name+limit)) limit++
 #endif
                 ;
@@ -57,7 +57,7 @@
     for (p = base; *p; p++) {
         if (IS_DIR_SEP(*p))
             base = p + 1;
-#if defined(WIN32)
+#if defined(WIN32) && defined (KPSE_COMPAT_API)
         else if (IS_KANJI(p))
             p++;
 #endif

diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/xdirname.c kpathsea/xdirname.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/xdirname.c	Fri Feb 19 11:43:38 2016
+++ kpathsea/xdirname.c	Tue Jun 28 08:21:03 2016
@@ -43,13 +43,13 @@
         limit = 2;
     } else if (IS_UNC_NAME(name)) {
         for (limit = 2; name[limit] && !IS_DIR_SEP (name[limit]); limit++)
-#if defined(WIN32)
+#if defined(WIN32) && defined(KPSE_COMPAT_API)
             if (IS_KANJI(name+limit)) limit++
 #endif
             ;
         if (name[limit++] && name[limit] && !IS_DIR_SEP (name[limit])) {
             for (; name[limit] && !IS_DIR_SEP (name[limit]); limit++)
-#if defined(WIN32)
+#if defined(WIN32) && defined(KPSE_COMPAT_API)
                 if (IS_KANJI(name+limit)) limit++
 #endif
                 ;
@@ -68,7 +68,10 @@
             for (i++; IS_DIR_SEP (name[i]); i++)
                 ;
             loc = i + 1;
-        } else if (IS_KANJI(name+i)) i++;
+        }
+#if defined (KPSE_COMPAT_API)
+        else if (IS_KANJI(name+i)) i++;
+#endif
     }
 #else
     for (loc = strlen (name); loc > limit && !IS_DIR_SEP (name[loc-1]); loc--)
@@ -107,8 +110,10 @@
     for (p = ret; *p; p++) {
         if (*p == '\\')
             *p = '/';
+#if defined (KPSE_COMPAT_API)
         else if (IS_KANJI(p))
             p++;
+#endif
     }
 #endif
 
diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/xdirtest.c kpathsea/xdirtest.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/xdirtest.c	Fri Feb 19 11:43:38 2016
+++ kpathsea/xdirtest.c	Tue Jun 28 07:25:25 2016
@@ -100,10 +100,10 @@
 }
 
 static void
-do_kanji (void) {
+do_kanji (kpathsea kpse) {
     const char **p;
 
-    printf("\nAssuming CP %s 932\n", is_cp932_system ? "is" : "is not");
+    printf("\nAssuming CP %s 932\n", kpse->Is_cp932_system ? "is" : "is not");
 
     for (p = ktab; *p; p++) {
         char *q = to_kanji(*p);
@@ -117,18 +117,18 @@
 }
 
 static void
-kanji_test(void) {
-    int save_cp932 = is_cp932_system;
+kanji_test(kpathsea kpse) {
+    int save_cp932 = kpse->Is_cp932_system;
 
     printf("\nTesting 2-Byte Kanji (CP 932, SJIS) codes with 'K' representing 0x81\n");
 
-    is_cp932_system = 932;	/* pretend CP is 932 */
-    do_kanji();
+    kpse->Is_cp932_system = 932;	/* pretend CP is 932 */
+    do_kanji(kpse);
 
-    is_cp932_system = 0;	/* pretend CP is not 932 */
-    do_kanji();
+    kpse->Is_cp932_system = 0;	/* pretend CP is not 932 */
+    do_kanji(kpse);
 
-    is_cp932_system = save_cp932;
+    kpse->Is_cp932_system = save_cp932;
 }
 #endif
 
@@ -150,7 +150,7 @@
     }
 
 #if defined (WIN32)
-    kanji_test();
+    kanji_test(kpse);
 #endif
 
     return 0;
diff -u c:/usr/SVN/tlbuild/source/texk/kpathsea/xgetcwd.c kpathsea/xgetcwd.c
--- c:/usr/SVN/tlbuild/source/texk/kpathsea/xgetcwd.c	Fri Feb 19 11:43:38 2016
+++ kpathsea/xgetcwd.c	Tue Jun 28 08:13:50 2016
@@ -61,8 +61,10 @@
     for (pp = path; *pp; pp++) {
         if (*pp == '\\')
             *pp = '/';
+#if defined (KPSE_COMPAT_API)
         else if (IS_KANJI(pp))
             pp++;
+#endif
     }
 #endif
 
