Permalink
Browse files

Issues with non-ASCII chars in path or filename #604

Windows fopen and fstream do not support UTF-8 paths.

gimx.exe issues:
* failure to write log file
* failure to read macros

gimx-launcher issues:
* failure to save and restore choices
* failure to read gimx exit status
* failure to download configurations
* failure to download updates (if temp dir was modified)
  • Loading branch information...
matlo committed Jan 8, 2019
1 parent e27f79a commit bb77831caa39694890695d2606a878f1c6b684f2
@@ -311,7 +311,7 @@ int args_read(int argc, char *argv[], s_gimx_params* params)
{
char file_path[PATH_MAX];
snprintf(file_path, sizeof(file_path), "%s%s%s%s", params->homedir, GIMX_DIR, LOG_DIR, params->logfilename);
params->logfile = fopen(file_path, "w");
params->logfile = fopen2(file_path, "w");
if(params->logfile != NULL)
{
dup2(fileno(params->logfile), fileno(stdout));
@@ -334,7 +334,7 @@ static int read_link_keys(uint16_t index, uint16_t nb_keys, bdaddr_t bdaddrs[nb_

char line[LINE_MAX];

FILE* file = fopen(file_path, "r");
FILE* file = fopen2(file_path, "r");
int i = 0;

if (!file)
@@ -94,6 +94,40 @@ void terminate(int sig __attribute__((unused)))
set_done();
}

#ifndef WIN32
FILE *fopen2(const char *path, const char *mode) {
return fopen(path, mode);
}
#else
static wchar_t * utf8_to_utf16le(const char * inbuf)
{
wchar_t * outbuf = NULL;
int outsize = MultiByteToWideChar(CP_UTF8, 0, inbuf, -1, NULL, 0);
if (outsize != 0) {
outbuf = (wchar_t*) malloc(outsize * sizeof(*outbuf));
if (outbuf != NULL) {
int res = MultiByteToWideChar(CP_UTF8, 0, inbuf, -1, outbuf, outsize);
if (res == 0) {
free(outbuf);
outbuf = NULL;
}
}
}

return outbuf;
}

FILE *fopen2(const char *path, const char *mode) {
wchar_t * wpath = utf8_to_utf16le(path);
wchar_t * wmode = utf8_to_utf16le(mode);
FILE* file = _wfopen(wpath, wmode);
perror("");
free(wmode);
free(wpath);
return file;
}
#endif

int ignore_event(GE_Event* event __attribute__((unused)))
{
return 0;
@@ -180,7 +214,7 @@ void show_config()

snprintf(file_path, sizeof(file_path), "%s%s%s%s", gimx_params.homedir, GIMX_DIR, CONFIG_DIR, gimx_params.config_file);

FILE * fp = fopen(file_path, "r");
FILE * fp = fopen2(file_path, "r");
if (fp == NULL)
{
gwarn("failed to dump %s\n", file_path);
@@ -542,7 +576,7 @@ int main(int argc, char *argv[])
#endif
if (file != NULL && file[0] != '\0')
{
FILE * fp = fopen(file, "w");
FILE * fp = fopen2(file, "w");
if (fp != NULL)
{
fprintf(fp, "%d\n", status);
@@ -236,4 +236,6 @@ int ignore_event(GE_Event*);
#define REMOVE_FUNCTION gpoll_remove_fd
#endif

FILE *fopen2(const char *path, const char *mode);

#endif /* GIMX_H_ */
@@ -974,7 +974,7 @@ static void read_configs_txt(const char* dir_path)
int ret;

snprintf(file_path, sizeof(file_path), "%s%s", dir_path, MACRO_CONFIGS_FILE);
fp = fopen(file_path, "r");
fp = fopen2(file_path, "r");
if (fp)
{
if (gimx_params.logfile != NULL) {
@@ -1144,7 +1144,7 @@ static void read_macros() {
//else: no configs.txt => read all macros.

snprintf(file_path, sizeof(file_path), "%s%s", dir_path, filenames[i]);
fp = fopen(file_path, "r");
fp = fopen2(file_path, "r");
if (!fp) {
gwarn("failed to open %s\n", file_path);
} else {
Oops, something went wrong.

0 comments on commit bb77831

Please sign in to comment.