#include #include #include #define BUF_SIZ 1024 /****************** ** removeNewLine ** ******************* ** Removes a trailing newline character if present */ static void removeNewLine(char * s) { if(strlen(s)>0 && s[strlen(s)-1] == '\n') { s[strlen(s)-1] = '\0'; } } /*************** ** runCommand ** **************** ** Run the system command through a pipe ** The pointer result must point to a pre-allocated array of at least BUF_SIZ */ static void runCommand (const char *command, char *result) { FILE * pipe; pipe = popen(command, "r"); if(pipe == NULL) { /* command failed */ result[0] = '\0'; } else { if(NULL == fgets(result, BUF_SIZ, pipe)){ /* command failed */ result[0] = '\0'; } pclose(pipe); } removeNewLine(result); } /******************** ** readProcCpuInfo ** ********************* ** Reads and parses /proc/cpuinfo on a Linux system ** The pointers must point to pre-allocated arrays of at least BUF_SIZ */ static void readProcCpuInfo (char *model, char *cache) { FILE * info; char * cp; int cpus = 0; char * buffer_end; char buffer[BUF_SIZ]; char vendor_id[BUF_SIZ]; char model_name[BUF_SIZ]; char cpu_MHz[BUF_SIZ]; int i; float f; vendor_id[0] = model_name[0] = cpu_MHz[0] = model[0] = cache[0] = '\0'; info = fopen("/proc/cpuinfo", "r"); if(info != NULL) { /* command did not fail */ while(NULL != fgets(buffer, BUF_SIZ, info)){ buffer_end = buffer + strlen(buffer); cp = buffer; if(! strncmp(buffer, "processor", 9)) { cpus++; } else if(! strncmp(buffer, "vendor_id", 9)) { cp+=strlen("vendor_id"); while(cp < buffer_end && ( *cp == ' ' || *cp == ':'|| *cp == '\t')) cp++; if(cp1) { if (cpus==2) { strcpy(model, "Dual"); } else { sprintf(model, "%d CPU", cpus); } } cp = model + strlen(model); if(vendor_id[0] != '\0'){ if(cp != model){ *cp++ = ' '; } strcpy(cp, vendor_id); cp += strlen(vendor_id); } if(model_name[0] != '\0'){ if(cp != model){ *cp++ = ' '; } strcpy(cp, model_name); cp += strlen(model_name); } if(cpu_MHz[0] != '\0'){ if(cp != model){ *cp++ = ' '; } f = atof(cpu_MHz); i = (int)(f+0.5f); sprintf(cpu_MHz, "%dMHz", i); strcpy(cp, cpu_MHz); cp += strlen(cpu_MHz); } fclose(info); } } /************* ** hardware ** ************** ** Runs the system command "uname -s -r" ** Reads /proc/cpuinfo if on a linux system ** Writes output */ #define BUF_SIZ 1024 void hardware(void) { char os[BUF_SIZ]; char model[BUF_SIZ]; char cache[BUF_SIZ]; char os_command[] = "uname -s -r"; #ifdef NO_UNAME os[0] = '\0'; #else runCommand(os_command, os); #endif if(NULL != strstr(os, "Linux")) { readProcCpuInfo (model, cache); } else { model[0] = '\0'; cache[0] = '\0'; } printf("CPU : %s\n", model); printf("L2 Cache : %s\n", cache); printf("OS : %s\n", os); } /************************ ** main for hardware.c ** ************************* ** For testing of code only ** Should be commented out */ /* int main(int argc, char * argv[]) { hardware(0, NULL); return 0; } */