考虑以下简约代码片段:
#include <stdio.h>
#include <errno.h>
#include <locale.h>
int main() {
const char *locale = setlocale(LC_ALL, "");
if (errno == ENOENT) {
printf("Locale %s unavailable.\n", locale ? locale : "<unknown>");
} else if (errno) {
printf("setlocale() failed: errno = %d\n", errno);
perror("setlocale() failed");
}
if (locale) {
printf("Current locale: %s\n", locale);
}
return errno;
}
这在许多UNIX机器上成功运行(Solaris 8,Debian Linux 8,OpenBSD 5.8)。但是,有一些例外。
根据manual page,setlocale
返回NULL
,如果进程环境(LC_ALL
等)指定的区域设置不可用(即缺少相应的文件,需要使用localegen
或类似工具生成。测试显示errno
在这种情况下设置为ENOENT
,但在其中一个 Debian 7 框中setlocale
会返回有效的非NULL
区域设置字符串和将errno
设置为ENOENT
。
Cygwin 表现出另一种奇怪的行为:对于每个单字节Cyrillic语言环境(ru_RU.CP1251
,ru_RU.CP866
,ru_RU.KOI8-R
,{{1 }}),ru_RU.ISO-8859-5
返回正确的区域设置字符串和将setlocale
设置为errno
。与此同时,EILSEQ
不受影响。
如何进一步诊断上述任何病例?
答案 0 :(得分:1)
POSIX does not specify setlocale
errno
如何影响errno
(请参阅“没有指定错误”的ERRORS部分)。 setlocale
来电后,您可以放心地忽略setlocale
的价值。
errno
的实现永远不应该触及setlocale
,但似乎您测试的某些实现在这方面存在问题。我建议您针对提供错误private static String chromeDriverLocation = "drivers/chromedriver.exe";
File cDriver = new File(DriverFactory.class.getResource(chromeDriverLocation).getFile());
// Is it executable
if (!cDriver.canExecute()) {
cDriver.setExecutable(true);
}
System.setProperty("webdriver.chrome.driver", DriverFactory.class.getResource(chromeDriverLocation).getFile());
实现的项目提交错误报告。
答案 1 :(得分:1)
In general when it comes to most functions from POSIX you are not supposed to look at errno
unless the function returned an error.
POSIX documents it here.
The value of errno should only be examined when it is indicated to be valid by a function's return value.
Functions are allowed to change errno
even when they succeed:
The setting of errno after a successful call to a function is unspecified unless the description of that function specifies that errno shall not be modified.
This happens quite commonly where a function tries one thing, that fails, tries another thing and that succeeds. It's often too expensive or too hard to preserve errno around system calls in libraries.
Looking at errno
without the function signaling an error to you is a bug, you can't do that.