Add Android 10 and 11 and following style(9)
[android-sysinfo/.git] / sysinfo.c
CommitLineData
e4b66277 1#include <fcntl.h>
410f5cae 2#include <stdio.h>
e4b66277
SB
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
410f5cae 6
e4b66277
SB
7#include <sys/types.h>
8#include <sys/uio.h>
410f5cae
SB
9#include <sys/utsname.h>
10#include <sys/system_properties.h>
11
ade99a66
SB
12#define nitems(container) (sizeof((container)) / sizeof((container)[0]))
13
410f5cae
SB
14struct prop {
15 char *key_name;
16 char *prop_name;
17};
18
676aac54
SB
19struct android_version {
20 char *number;
21 char *name;
22};
23
24char *
25android_to_name(const char *version)
26{
27 int i;
28 char *name = NULL;
29
30 /* https://en.wikipedia.org/wiki/Android_version_history#Overview */
31 struct android_version versions[] = {
db3a7d35
SB
32 {"11", "Android 11"},
33 {"10", "Android 10"},
676aac54
SB
34 {"9.0", "Pie"},
35 {"8.0", "Oreo"},
36 {"7.0", "Nougat"},
37 {"6.0", "Marshmallow"},
38 {"5.0", "Lollipop"},
39 {"4.4", "KitKat"},
40 {"4.1", "Jelly Bean"},
41 {"4.0", "Ice Cream Sandwich"},
42 {"3.0", "Honeycomb"},
43 {"2.3", "Gingerbread"},
44 {"2.2", "Froyo"},
45 {"2.0", "Eclair"},
46 {"1.6", "Donut"},
47 {"1.5", "Cupcake"},
48 };
49
50
ade99a66 51 for (i = 0; i < nitems(versions); i++ ) {
676aac54
SB
52 if (!strncmp(version, versions[i].number, 3)) {
53 name = versions[i].name;
54 break;
55 }
56 }
57
58 if (name == NULL) {
ade99a66 59 for (i = 0; i < nitems(versions); i++ ) {
676aac54
SB
60 if (!strncmp(version, versions[i].number, 1)) {
61 name = versions[i].name;
62 break;
63 }
64 }
65 }
66
67 if (name == NULL)
68 name = "Unknow";
69
70 return (name);
71}
72
db3a7d35
SB
73static void
74system_kernel(void)
410f5cae
SB
75{
76 struct utsname uts;
77 char build_host[512];
78 char build_user[512];
79
80 __system_property_get("ro.build.host", build_host);
81 __system_property_get("ro.build.user", build_user);
82
83 if (uname(&uts) != -1) {
84 printf("%-25s: %s %s %s@%s %s %s\n", "Kernel version", uts.sysname,
85 uts.release, build_user, build_host, uts.version, uts.machine);
86 }
87}
88
db3a7d35
SB
89static void
90system_cpu(void)
e4b66277
SB
91{
92 int i, fd, bytes;
93 char *p;
94 char buffer[1024];
95 const char *needle = "Processor :";
96
97 fd = open("/proc/cpuinfo", O_RDONLY);
98 if (fd == -1) {
99 return;
100 }
101
102 bytes = read(fd, buffer, sizeof(buffer) - 1);
103 if (bytes == -1) {
104 return;
105 }
106
107 buffer[bytes] = '\0';
108 p = buffer;
109
110 p = strstr(p, "Processor :");
111 p += strlen(needle) - 1;
112
113 printf("%-25s: ", "CPU");
114 while ( *p != '\n') {
115 printf("%c", *p++);
116 }
117
118 printf("\n");
119
120 close(fd);
121}
122
db3a7d35
SB
123static void
124system_properties(struct prop *array, int size)
410f5cae
SB
125{
126 int i, len;
127 char value[1024] = {0};
128
129 for (i=0; i < size; i++) {
130 if (i == 4)
131 system_kernel();
132
133 len = __system_property_get(array[i].prop_name, value);
134 value[len] = '\0';
676aac54
SB
135 if (i == 2) {
136 printf("%-25s: %s", array[i].key_name, value);
137 printf(" (%s)\n", android_to_name(value));
138 } else {
139 printf("%-25s: %s\n", array[i].key_name, value);
140 }
410f5cae
SB
141 }
142
e4b66277 143 system_cpu();
410f5cae
SB
144}
145
146int
147main(void)
148{
149 struct prop props[] = {
a2c4f26d 150 {.key_name = "Model name", .prop_name = "ro.product.model"},
410f5cae
SB
151 {.key_name = "Android codename", .prop_name = "ro.build.product"},
152 {.key_name = "Android version", .prop_name = "ro.build.version.release"},
153 {.key_name = "Android SDK version", .prop_name = "ro.build.version.sdk"},
154 {.key_name = "Baseband version", .prop_name = "gsm.version.baseband"},
155 {.key_name = "Build number", .prop_name = "ro.build.display.id"},
156 };
157
ade99a66 158 system_properties(props, nitems(props));
410f5cae
SB
159
160 return (0);
161}