7
#define strlcpy strncpy
11
char buf[256], features[1024], model_buf[1024];
12
bool vendor_found=false, model_found=false;
13
bool cache_found=false, features_found=false;
14
bool icache_found=false,dcache_found=false;
15
bool model_hack=false, vendor_hack=false;
17
int family=-1, model=-1, stepping=-1;
18
char p_vendor[256], p_model[256];
23
FILE* f = fopen("/proc/cpuinfo", "r");
27
strcpy(p_model, "MIPS ");
30
strcpy(p_vendor, "HP (DEC) ");
33
strcpy(p_vendor, "HP ");
36
strcpy(p_model, "IA-64 ");
41
while (fgets(buf, 256, f)) {
42
//strip_whitespace(buf);
44
/* there might be conflicts if we dont #ifdef */
46
strstr(buf, "vendor : ")
48
strstr(buf, "cpu\t\t: ")
50
strstr(buf, "machine\t\t: ")
52
strstr(buf, "type\t\t: ")
54
strstr(buf, "cpu\t\t\t: ")
56
strstr(buf, "vendor_id\t: ") || strstr(buf, "system type\t\t: ")
59
if (!vendor_hack && !vendor_found) {
61
strlcpy(p_vendor, strchr(buf, ':') + 2, sizeof(p_vendor));
62
} else if (!vendor_found) {
64
strlcpy(buf2, strchr(buf, ':') + 2, sizeof(p_vendor) - strlen(p_vendor) - 1);
65
strcat(p_vendor, buf2);
71
strstr(buf, "family : ") || strstr(buf, "model name : ")
72
#elif __powerpc__ || __sparc__
73
strstr(buf, "cpu\t\t: ")
75
strstr(buf, "model name\t: ") || strstr(buf, "cpu model\t\t: ")
78
if (!model_hack && !model_found) {
82
if ((coma = strrchr(buf, ','))) { /* we have ", altivec supported" */
83
*coma = '\0'; /* strip the unwanted line */
84
strcpy(features, "altivec");
85
features_found = true;
88
strlcpy(p_model, strchr(buf, ':') + 2, sizeof(p_model));
89
} else if (!model_found) {
91
/* depending on kernel version, family can be either
92
a number or a string. If number, we have a model name,
95
testc = strrchr(buf, ':')+2;
96
if (isdigit(*testc)) {
98
continue; /* skip this line */
102
strlcpy(buf2, strchr(buf, ':') + 2, sizeof(p_model) - strlen(p_model) - 1);
103
strcat(p_model, buf2);
108
/* XXX: hppa: "cpu family : PA-RISC 2.0" */
109
if (strstr(buf, "cpu family\t: ") && family<0) {
110
family = atoi(buf+strlen("cpu family\t: "));
112
/* XXX: hppa: "model : 9000/785/J6000" */
113
if (strstr(buf, "model\t\t: ") && model<0) {
114
model = atoi(buf+strlen("model\t\t: "));
117
if (strstr(buf, "model : ") && model<0) {
118
model = atoi(buf+strlen("model : "));
121
if (strstr(buf, "stepping\t: ") && stepping<0) {
122
stepping = atoi(buf+strlen("stepping\t: "));
125
if (!icache_found && strstr(buf, "I-cache\t\t: ")) {
127
sscanf(buf, "I-cache\t\t: %d", &n);
130
if (!dcache_found && strstr(buf, "D-cache\t\t: ")) {
132
sscanf(buf, "D-cache\t\t: %d", &n);
136
if (!cache_found && strstr(buf, "L2 cache\t: ")) {
138
sscanf(buf, "L2 cache\t: %d", &n);
142
if (!cache_found && (strstr(buf, "cache size\t: ") == buf)) {
144
sscanf(buf, "cache size\t: %d", &n);
148
if (!features_found) {
149
// Some versions of the linux kernel call them flags,
150
// others call them features, so look for both.
151
if ((strstr(buf, "flags\t\t: ") == buf)) {
152
strlcpy(features, strchr(buf, ':') + 2, sizeof(features));
153
} else if ((strstr(buf, "features\t\t: ") == buf)) {
154
strlcpy(features, strchr(buf, ':') + 2, sizeof(features));
155
} else if ((strstr(buf, "features : ") == buf)) { /* ia64 */
156
strlcpy(features, strchr(buf, ':') + 2, sizeof(features));
158
if (strlen(features)) {
159
features_found = true;
163
strcpy(model_buf, p_model);
164
if (family>=0 || model>=0 || stepping>0) {
165
strcat(model_buf, " [");
167
sprintf(buf, "Family %d ", family);
168
strcat(model_buf, buf);
171
sprintf(buf, "Model %d ", model);
172
strcat(model_buf, buf);
175
sprintf(buf, "Stepping %d", stepping);
176
strcat(model_buf, buf);
178
strcat(model_buf, "]");
180
if (strlen(features)) {
181
strcat(model_buf, "[");
182
strcat(model_buf, features);
183
strcat(model_buf, "]");
187
printf("p_vendor: %s\nm_cache: %d\nmodel_buf: %s\n", p_vendor, m_cache, model_buf);