2. uname - 获取系统信息 Link to heading

函数介绍 Link to heading

uname系统调用用于获取当前Linux系统的详细信息,包括内核名称、版本、硬件架构等。这些信息对于系统管理和程序兼容性检查非常有用。

函数原型 Link to heading

#include <sys/utsname.h>

int uname(struct utsname *buf);

功能 Link to heading

获取系统标识信息,包括内核名称、网络节点名、内核版本、硬件架构等。

参数 Link to heading

  • struct utsname *buf: 指向utsname结构体的指针,用于存储系统信息
    struct utsname {
        char sysname[];    // 系统名称 (如 "Linux")
        char nodename[];   // 网络节点名 (主机名)
        char release[];    // 内核发行版本
        char version[];    // 内核版本
        char machine[];    // 硬件架构
        char domainname[]; // 域名 (NIS/YP)
    };
    

返回值 Link to heading

  • 成功时返回0
  • 失败时返回-1,并设置errno

相似函数 Link to heading

  • sysinfo(): 获取系统统计信息
  • 命令行uname工具

示例代码 Link to heading

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
#include <errno.h>
#include <string.h>

int main() {
    struct utsname system_info;
    
    printf("=== Uname函数示例 ===\n");
    
    // 调用uname获取系统信息
    if (uname(&system_info) == -1) {
        perror("uname调用失败");
        exit(EXIT_FAILURE);
    }
    
    // 打印系统信息
    printf("系统名称 (sysname): %s\n", system_info.sysname);
    printf("网络节点名 (nodename): %s\n", system_info.nodename);
    printf("内核发行版本 (release): %s\n", system_info.release);
    printf("内核版本 (version): %s\n", system_info.version);
    printf("硬件架构 (machine): %s\n", system_info.machine);
    
    #ifdef _GNU_SOURCE
    printf("域名 (domainname): %s\n", system_info.domainname);
    #endif
    
    // 示例应用:根据系统类型执行不同操作
    printf("\n=== 实际应用示例 ===\n");
    
    // 检查操作系统类型
    if (strcmp(system_info.sysname, "Linux") == 0) {
        printf("检测到Linux系统\n");
    } else {
        printf("检测到其他系统: %s\n", system_info.sysname);
    }
    
    // 检查硬件架构
    if (strcmp(system_info.machine, "x86_64") == 0) {
        printf("运行在64位x86架构上\n");
    } else if (strcmp(system_info.machine, "aarch64") == 0) {
        printf("运行在64位ARM架构上\n");
    } else {
        printf("运行在%s架构上\n", system_info.machine);
    }
    
    // 检查内核版本(简单比较)
    printf("内核版本信息: %s\n", system_info.release);
    
    // 解析版本号示例
    int major, minor, patch;
    if (sscanf(system_info.release, "%d.%d.%d", &major, &minor, &patch) == 3) {
        printf("解析的内核版本: %d.%d.%d\n", major, minor, patch);
        
        // 根据内核版本做兼容性检查
        if (major >= 4) {
            printf("内核版本 >= 4.0,支持较新特性\n");
        } else {
            printf("内核版本较低,可能需要特殊处理\n");
        }
    }
    
    // 获取主机名
    printf("主机名: %s\n", system_info.nodename);
    
    return 0;
}

uname函数详解 Link to heading

1. 函数介绍 Link to heading

uname函数是Linux系统中用于获取系统信息的标准函数,它的名字来源于"Unix name"。这个函数就像系统的"身份证"一样,能够提供关于当前运行系统的详细信息,包括操作系统名称、版本、硬件架构等。

可以把uname想象成一个"系统信息查询员",当你需要了解当前系统的基本信息时,它能够快速提供准确的答案。无论是在程序中需要根据系统类型执行不同逻辑,还是在调试时需要确认系统环境,uname都是一个非常实用的工具。

使用场景:

  • 程序的系统兼容性检查
  • 系统信息显示和日志记录
  • 根据系统类型执行特定代码
  • 系统管理和监控工具
  • 软件安装程序的环境检测

2. 函数原型 Link to heading

#include <sys/utsname.h>

int uname(struct utsname *buf);

3. 功能 Link to heading

uname函数的主要功能是获取当前系统的标识信息。它填充一个utsname结构体,其中包含以下系统信息:

  • 操作系统名称
  • 网络节点名称(主机名)
  • 操作系统发行版本
  • 操作系统版本
  • 硬件架构类型

4. 参数 Link to heading

  • buf: 系统信息缓冲区
    • 类型:struct utsname*
    • 含义:指向utsname结构体的指针,用于存储系统信息
    • 该结构体包含以下字段:
      • sysname[]: 操作系统名称
      • nodename[]: 网络节点名称(主机名)
      • release[]: 操作系统发行版本
      • version[]: 操作系统版本
      • machine[]: 硬件架构类型
      • domainname[]: 网络域名(某些系统支持)

5. 返回值 Link to heading

  • 成功: 返回0
  • 失败: 返回-1,并设置errno错误码
    • EFAULT:buf参数指向无效内存地址
    • EINVAL:参数无效(理论上不会发生)

6. 相似函数或关联函数 Link to heading

  • gethostname(): 获取主机名
  • sysconf(): 获取系统配置信息
  • getdomainname(): 获取网络域名
  • uname命令: 命令行下的uname工具
  • /proc/version: 系统版本信息文件
  • system(): 执行系统命令

7. 示例代码 Link to heading

示例1:基础uname使用 - 获取系统信息 Link to heading

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <errno.h>

int main() {
    struct utsname system_info;
    
    printf("=== 基础uname使用示例 ===\n");
    
    // 调用uname函数获取系统信息
    if (uname(&system_info) == -1) {
        perror("uname调用失败");
        exit(EXIT_FAILURE);
    }
    
    // 显示系统信息
    printf("系统信息:\n");
    printf("----------------------------\n");
    printf("操作系统名称: %s\n", system_info.sysname);
    printf("网络节点名:   %s\n", system_info.nodename);
    printf("内核发行版:   %s\n", system_info.release);
    printf("内核版本:     %s\n", system_info.version);
    printf("硬件架构:     %s\n", system_info.machine);
    
    // 如果支持域名信息
    #ifdef _GNU_SOURCE
    printf("网络域名:     %s\n", system_info.domainname);
    #endif
    
    printf("----------------------------\n");
    
    // 分析系统类型
    printf("\n系统类型分析:\n");
    if (strcmp(system_info.sysname, "Linux") == 0) {
        printf("✓ 这是一个Linux系统\n");
    } else if (strcmp(system_info.sysname, "Darwin") == 0) {
        printf("✓ 这是一个macOS系统\n");
    } else {
        printf("? 这是一个%s系统\n", system_info.sysname);
    }
    
    // 分析硬件架构
    printf("硬件架构分析:\n");
    if (strcmp(system_info.machine, "x86_64") == 0) {
        printf("✓ 64位x86架构\n");
    } else if (strcmp(system_info.machine, "i386") == 0 || 
               strcmp(system_info.machine, "i686") == 0) {
        printf("✓ 32位x86架构\n");
    } else if (strncmp(system_info.machine, "arm", 3) == 0) {
        printf("✓ ARM架构\n");
    } else if (strncmp(system_info.machine, "aarch64", 7) == 0) {
        printf("✓ 64位ARM架构\n");
    } else {
        printf("? %s架构\n", system_info.machine);
    }
    
    return 0;
}

示例2:详细的系统信息分析 Link to heading

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <ctype.h>

// 判断是否为Linux系统
int is_linux_system(const char* sysname) {
    return (strcmp(sysname, "Linux") == 0);
}

// 判断是否为64位系统
int is_64bit_system(const char* machine) {
    return (strcmp(machine, "x86_64") == 0 || 
            strcmp(machine, "aarch64") == 0 ||
            strcmp(machine, "ppc64") == 0 ||
            strcmp(machine, "s390x") == 0 ||
            strncmp(machine, "mips64", 6) == 0);
}

// 判断是否为ARM架构
int is_arm_architecture(const char* machine) {
    return (strncmp(machine, "arm", 3) == 0 || 
            strncmp(machine, "aarch64", 7) == 0);
}

// 分析内核版本
void analyze_kernel_version(const char* release) {
    printf("内核版本分析:\n");
    
    // Linux内核版本通常是 x.y.z 格式
    int major, minor, patch;
    if (sscanf(release, "%d.%d.%d", &major, &minor, &patch) == 3) {
        printf("  主版本号: %d\n", major);
        printf("  次版本号: %d\n", minor);
        printf("  修订版本: %d\n", patch);
        
        // 根据主版本号判断内核新旧
        if (major >= 5) {
            printf("  ✓ 现代Linux内核 (5.x 或更新)\n");
        } else if (major == 4) {
            printf("  ✓ 较新的Linux内核 (4.x)\n");
        } else if (major == 3) {
            printf("  ⚠ 较旧的Linux内核 (3.x)\n");
        } else {
            printf("  ⚠ 非常旧的Linux内核 (%d.x)\n", major);
        }
    } else {
        printf("  无法解析版本格式: %s\n", release);
    }
}

// 分析主机名
void analyze_hostname(const char* nodename) {
    printf("主机名分析:\n");
    printf("  主机名: %s\n", nodename);
    
    // 检查主机名长度
    size_t len = strlen(nodename);
    printf("  长度: %zu 字符\n", len);
    
    // 检查是否包含特殊字符
    int has_special = 0;
    for (size_t i = 0; i < len; i++) {
        if (!isalnum(nodename[i]) && nodename[i] != '-' && nodename[i] != '.') {
            has_special = 1;
            break;
        }
    }
    
    if (has_special) {
        printf("  ⚠ 主机名包含特殊字符\n");
    } else {
        printf("  ✓ 主机名格式规范\n");
    }
}

// 显示系统兼容性信息
void show_compatibility_info(const struct utsname* info) {
    printf("系统兼容性信息:\n");
    
    // 可执行文件格式
    if (is_64bit_system(info->machine)) {
        printf("  可执行格式: 64位 ELF\n");
    } else {
        printf("  可执行格式: 32位 ELF\n");
    }
    
    // 库兼容性
    if (is_linux_system(info->sysname)) {
        printf("  系统调用: Linux ABI\n");
    }
    
    // 字节序信息(通过架构推断)
    if (strcmp(info->machine, "x86_64") == 0 || 
        strcmp(info->machine, "i386") == 0) {
        printf("  字节序: 小端序 (Little Endian)\n");
    }
}

int main() {
    struct utsname system_info;
    
    printf("=== 详细系统信息分析 ===\n\n");
    
    // 获取系统信息
    if (uname(&system_info) == -1) {
        perror("uname调用失败");
        exit(EXIT_FAILURE);
    }
    
    // 基本信息显示
    printf("1. 基本系统信息:\n");
    printf("   操作系统: %s\n", system_info.sysname);
    printf("   主机名:   %s\n", system_info.nodename);
    printf("   内核版本: %s\n", system_info.release);
    printf("   构建版本: %s\n", system_info.version);
    printf("   硬件架构: %s\n", system_info.machine);
    
    // 详细分析
    printf("\n2. 详细分析:\n");
    analyze_hostname(system_info.nodename);
    printf("\n");
    analyze_kernel_version(system_info.release);
    printf("\n");
    show_compatibility_info(&system_info);
    
    // 系统分类
    printf("\n3. 系统分类:\n");
    if (is_linux_system(system_info.sysname)) {
        printf("   ✓ Linux系统家族\n");
        
        // 进一步分类Linux发行版(基于内核版本等信息)
        if (strstr(system_info.version, "Ubuntu")) {
            printf("   ✓ 可能是Ubuntu发行版\n");
        } else if (strstr(system_info.version, "Debian")) {
            printf("   ✓ 可能是Debian发行版\n");
        } else if (strstr(system_info.version, "CentOS") || 
                   strstr(system_info.version, "Red Hat")) {
            printf("   ✓ 可能是Red Hat系列发行版\n");
        } else {
            printf("   ✓ 其他Linux发行版\n");
        }
    }
    
    if (is_64bit_system(system_info.machine)) {
        printf("   ✓ 64位系统\n");
    } else {
        printf("   ✓ 32位系统\n");
    }
    
    if (is_arm_architecture(system_info.machine)) {
        printf("   ✓ ARM架构系统\n");
    }
    
    // 生成系统指纹(用于唯一标识)
    printf("\n4. 系统指纹:\n");
    printf("   指纹字符串: %s-%s-%s\n", 
           system_info.sysname, 
           system_info.release, 
           system_info.machine);
    
    // 应用场景示例
    printf("\n5. 应用场景适配:\n");
    
    // 根据系统类型决定编译选项
    if (is_linux_system(system_info.sysname)) {
        printf("   编译建议: 使用Linux特定优化\n");
    }
    
    // 根据架构决定二进制分发
    if (is_64bit_system(system_info.machine)) {
        printf("   分发建议: 提供64位版本\n");
    } else {
        printf("   分发建议: 提供32位版本\n");
    }
    
    if (is_arm_architecture(system_info.machine)) {
        printf("   优化建议: 针对ARM架构优化\n");
    }
    
    return 0;
}

示例3:系统信息比较和兼容性检查 Link to heading

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <time.h>

// 系统信息结构体
typedef struct {
    struct utsname info;
    time_t timestamp;
} system_snapshot_t;

// 保存系统快照
int save_system_snapshot(system_snapshot_t* snapshot) {
    if (uname(&snapshot->info) == -1) {
        return -1;
    }
    snapshot->timestamp = time(NULL);
    return 0;
}

// 比较两个系统快照
void compare_system_snapshots(const system_snapshot_t* snap1, 
                             const system_snapshot_t* snap2) {
    printf("=== 系统信息比较 ===\n");
    printf("快照1时间: %s", ctime(&snap1->timestamp));
    printf("快照2时间: %s", ctime(&snap2->timestamp));
    
    printf("比较结果:\n");
    
    // 比较操作系统名称
    if (strcmp(snap1->info.sysname, snap2->info.sysname) == 0) {
        printf("✓ 操作系统名称相同: %s\n", snap1->info.sysname);
    } else {
        printf("✗ 操作系统名称不同: %s vs %s\n", 
               snap1->info.sysname, snap2->info.sysname);
    }
    
    // 比较主机名
    if (strcmp(snap1->info.nodename, snap2->info.nodename) == 0) {
        printf("✓ 主机名相同: %s\n", snap1->info.nodename);
    } else {
        printf("⚠ 主机名不同: %s vs %s\n", 
               snap1->info.nodename, snap2->info.nodename);
    }
    
    // 比较内核版本
    if (strcmp(snap1->info.release, snap2->info.release) == 0) {
        printf("✓ 内核版本相同: %s\n", snap1->info.release);
    } else {
        printf("⚠ 内核版本不同: %s vs %s\n", 
               snap1->info.release, snap2->info.release);
    }
    
    // 比较硬件架构
    if (strcmp(snap1->info.machine, snap2->info.machine) == 0) {
        printf("✓ 硬件架构相同: %s\n", snap1->info.machine);
    } else {
        printf("✗ 硬件架构不同: %s vs %s\n", 
               snap1->info.machine, snap2->info.machine);
    }
}

// 检查系统兼容性
int check_system_compatibility(const struct utsname* info) {
    printf("=== 系统兼容性检查 ===\n");
    int compatible = 1;
    
    // 检查操作系统
    if (strcmp(info->sysname, "Linux") != 0) {
        printf("✗ 不支持的操作系统: %s\n", info->sysname);
        compatible = 0;
    } else {
        printf("✓ 支持的操作系统: %s\n", info->sysname);
    }
    
    // 检查内核版本(假设需要3.0以上)
    int major, minor;
    if (sscanf(info->release, "%d.%d", &major, &minor) >= 2) {
        if (major >= 3) {
            printf("✓ 支持的内核版本: %s\n", info->release);
        } else {
            printf("✗ 内核版本过低: %s (需要3.0+)\n", info->release);
            compatible = 0;
        }
    } else {
        printf("? 无法确定内核版本格式\n");
    }
    
    // 检查硬件架构(假设支持x86_64和ARM64)
    if (strcmp(info->machine, "x86_64") == 0 || 
        strcmp(info->machine, "aarch64") == 0) {
        printf("✓ 支持的硬件架构: %s\n", info->machine);
    } else {
        printf("⚠ 可能不支持的硬件架构: %s\n", info->machine);
        // 这里可以根据需要决定是否标记为不兼容
    }
    
    return compatible;
}

// 生成系统报告
void generate_system_report(const struct utsname* info) {
    printf("\n=== 系统详细报告 ===\n");
    
    // 基本信息
    printf("【基本信息】\n");
    printf("  操作系统: %s\n", info->sysname);
    printf("  主机名称: %s\n", info->nodename);
    printf("  硬件架构: %s\n", info->machine);
    
    // 内核信息
    printf("【内核信息】\n");
    printf("  发行版本: %s\n", info->release);
    printf("  构建信息: %s\n", info->version);
    
    // 系统特征
    printf("【系统特征】\n");
    
    // 架构特征
    if (strcmp(info->machine, "x86_64") == 0) {
        printf("  架构类型: 64位Intel/AMD x86\n");
        printf("  指令集: 支持SSE, AVX等扩展指令\n");
    } else if (strcmp(info->machine, "aarch64") == 0) {
        printf("  架构类型: 64位ARM\n");
        printf("  指令集: ARMv8-A架构\n");
    } else if (strncmp(info->machine, "arm", 3) == 0) {
        printf("  架构类型: 32位ARM\n");
        printf("  指令集: ARM架构\n");
    } else {
        printf("  架构类型: %s\n", info->machine);
    }
    
    // 操作系统特征
    if (strcmp(info->sysname, "Linux") == 0) {
        printf("  系统类型: 类Unix操作系统\n");
        printf("  系统调用: POSIX兼容\n");
        printf("  文件系统: 支持ext4, xfs, btrfs等\n");
    }
    
    // 版本特征
    printf("【版本特征】\n");
    if (strstr(info->version, "Ubuntu")) {
        printf("  发行版: Ubuntu系列\n");
    } else if (strstr(info->version, "Debian")) {
        printf("  发行版: Debian系列\n");
    } else if (strstr(info->version, "CentOS") || 
               strstr(info->version, "Red Hat")) {
        printf("  发行版: Red Hat系列\n");
    } else if (strstr(info->version, "SUSE")) {
        printf("  发行版: SUSE系列\n");
    }
    
    // 安全特征
    printf("【安全特征】\n");
    printf("  用户权限: 支持多用户权限管理\n");
    printf("  进程隔离: 支持进程间隔离\n");
    printf("  内存保护: 支持内存保护机制\n");
}

