Skip to content

Commit 242f5f7

Browse files
authored
improve locale fallback
1 parent 8a72ffb commit 242f5f7

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

useful-tools/lib/anylinux.c

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,63 @@ typedef int (*execve_func_t)(const char *filename, char *const argv[], char *con
3737

3838
__attribute__((constructor))
3939
static void locale_fix_init(void) {
40-
if (!setlocale(LC_ALL, "")) {
41-
LOG("Failed to set locale, falling back to C locale.");
42-
if (!setlocale(LC_ALL, "C")) {
43-
LOG("Failed to setlocale(LC_ALL, \"C\"): %s", strerror(errno));
44-
}
45-
if (setenv("LC_ALL", "C", 1) != 0) {
46-
LOG("Failed to setenv(LC_ALL, \"C\"): %s", strerror(errno));
40+
if (! setlocale(LC_ALL, "")) {
41+
LOG("Failed to set locale, falling back to bundled C.UTF-8 locale");
42+
// Check if bundled C.UTF-8 locale is available
43+
const char *appdir = getenv("APPDIR");
44+
if (appdir) {
45+
char locale_path[PATH_MAX];
46+
snprintf(locale_path, sizeof(locale_path), "%s/lib/locale/C.utf8", appdir);
47+
48+
if (access(locale_path, F_OK) == 0) {
49+
LOG("Found bundled C.UTF-8 locale: %s", locale_path);
50+
// Set LOCPATH to the bundled locales directory
51+
char locpath[PATH_MAX];
52+
snprintf(locpath, sizeof(locpath), "%s/lib/locale", appdir);
53+
if (setenv("LOCPATH", locpath, 1) == 0) {
54+
LOG("Set LOCPATH to %s", locpath);
55+
} else {
56+
LOG("Failed to setenv(LOCPATH, \"%s\"): %s", locpath, strerror(errno));
57+
}
58+
59+
// Set LC_ALL to C.UTF-8
60+
LOG("Setting LC_ALL to C.UTF-8");
61+
if (setenv("LC_ALL", "C.UTF-8", 1) == 0) {
62+
if (! setlocale(LC_ALL, "")) {
63+
LOG("Failed to set locale with C.UTF-8, falling back to bare C locale.");
64+
if (! setlocale(LC_ALL, "C")) {
65+
LOG("Failed to setlocale(LC_ALL, \"C\"): %s", strerror(errno));
66+
}
67+
if (setenv("LC_ALL", "C", 1) != 0) {
68+
LOG("Failed to setenv(LC_ALL, \"C\"): %s", strerror(errno));
69+
}
70+
}
71+
} else {
72+
LOG("Failed to setenv(LC_ALL, \"C.UTF-8\"): %s", strerror(errno));
73+
if (!setlocale(LC_ALL, "C")) {
74+
LOG("Failed to setlocale(LC_ALL, \"C\"): %s", strerror(errno));
75+
}
76+
if (setenv("LC_ALL", "C", 1) != 0) {
77+
LOG("Failed to setenv(LC_ALL, \"C\"): %s", strerror(errno));
78+
}
79+
}
80+
} else {
81+
LOG("Bundled C.UTF-8 locale not found at %s, falling back to C locale.", locale_path);
82+
if (!setlocale(LC_ALL, "C")) {
83+
LOG("Failed to setlocale(LC_ALL, \"C\"): %s", strerror(errno));
84+
}
85+
if (setenv("LC_ALL", "C", 1) != 0) {
86+
LOG("Failed to setenv(LC_ALL, \"C\"): %s", strerror(errno));
87+
}
88+
}
89+
} else {
90+
LOG("APPDIR not set, cannot check for bundled locales. Falling back to C locale.");
91+
if (!setlocale(LC_ALL, "C")) {
92+
LOG("Failed to setlocale(LC_ALL, \"C\"): %s", strerror(errno));
93+
}
94+
if (setenv("LC_ALL", "C", 1) != 0) {
95+
LOG("Failed to setenv(LC_ALL, \"C\"): %s", strerror(errno));
96+
}
4797
}
4898
}
4999
}

0 commit comments

Comments
 (0)