Skip to content

Commit

Permalink
Fix invalid EXEFILE and EXEDIR on Windows (lite-xl#1396)
Browse files Browse the repository at this point in the history
* fix(main): fix get_exe_filename returning invalid result on Windows
* fix(main): fix bootstrap not intepreting UTF-8 properly
  • Loading branch information
takase1121 authored Apr 7, 2023
1 parent a0de8ea commit ecb599e
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ static double get_scale(void) {

static void get_exe_filename(char *buf, int sz) {
#if _WIN32
int len = GetModuleFileName(NULL, buf, sz - 1);
buf[len] = '\0';
int len;
wchar_t *buf_w = malloc(sizeof(wchar_t) * sz);
if (buf_w) {
len = GetModuleFileNameW(NULL, buf_w, sz - 1);
buf_w[len] = L'\0';
// if the conversion failed we'll empty the string
if (!WideCharToMultiByte(CP_UTF8, 0, buf_w, -1, buf, sz, NULL, NULL))
buf[0] = '\0';
free(buf_w);
} else {
buf[0] = '\0';
}
#elif __linux__
char path[] = "/proc/self/exe";
ssize_t len = readlink(path, buf, sz - 1);
Expand Down Expand Up @@ -218,9 +228,10 @@ int main(int argc, char **argv) {
const char *init_lite_code = \
"local core\n"
"xpcall(function()\n"
" local match = require('utf8extra').match\n"
" HOME = os.getenv('" LITE_OS_HOME "')\n"
" local exedir = EXEFILE:match('^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$')\n"
" local prefix = os.getenv('LITE_PREFIX') or exedir:match('^(.*)" LITE_PATHSEP_PATTERN "bin$')\n"
" local exedir = match(EXEFILE, '^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$')\n"
" local prefix = os.getenv('LITE_PREFIX') or match(exedir, '^(.*)" LITE_PATHSEP_PATTERN "bin$')\n"
" dofile((MACOS_RESOURCES or (prefix and prefix .. '/share/lite-xl' or exedir .. '/data')) .. '/core/start.lua')\n"
" core = require(os.getenv('LITE_XL_RUNTIME') or 'core')\n"
" core.init()\n"
Expand Down

0 comments on commit ecb599e

Please sign in to comment.