-
Notifications
You must be signed in to change notification settings - Fork 2
/
sysinfo.c
194 lines (162 loc) · 4.46 KB
/
sysinfo.c
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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include <sys/system_properties.h>
#define nitems(container) (sizeof((container)) / sizeof((container)[0]))
struct prop {
char *key_name;
char *prop_name;
};
struct android_version {
char *number;
char *name;
};
/* https://en.wikipedia.org/wiki/Android_version_history#Overview */
static struct android_version versions[] = {
{"15", "Android 15"},
{"14", "Android 14"},
{"13", "Android 13"},
{"12.1", "Android 12L"},
{"12", "Android 12"},
{"11", "Android 11"},
{"10", "Android 10"},
{"9.0", "Pie"},
{"8.0", "Oreo"},
{"7.0", "Nougat"},
{"6.0", "Marshmallow"},
{"5.0", "Lollipop"},
{"4.4", "KitKat"},
{"4.1", "Jelly Bean"},
{"4.0", "Ice Cream Sandwich"},
{"3.0", "Honeycomb"},
{"2.3", "Gingerbread"},
{"2.2", "Froyo"},
{"2.0", "Eclair"},
{"1.6", "Donut"},
{"1.5", "Cupcake"},
};
char *
android_to_name(const char *version)
{
int i;
char *name = NULL;
/* https://en.wikipedia.org/wiki/Android_version_history#Overview */
struct android_version versions[] = {
{"11", "Android 11"},
{"10", "Android 10"},
{"9.0", "Pie"},
{"8.0", "Oreo"},
{"7.0", "Nougat"},
{"6.0", "Marshmallow"},
{"5.0", "Lollipop"},
{"4.4", "KitKat"},
{"4.1", "Jelly Bean"},
{"4.0", "Ice Cream Sandwich"},
{"3.0", "Honeycomb"},
{"2.3", "Gingerbread"},
{"2.2", "Froyo"},
{"2.0", "Eclair"},
{"1.6", "Donut"},
{"1.5", "Cupcake"},
};
for (i = 0; i < nitems(versions); i++ ) {
if (!strncmp(version, versions[i].number, 3)) {
name = versions[i].name;
break;
}
}
if (name == NULL) {
for (i = 0; i < nitems(versions); i++ ) {
if (!strncmp(version, versions[i].number, 1)) {
name = versions[i].name;
break;
}
}
}
if (name == NULL)
name = "Unknow";
return (name);
}
static void
system_kernel(void)
{
struct utsname uts;
char build_host[512];
char build_user[512];
__system_property_get("ro.build.host", build_host);
__system_property_get("ro.build.user", build_user);
if (uname(&uts) != -1) {
printf("%-25s: %s %s %s@%s %s %s\n", "Kernel version", uts.sysname,
uts.release, build_user, build_host, uts.version, uts.machine);
}
}
static void
system_cpu(void)
{
int i, fd, bytes;
char *p;
char *s;
char buffer[128];
const char *needle = "Processor :";
fd = open("/proc/cpuinfo", O_RDONLY);
if (fd == -1) {
return;
}
bytes = read(fd, buffer, sizeof(buffer) - 1);
if (bytes == -1) {
goto cleanup;
}
buffer[bytes] = '\0';
p = buffer;
s = strstr(p, "model name :");
if (s == NULL)
s = strstr(p, "Processor :");
if (s == NULL)
goto cleanup;
s += strlen(needle) - 1;
printf("%-25s: ", "CPU");
while ( *s != '\n') {
printf("%c", *s++);
}
printf("\n");
cleanup:
close(fd);
}
static void
system_properties(struct prop *array, int size)
{
int i, len;
char value[1024] = {0};
for (i=0; i < size; i++) {
if (i == 4)
system_kernel();
len = __system_property_get(array[i].prop_name, value);
value[len] = '\0';
if (i == 2) {
printf("%-25s: %s", array[i].key_name, value);
printf(" (%s)\n", android_to_name(value));
} else {
printf("%-25s: %s\n", array[i].key_name, value);
}
}
system_cpu();
}
int
main(void)
{
struct prop props[] = {
{.key_name = "Model name", .prop_name = "ro.product.model"},
{.key_name = "Android codename", .prop_name = "ro.build.product"},
{.key_name = "Android version", .prop_name = "ro.build.version.release"},
{.key_name = "Android SDK version", .prop_name = "ro.build.version.sdk"},
{.key_name = "Baseband version", .prop_name = "gsm.version.baseband"},
{.key_name = "Build number", .prop_name = "ro.build.display.id"},
};
system_properties(props, nitems(props));
return (0);
}