--- uname.c Sun Mar 3 03:59:49 2002 +++ uname.c Sun Mar 3 04:00:58 2002 @@ -112,6 +112,119 @@ exit (status); } +/* Hack to get processor model name from /proc/cpuinfo on linux machines */ + +/* szChoices holds the list of strings you want to look for. */ +char* szChoices[] = +{ + "model name", + "platform string", + "cpu\t\t:" +}; +int nChoices = 3; + + +int +linux_get_processor(char* processor) +{ + FILE* fd; + char buff[256]; + char* buffwalk; + int i = 0; + char** szStrings = NULL; + + szStrings = (char**)(malloc(nChoices)); + + if (szStrings == NULL) + return 0; + + for (i = 0; i < nChoices; i++) + szStrings[i] = NULL; + + fd = fopen("/proc/cpuinfo", "r"); + + if (!fd) + goto LocalError; + + while (!feof(fd)) + { + if (!fgets(buff, 256, fd)) + break; + + for (i = 0; i < nChoices; i++) + { + if (!strncmp(buff, szChoices[i], strlen(szChoices[i])) + && szStrings[i] == NULL) + { + szStrings[i] = (char*)(malloc(strlen(buff) + 1)); + + if (szStrings[i] == NULL) + { + fclose(fd); + goto LocalError; + } + + strncpy(szStrings[i], buff, strlen(buff)); + } + } + } + + for(i = 0; i < nChoices; i++) + if (szStrings[i] != NULL) + break; + + if (i == nChoices) + { + fclose(fd); + goto LocalError; + } + + buffwalk = szStrings[i]; + while (*buffwalk != ':' && *buffwalk != '\0') + buffwalk++; + + if (*buffwalk == '\0') + { + fclose(fd); + goto LocalError; + } + + /* get past the ':' and the following space */ + buffwalk += 2; + + /* now copy the resulting string into *processor */ + strncpy(processor, buffwalk, strlen(buffwalk) + 1); + + buffwalk = processor; + while (*buffwalk != '\n' && *buffwalk != '\0') + buffwalk++; + + *buffwalk = '\0'; + + /* sanity check */ + buffwalk = NULL; + + for (i = 0; i < nChoices; i++) + { + if (szStrings[i]) + free(szStrings[i]); + } + free(szStrings); + + fclose(fd); + return 1; + +LocalError: + for (i = 0; i < nChoices; i++) + { + if (szStrings[i]) + free(szStrings[i]); + } + free(szStrings); + + return 0; +} + int main (int argc, char **argv) { @@ -184,6 +297,15 @@ if (sysinfo (SI_ARCHITECTURE, processor, sizeof (processor)) == -1) error (1, errno, _("cannot get processor type")); #else + /* If this is a linux machine and does not have SI_ARCHITECTURE, then try + * to get processor from /proc/cpuinfo + */ + if (!strncmp(name.sysname, "Linux", strlen("Linux"))) + { + if (!linux_get_processor(processor)) + strcpy(processor, "unknown"); + } + else strcpy (processor, "unknown"); #endif