+/* Current codepage for encoding file names. */
+static int file_name_codepage;
+
+/* Return the maximum length in bytes of a multibyte character
+ sequence encoded in the current ANSI codepage. This is required to
+ correctly walk the encoded file names one character at a time. */
+static int
+max_filename_mbslen (void)
+{
+ /* A simple cache to avoid calling GetCPInfo every time we need to
+ normalize a file name. The file-name encoding is not supposed to
+ be changed too frequently, if ever. */
+ static Lisp_Object last_file_name_encoding;
+ static int last_max_mbslen;
+ Lisp_Object current_encoding;
+
+ current_encoding = Vfile_name_coding_system;
+ if (NILP (current_encoding))
+ current_encoding = Vdefault_file_name_coding_system;
+
+ if (!EQ (last_file_name_encoding, current_encoding))
+ {
+ CPINFO cp_info;
+
+ last_file_name_encoding = current_encoding;
+ /* Default to the current ANSI codepage. */
+ file_name_codepage = w32_ansi_code_page;
+ if (!NILP (current_encoding))
+ {
+ char *cpname = SDATA (SYMBOL_NAME (current_encoding));
+ char *cp = NULL, *end;
+ int cpnum;
+
+ if (strncmp (cpname, "cp", 2) == 0)
+ cp = cpname + 2;
+ else if (strncmp (cpname, "windows-", 8) == 0)
+ cp = cpname + 8;
+
+ if (cp)
+ {
+ end = cp;
+ cpnum = strtol (cp, &end, 10);
+ if (cpnum && *end == '\0' && end - cp >= 2)
+ file_name_codepage = cpnum;
+ }
+ }
+
+ if (!file_name_codepage)
+ file_name_codepage = CP_ACP; /* CP_ACP = 0, but let's not assume that */
+
+ if (!GetCPInfo (file_name_codepage, &cp_info))
+ {
+ file_name_codepage = CP_ACP;
+ if (!GetCPInfo (file_name_codepage, &cp_info))
+ emacs_abort ();
+ }
+ last_max_mbslen = cp_info.MaxCharSize;
+ }
+
+ return last_max_mbslen;
+}