Merge "windows: handle unicode path" into emu-master-dev
diff --git a/hostapd/config_file.c b/hostapd/config_file.c
index 42f3b40..006c22e 100644
--- a/hostapd/config_file.c
+++ b/hostapd/config_file.c
@@ -41,7 +41,7 @@
 	int line = 0, vlan_id;
 	struct hostapd_vlan *vlan;
 
-	f = fopen(fname, "r");
+	f = os_fopen(fname, "r");
 	if (!f) {
 		wpa_printf(MSG_ERROR, "VLAN file '%s' not readable.", fname);
 		return -1;
@@ -179,7 +179,7 @@
 	u8 addr[ETH_ALEN];
 	int vlan_id;
 
-	f = fopen(fname, "r");
+	f = os_fopen(fname, "r");
 	if (!f) {
 		wpa_printf(MSG_ERROR, "MAC list file '%s' not found.", fname);
 		return -1;
@@ -320,7 +320,7 @@
 #endif /* CONFIG_SQLITE */
 	}
 
-	f = fopen(fname, "r");
+	f = os_fopen(fname, "r");
 	if (!f) {
 		wpa_printf(MSG_ERROR, "EAP user file '%s' not found.", fname);
 		return -1;
@@ -4374,7 +4374,7 @@
 	int errors = 0;
 	size_t i;
 
-	f = fopen(fname, "r");
+	f = os_fopen(fname, "r");
 	if (f == NULL) {
 		wpa_printf(MSG_ERROR, "Could not open configuration file '%s' "
 			   "for reading.", fname);
diff --git a/src/utils/os.h b/src/utils/os.h
index 2ec5c13..a575dea 100644
--- a/src/utils/os.h
+++ b/src/utils/os.h
@@ -239,6 +239,12 @@
  */
 char * os_readfile(const char *name, size_t *len);
 
+#ifdef _WIN32
+FILE *os_fopen(const char *utf8filename, const char *mode);
+#else
+#define os_fopen fopen
+#endif
+
 /**
  * os_file_exists - Check whether the specified file exists
  * @fname: Path and name of the file
diff --git a/src/utils/os_win32.c b/src/utils/os_win32.c
index b66bfe8..3ba93f9 100644
--- a/src/utils/os_win32.c
+++ b/src/utils/os_win32.c
@@ -216,6 +216,24 @@
 	return buf;
 }
 
+static wchar_t *s_convert_to_wchar(const char *utf8str) {
+    if (!utf8str) {
+        return NULL;
+    }
+    int len = MultiByteToWideChar(CP_UTF8, 0, utf8str, -1, NULL, 0);
+    wchar_t *wstr = os_zalloc(len * sizeof(wchar_t));
+    MultiByteToWideChar(CP_UTF8, 0, utf8str, -1, wstr, len);
+    return wstr;
+}
+
+FILE *os_fopen(const char *utf8filename, const char *mode) {
+    wchar_t *wmode = s_convert_to_wchar(mode);
+    wchar_t *wfilename = s_convert_to_wchar(utf8filename);
+    FILE *file = _wfopen(wfilename, wmode);
+    free(wfilename);
+    free(wmode);
+    return file;
+}
 
 int os_fdatasync(FILE *stream)
 {