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
|
#include "host.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__linux__)
#include <unistd.h>
#endif
const char *run_host_command(const char *command) {
char buf[64];
char *out = NULL;
size_t outsize = 0;
FILE *p = popen(command, "r");
if (!p) {
fprintf(stderr, "ホストコマンドを実効に失敗: %s", command);
return NULL;
}
while (fgets(buf, sizeof(buf), p) != NULL) {
buf[strcspn(buf, "\n")] = '\0';
#if defined(__linux__)
if (
strstr(buf, "To be filled by O.E.M.") != NULL ||
strstr(buf, "To Be Filled By O.E.M.") != NULL ||
strstr(buf, "OEM") != NULL ||
strstr(buf, "Not Applicable") != NULL ||
strstr(buf, "System Product Name") != NULL ||
strstr(buf, "System Version") != NULL ||
strstr(buf, "Undefined") != NULL ||
strstr(buf, "Default string") != NULL ||
strstr(buf, "Not Specified") != NULL ||
strstr(buf, "Type1ProductConfigId") != NULL ||
strstr(buf, "INVALID") != NULL ||
strstr(buf, "All Series") != NULL
) {
return NULL;
}
#endif
size_t len = strlen(buf);
char *nout = realloc(out, outsize + len + 1);
if (nout == NULL) {
perror("メモリの役割に失敗");
free(out);
pclose(p);
return NULL;
}
out = nout;
memccpy(out + outsize, buf, sizeof(buf), len);
outsize += len;
out[outsize] = '\0';
}
pclose(p);
return out;
}
void display_host_model() {
#if defined(__OpenBSD__)
const char *cmd = run_host_command("sysctl -n hw.vendor && echo \" \" && "
"if [ \"$(sysctl -n hw.version 2>&1)\" != "
"\"sysctl: hw.version: value is not available\" ]; then "
"sysctl -n hw.version && echo \" \"; fi && "
"sysctl -n hw.product");
if (!cmd) return;
printf("%s", cmd);
free((void *)cmd);
#elif defined(__FreeBSD__)
const char *family = run_host_command("kenv | grep smbios.system.family | "
"sed 's/\"//g' | sed 's/smbios.system.family=//'");
if (strncmp(family, " ", strlen(family)) == 0) {
family = run_host_command("kenv | grep smbios.system.version | "
"sed 's/\"//g' | sed 's/smbios.system.version=//'");
}
const char *product = run_host_command("kenv | grep smbios.system.product | "
"sed 's/\"//g' | sed 's/smbios.system.product=//'");
const char *maker = run_host_command("kenv | grep smbios.system.maker | "
"sed 's/\"//g' | sed 's/smbios.system.maker=//'");
printf("%s %s %s", maker, family, product);
if (maker) free((void *)maker);
if (family) free((void *)family);
if (product) free((void *)product);
#elif defined(__NetBSD__)
const char *cmd = run_host_command("sysctl -n machdep.dmi.system-vendor && "
"echo \" \" && sysctl -n machdep.dmi.system-version && "
"echo \" \" && sysctl -n machdep.dmi.system-product");
if (!cmd) return;
printf("%s", cmd);
free((void *)cmd);
#elif defined(__sun)
const char *cmd = run_host_command("smbios | grep \"Product\" | "
"sed 's/ Product: //' | awk '{$1=$1};1' | head -1");
if (!cmd) return;
printf("%s", cmd);
free((void *)cmd);
#elif defined(__linux__)
const char *cmd1 = NULL;
const char *cmd2 = NULL;
if (access("/system/app/", F_OK) != -1) {
cmd1 = "getprop ro.product.brand";
cmd2 = "getprop ro.product.model";
} else if (
access("/sys/devices/virtual/dmi/id/product_name", F_OK) != -1 &&
access("/sys/devices/virtual/dmi/id/product_version", F_OK) != 1
) {
cmd1 = "cat /sys/devices/virtual/dmi/id/product_name";
cmd2 = "cat /sys/devices/virtual/dmi/id/product_version";
} else if (access("/sys/firmware/devicetree/base/model", F_OK) != -1) {
cmd1 = "cat /sys/firmware/devicetree/base/model";
} else if (access("/tmp/sysinfo/model", F_OK) != 1) {
cmd1 = "cat /tmp/sysinfo/model";
}
if (!cmd1) {
printf("Unknown");
} else {
const char *cmd = run_host_command(cmd1);
if (!cmd) return;
printf("%s", cmd);
free((void *)cmd);
}
if (cmd2) {
const char *model = run_host_command(cmd2);
if (!model) return;
printf(" %s", model);
free((void *)model);
}
#elif defined(__APPLE__)
const char *cmd = run_host_command("sysctl -n hw.model");
if (!cmd) return;
printf("%s", cmd);
free((void *)cmd);
FILE *p = popen("kextstat | grep -F -e \"FakeSMC\" -e \"VirtualSMC\"", "r");
if (!p) {
fprintf(stderr, "ホストコマンドを実効に失敗");
return;
}
while (fgets(buf, sizeof(buf), p) != NULL) {
buf[strcspn(buf, "\n")] = '\0';
printf("%s", buf);
}
pclose(p);
#endif
}
|