Skip to content

Commit 6f3ed4a

Browse files
committed
Processing (Linux): cache the index of $LANG
1 parent cc8f8ff commit 6f3ed4a

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/common/processing_linux.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ static inline int ffPipe2(int* fds, int flags)
5353
#endif
5454
}
5555

56+
57+
// Not thread-safe
5658
const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool useStdErr)
5759
{
5860
int pipes[2];
@@ -67,16 +69,40 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
6769
posix_spawn_file_actions_addopen(&file_actions, useStdErr ? STDOUT_FILENO : STDERR_FILENO, "/dev/null", O_WRONLY, 0);
6870
pid_t childPid = -1;
6971

70-
char* oldLang = NULL;
71-
int langIndex = -1;
72-
for (int i = 0; environ[i] != NULL; i++)
72+
static char* oldLang = NULL;
73+
static int langIndex = -1;
74+
75+
if (langIndex >= 0)
76+
{
77+
// Found before
78+
if (oldLang) // oldLang was set only if it needed to be changed
79+
{
80+
if (environ[langIndex] != oldLang)
81+
{
82+
// environ is changed outside of this function
83+
langIndex = -1;
84+
}
85+
else
86+
environ[langIndex] = (char*) "LANG=C.UTF-8";
87+
}
88+
}
89+
if (langIndex < 0)
7390
{
74-
if (ffStrStartsWith(environ[i], "LANG="))
91+
for (int i = 0; environ[i] != NULL; i++)
7592
{
76-
oldLang = environ[i];
77-
environ[i] = (char*) "LANG=C";
78-
langIndex = i;
79-
break;
93+
if (ffStrStartsWith(environ[i], "LANG="))
94+
{
95+
langIndex = i;
96+
const char* langValue = environ[i] + 5; // Skip "LANG="
97+
if (ffStrEqualsIgnCase(langValue, "C") ||
98+
ffStrStartsWithIgnCase(environ[i], "C.") ||
99+
ffStrEqualsIgnCase(langValue, "en_US") ||
100+
ffStrStartsWithIgnCase(langValue, "en_US."))
101+
break; // No need to change LANG
102+
oldLang = environ[i];
103+
environ[i] = (char*) "LANG=C.UTF-8"; // Set LANG to C.UTF-8 for consistent output
104+
break;
105+
}
80106
}
81107
}
82108

0 commit comments

Comments
 (0)