在运行时确定操作系统

发布于 2024-12-24 01:22:27 字数 451 浏览 5 评论 0原文

ISO C 和 POSIX 都不提供在运行时确定底层操作系统的功能。从理论的角度来看,这并不重要,因为 C 为最常见的系统调用提供了包装器,并且从吹毛求疵的角度来看,甚至不必底层操作系统。

然而,在许多现实场景中,事实证明,了解比 C 愿意分享的更多有关主机环境的信息是有帮助的,例如,为了找出存储配置文件的位置或如何调用 select(),所以:

用 C 编写的应用程序是否有一种惯用的方法来在运行时确定底层操作系统?

至少,我可以轻松地在 Linux、Windows、BSD 和 MacOS 之间做出选择吗?

我目前的猜测是检查某些文件/目录是否存在,例如 C:\/,但这种方法似乎不可靠。也许查询一系列此类来源可能有助于建立“操作系统指纹”的概念,从而提高可靠性。无论如何,我期待您的建议。

Neither ISO C nor POSIX offer functionality to determine the underlying OS during runtime. From a theoretical point of view, it doesn't matter since C offers wrappers for the most common system calls, and from a nit-picking point of view, there doesn't even have to be an underlying OS.

However, in many real-world scenarios, it has proven helpful to know more about the host environment than C is willing to share, e.g. in order to find out where to store config files or how to call select(), so:

Is there an idiomatic way for an application written in C to determine the underlying OS during runtime?

At least, can I easily decide between Linux, Windows, BSD and MacOS?

My current guess is to check for the existence of certain files/directories, such as C:\ or /, but this approach seems unreliable. Maybe querying a series of such sources may help to establish the notion of "OS fingerprints", thus increasing reliability. Anyway, I'm looking forward to your suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尐籹人 2024-12-31 01:22:27

实际上,大多数系统都有一个 uname 命令来显示当前正在使用的内核。在 Mac OS 上,这通常是“Darwin”,在 Linux 上,它只是简单的“Linux”,在 Windows 上,它是“ERROR”,而 FreeBSD 将返回“FreeBSD”。

更完整的 uname 输出列表

我很确定有一个uname 的 C 等效项,因此您不需要 system()

Actually, most systems have a uname command which shows the current kernel in use. On Mac OS, this is usually "Darwin", on Linux it's just plain "Linux", on Windows it's "ERROR" and FreeBSD will return "FreeBSD".

More complete list of uname outputs

I'm pretty sure that there's a C equivalent for uname, so you won't need system()

我恋#小黄人 2024-12-31 01:22:27

如果您使用的是 POSIX 系统,则可以从 调用 uname()。

这显然不是 100% 可移植的,但我认为不会有任何方法可以在运行时实现这一点。

有关详细信息,请参阅手册页

IF you are on a POSIX system, you can call uname() from <sys/utsname.h>.

This obviously isn't 100% portable, but I don't think there will be any method that can grant that at runtime.

see the man page for details

友谊不毕业 2024-12-31 01:22:27

运行时不是确定这一点的时候,因为如果没有一个平台的史诗般的拼凑二进制文件将无法在另一个平台上运行,您应该只在平台敏感代码周围使用#ifdef

Runtime isn't the time to determine this, being that without epic kludges binaries for one platform won't run on another, you should just use #ifdefs around the platform sensitive code.

残疾 2024-12-31 01:22:27

接受的答案指出uname,但没有提供最小的工作示例,因此这里适合任何感兴趣的人 - 希望它能为您节省我所花费的时间:(

#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>

int main(void) {
   struct utsname buffer;
   if (uname(&buffer) != 0) {
      perror("uname");
      exit(0);
   }
   printf("OS: %s\n", buffer.sysname);

   return 0;
}

可能)输出:

操作系统:Linux

PS:不幸的是,这使用了 POSIX 标头: 编译由于缺少文件 sys/utsname.h 而失败,该文件很可能无法在 Windows 中运行。

The accepted answer states uname, but doesn't provide a minimal working example, so here it is for anyone interested-hope it will save you the time it took for me:

#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>

int main(void) {
   struct utsname buffer;
   if (uname(&buffer) != 0) {
      perror("uname");
      exit(0);
   }
   printf("OS: %s\n", buffer.sysname);

   return 0;
}

(Possible) Output:

OS: Linux

PS: Unfortunately, this uses a POSIX header: Compilation fails due to missing file sys/utsname.h, which most probably won't work in Windows.

初与友歌 2024-12-31 01:22:27
if (strchr(getenv("PATH"),'\\'))
    puts("You may be on windows...");

即使我同意“运行时不是确定这个的时间......”

if (strchr(getenv("PATH"),'\\'))
    puts("You may be on windows...");

Even do I agree that "Runtime isn't the time to determine this..."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文