首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C:如何获取操作系统名称( Ubuntu、Debian、Centos、..)如果定义操作系统名称= ubuntu

C:如何获取操作系统名称( Ubuntu、Debian、Centos、..)如果定义操作系统名称= ubuntu
EN

Stack Overflow用户
提问于 2018-07-23 21:14:04
回答 1查看 114关注 0票数 0

我想用C写一个脚本,只在Ubuntu系统上运行。我得到了操作系统的名称。但。我没有定义if子句。谢谢你的帮助

像这样的算法:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-23 22:14:28

我认为检查操作系统名称最可靠的方法是使用uname实用程序。

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int match_OS(const char* name) {
    FILE *p;
    int result;
    char *s = malloc(1024);
    p = popen("uname -v", "r");
    s = fgets(s, 1024, p);
    result = (strstr(s, name) != NULL);
    pclose(p);
    free(s);
    return result;
}

int main() {
    if (match_OS("Ubuntu")) {
        puts("This is Ubuntu");
    }
    else {
        puts("This isn't Ubuntu");
    }
    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51479705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档