#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
int main(void) {
int proc_cpuinfo_fd = open("/proc/cpuinfo", O_RDONLY);
if (proc_cpuinfo_fd) {
char *proc_cpuinfo = malloc(2048);
if (proc_cpuinfo != NULL) {
if (read(proc_cpuinfo_fd, proc_cpuinfo, 2047) < 0) {
printf("Read failed");
return 1;
}
*(proc_cpuinfo+2048) = '\0';
char *model = strstr(proc_cpuinfo, "model name");
int count = 0;
char *start = model;
if (model != NULL) {
model += 13; /* model name\t : <model> */
start += 13;
while (*model != '\n') {
count++;
model++;
}
}
if (write(STDOUT_FILENO, start, count) < 0) {
perror("stdout");
return 1;
}
printf("\n");
free(proc_cpuinfo);
return 0;
}
} else {
perror("file");
return -1;
}
}
Could someone compile this and see if it runs fine on your system?
Should print
thinkpad :: ~ % gcc -g -Wall -Wextra test.c -o test
thinkpad :: ~ % ./test
Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz