1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| #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) { 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); } 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) { strcpy(info->distribution, "macOS"); 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"); }
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); } 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); 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"); } printf("→ 建议并行任务数: %d\n", sys_info.cpu_count); printf("\n=== 工具执行完成 ===\n"); return 0; }
|