在运行时确定操作系统 [英] Determine OS during runtime

查看:71
本文介绍了在运行时确定操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ISO C和POSIX都不提供在运行时确定底层OS的功能.从理论的角度来看,这没关系,因为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.

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

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:

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

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

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

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

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

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.

推荐答案

实际上,大多数系统都有一个uname命令,该命令显示了正在使用的当前内核.在Mac OS上,通常是"Darwin",在Linux上通常是"Linux",在Windows上是"ERROR",FreeBSD将返回"FreeBSD".

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".

uname输出的更完整列表

More complete list of uname outputs

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

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

这篇关于在运行时确定操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