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