int main() {
    struct utsname current_info;
    system_snapshot_t snapshot1, snapshot2;
    
    printf("=== 系统信息综合应用示例 ===\n\n");
    
    // 获取当前系统信息
    if (uname(&current_info) == -1) {
        perror("uname调用失败");
        exit(EXIT_FAILURE);
    }
    
    // 保存快照
    if (save_system_snapshot(&snapshot1) == -1) {
        perror("保存快照1失败");
        exit(EXIT_FAILURE);
    }
    
    printf("1. 当前系统信息:\n");
    printf("   操作系统: %s\n", current_info.sysname);
    printf("   内核版本: %s\n", current_info.release);
    printf("   硬件架构: %s\n", current_info.machine);
    
    // 模拟系统变化(实际中可能需要等待系统更新)
    sleep(1);
    
    if (save_system_snapshot(&snapshot2) == -1) {
        perror("保存快照2失败");
        exit(EXIT_FAILURE);
    }
    
    // 比较快照
    printf("\n2. 系统快照比较:\n");
    compare_system_snapshots(&snapshot1, &snapshot2);
    
    // 兼容性检查
    printf("\n3. 系统兼容性检查:\n");
    int compatible = check_system_compatibility(&current_info);
    if (compatible) {
        printf("✓ 系统兼容性检查通过\n");
    } else {
        printf("✗ 系统兼容性检查未通过\n");
    }
    
    // 生成详细报告
    printf("\n4. 生成系统详细报告:\n");
    generate_system_report(&current_info);
    
    // 应用场景演示
    printf("\n5. 应用场景演示:\n");
    
    // 根据系统信息选择不同的处理逻辑
    if (strcmp(current_info.sysname, "Linux") == 0) {
        printf("   执行Linux特定代码路径\n");
        
        // 根据架构选择优化
        if (strcmp(current_info.machine, "x86_64") == 0) {
            printf("   启用x86_64优化选项\n");
        } else if (strcmp(current_info.machine, "aarch64") == 0) {
            printf("   启用ARM64优化选项\n");
        }
        
        // 根据内核版本启用特性
        int major, minor;
        if (sscanf(current_info.release, "%d.%d", &major, &minor) >= 2) {
            if (major >= 4) {
                printf("   启用现代内核特性\n");
            }
        }
    }
    
    printf("\n=== 演示完成 ===\n");
    
    return 0;
}

示例4:跨平台系统信息工具 Link to heading

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <time.h>

#ifdef __APPLE__
#include <sys/sysctl.h>
#endif

// 系统信息结构体
typedef struct {
    char os_name[256];
    char hostname[256];
    char kernel_version[256];
    char architecture[256];
    char distribution[256];
    long uptime_seconds;
    int cpu_count;
} extended_system_info_t;

// 获取扩展系统信息
int get_extended_system_info(extended_system_info_t* info) {
    struct utsname basic_info;
    
    // 获取基本系统信息
    if (uname(&basic_info) == -1) {
        return -1;
    }
    
    // 复制基本信息
    strncpy(info->os_name, basic_info.sysname, sizeof(info->os_name) - 1);
    strncpy(info->hostname, basic_info.nodename, sizeof(info->hostname) - 1);
    strncpy(info->kernel_version, basic_info.release, sizeof(info->kernel_version) - 1);
    strncpy(info->architecture, basic_info.machine, sizeof(info->architecture) - 1);
    
    // 初始化其他字段
    strcpy(info->distribution, "Unknown");
    info->uptime_seconds = 0;
    info->cpu_count = 1;
    
    // 根据不同系统获取额外信息
    if (strcmp(basic_info.sysname, "Linux") == 0) {
        // Linux系统特有信息
        
        // 尝试读取发行版信息
        FILE* fp = fopen("/etc/os-release", "r");
        if (fp) {
            char line[256];
            while (fgets(line, sizeof(line), fp)) {
                if (strncmp(line, "PRETTY_NAME=", 12) == 0) {
                    char* start = strchr(line, '"');
                    if (start) {
                        char* end = strchr(start + 1, '"');
                        if (end) {
                            *end = '\0';
                            strncpy(info->distribution, start + 1, sizeof(info->distribution) - 1);
                            break;
                        }
                    }
                }
            }
            fclose(fp);
        }
        
        // 获取系统运行时间
        fp = fopen("/proc/uptime", "r");
        if (fp) {
            double uptime;
            if (fscanf(fp, "%lf", &uptime) == 1) {
                info->uptime_seconds = (long)uptime;
            }
            fclose(fp);
        }
        
        // 获取CPU数量
        fp = fopen("/proc/cpuinfo", "r");
        if (fp) {
            char line[256];
            int cpu_count = 0;
            while (fgets(line, sizeof(line), fp)) {
                if (strncmp(line, "processor", 9) == 0) {
                    cpu_count++;
                }
            }
            if (cpu_count > 0) {
                info->cpu_count = cpu_count;
            }
            fclose(fp);
        }
        
    } 
#ifdef __APPLE__
    else if (strcmp(basic_info.sysname, "Darwin") == 0) {
        // macOS系统特有信息
        strcpy(info->distribution, "macOS");
        
        // 获取CPU数量
        int mib[2] = {CTL_HW, HW_NCPU};
        size_t len = sizeof(info->cpu_count);
        sysctl(mib, 2, &info->cpu_count, &len, NULL, 0);
    }
#endif
    
    return 0;
}

