Skip to content

Commit 7dc9466

Browse files
committed
Make UTF-8 get_home_path for winedit/write_history
1 parent 9cdbb1b commit 7dc9466

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/port/path.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,31 @@ get_man_path(const char *my_exec_path, char *ret_path)
762762
make_relative_path(ret_path, MANDIR, PGBINDIR, my_exec_path);
763763
}
764764

765+
#ifdef WIN32
766+
/*
767+
* make_wc_name
768+
*/
769+
770+
const wchar_t* make_wc_name(const char* name) {
771+
int wlen;
772+
size_t size = strlen(s) + 1;
773+
size_t wsize = size * sizeof(wchar_t);
774+
wchar_t *p = (wchar_t*)malloc(wsize);
775+
wlen = MultiByteToWideChar(CP_ACP,0,(LPCCH)name,(int)size,(LPWSTR)p,(int)wsize);
776+
return p;
777+
}
778+
779+
/*
780+
* make_utf8_path
781+
*/
782+
const char* make_utf8_path(const wchar_t* s) {
783+
int len;
784+
size_t size = wcslen(s) + 1;
785+
char *p = (char*)malloc(size * sizeof(wchar_t));
786+
len = WideCharToMultiByte(CP_UTF8,0,s,size,p,size * sizeof(wchar_t),NULL,NULL);
787+
return p;
788+
}
789+
#endif
765790

766791
/*
767792
* get_home_path
@@ -783,19 +808,27 @@ get_home_path(char *ret_path)
783808
strlcpy(ret_path, pwd->pw_dir, MAXPGPATH);
784809
return true;
785810
#else
786-
char *tmppath;
787-
811+
wchar_t *tmppath;
812+
wchar_t *vname;
813+
const char *utf8_path;
814+
788815
/*
789816
* Note: We use getenv() here because the more modern SHGetFolderPath()
790817
* would force the backend to link with shell32.lib, which eats valuable
791818
* desktop heap. XXX This function is used only in psql, which already
792819
* brings in shell32 via libpq. Moving this function to its own file
793820
* would keep it out of the backend, freeing it from this concern.
794821
*/
795-
tmppath = getenv("APPDATA");
822+
823+
vname = make_wc_name("APPDATA");
824+
tmppath = _wgetenv(vname);
825+
free(vname);
826+
796827
if (!tmppath)
797828
return false;
798-
snprintf(ret_path, MAXPGPATH, "%s\\postgresql", tmppath);
829+
utf8_path = make_utf8_path(tmppath);
830+
snprintf(ret_path, MAXPGPATH, "%s\\postgresql", utf8_path);
831+
free(utf8_path);
799832
return true;
800833
#endif
801834
}

0 commit comments

Comments
 (0)