// 格式化显示时间
void format_uptime(long seconds, char* buffer, size_t buffer_size) {
    long days = seconds / 86400;
    long hours = (seconds % 86400) / 3600;
    long minutes = (seconds % 3600) / 60;
    
    if (days > 0) {
        snprintf(buffer, buffer_size, "%ld天 %ld小时 %ld分钟", days, hours, minutes);
    } else if (hours > 0) {
        snprintf(buffer, buffer_size, "%ld小时 %ld分钟", hours, minutes);
    } else {
        snprintf(buffer, buffer_size, "%ld分钟", minutes);
    }
}

// 显示系统信息
void display_system_info(const extended_system_info_t* info) {
    printf("╔══════════════════════════════════════════════════════════════╗\n");
    printf("║                    系统信息报告                              ║\n");
    printf("╠══════════════════════════════════════════════════════════════╣\n");
    printf("║ 操作系统: %-48s ║\n", info->os_name);
    printf("║ 主机名称: %-48s ║\n", info->hostname);
    printf("║ 内核版本: %-48s ║\n", info->kernel_version);
    printf("║ 硬件架构: %-48s ║\n", info->architecture);
    printf("║ 发行版本: %-48s ║\n", info->distribution);
    
    // 显示系统运行时间
    if (info->uptime_seconds > 0) {
        char uptime_str[64];
        format_uptime(info->uptime_seconds, uptime_str, sizeof(uptime_str));
        printf("║ 运行时间: %-48s ║\n", uptime_str);
    }
    
    printf("║ CPU核心数: %-47d ║\n", info->cpu_count);
    printf("╚══════════════════════════════════════════════════════════════╝\n");
}

// 生成JSON格式的系统信息
void generate_json_info(const extended_system_info_t* info) {
    printf("\nJSON格式系统信息:\n");
    printf("{\n");
    printf("  \"os_name\": \"%s\",\n", info->os_name);
    printf("  \"hostname\": \"%s\",\n", info->hostname);
    printf("  \"kernel_version\": \"%s\",\n", info->kernel_version);
    printf("  \"architecture\": \"%s\",\n", info->architecture);
    printf("  \"distribution\": \"%s\",\n", info->distribution);
    printf("  \"uptime_seconds\": %ld,\n", info->uptime_seconds);
    printf("  \"cpu_count\": %d\n", info->cpu_count);
    printf("}\n");
}

// 系统健康检查
void system_health_check(const extended_system_info_t* info) {
    printf("\n系统健康检查:\n");
    printf("----------------\n");
    
    // 检查系统类型
    if (strcmp(info->os_name, "Linux") == 0) {
        printf("✓ Linux系统环境\n");
    } else {
        printf("ℹ 非Linux系统: %s\n", info->os_name);
    }
    
    // 检查架构
    if (strcmp(info->architecture, "x86_64") == 0) {
        printf("✓ 64位x86架构\n");
    } else if (strcmp(info->architecture, "aarch64") == 0) {
        printf("✓ 64位ARM架构\n");
    } else {
        printf("ℹ 其他架构: %s\n", info->architecture);
    }
    
    // 检查CPU数量
    if (info->cpu_count >= 4) {
        printf("✓ 多核心系统 (%d核心)\n", info->cpu_count);
    } else if (info->cpu_count >= 2) {
        printf("✓ 双核心系统\n");
    } else {
        printf("ℹ 单核心系统\n");
    }
    
    // 检查运行时间
    if (info->uptime_seconds > 0) {
        if (info->uptime_seconds > 86400) {  // 超过一天
            printf("✓ 系统稳定运行中\n");
        } else {
            printf("ℹ 系统运行时间较短\n");
        }
    }
}

int main() {
    extended_system_info_t sys_info;
    
    printf("=== 跨平台系统信息工具 ===\n\n");
    
    // 获取扩展系统信息
    if (get_extended_system_info(&sys_info) == -1) {
        perror("获取系统信息失败");
        exit(EXIT_FAILURE);
    }
    
    // 显示系统信息
    display_system_info(&sys_info);
    
    // 系统健康检查
    system_health_check(&sys_info);
    
    // 生成JSON格式信息
    generate_json_info(&sys_info);
    
    // 应用场景示例
    printf("\n应用场景适配:\n");
    printf("----------------\n");
    
    // 根据系统类型选择不同的处理
    if (strcmp(sys_info.os_name, "Linux") == 0) {
        printf("→ 启用Linux优化模式\n");
        
        // 根据发行版调整配置
        if (strstr(sys_info.distribution, "Ubuntu")) {
            printf("→ 应用Ubuntu特定配置\n");
        } else if (strstr(sys_info.distribution, "CentOS") || 
                   strstr(sys_info.distribution, "Red Hat")) {
            printf("→ 应用Red Hat特定配置\n");
        }
        
    } else if (strcmp(sys_info.os_name, "Darwin") == 0) {
        printf("→ 启用macOS优化模式\n");
    } else {
        printf("→ 使用通用配置\n");
    }
    
    // 根据CPU数量调整并行度
    printf("→ 建议并行任务数: %d\n", sys_info.cpu_count);
    
    printf("\n=== 工具执行完成 ===\n");
    
    return 0;
}

编译和运行 Link to heading

# 编译示例1
gcc -o uname_example1 uname_example1.c
./uname_example1

# 编译示例2
gcc -o uname_example2 uname_example2.c
./uname_example2

# 编译示例3
gcc -o uname_example3 uname_example3.c
./uname_example3

# 编译示例4
gcc -o uname_example4 uname_example4.c
./uname_example4

重要注意事项 Link to heading

  1. 结构体大小: utsname结构体的大小在不同系统中可能不同
  2. 域名支持: domainname字段不是所有系统都支持
  3. 权限要求: 通常不需要特殊权限即可调用
  4. 线程安全: uname函数是线程安全的
  5. 错误处理: 失败时主要返回EFAULT错误
  6. 字符编码: 返回的字符串通常是ASCII编码
  7. 缓冲区: 传入的结构体必须由调用者分配

常见应用场景 Link to heading

  1. 系统兼容性检测: 程序启动时检查运行环境
  2. 日志记录: 在日志中记录系统环境信息
  3. 调试信息: 帮助开发者了解运行环境
  4. 配置管理: 根据系统类型选择不同配置
  5. 性能优化: 根据硬件架构选择优化策略

通过这些示例,你可以理解uname函数在获取系统信息方面的强大功能,它为程序提供了识别和适应不同运行环境的能力。