--- uname.c Fri Mar 23 06:00:12 2001 +++ uname.c Fri Mar 23 06:11:19 2001 @@ -190,7 +190,16 @@ if (sysinfo (SI_ARCHITECTURE, processor, sizeof (processor)) == -1) error (1, errno, _("cannot get processor type")); #else - strcpy (processor, "unknown"); + /* 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 #if defined(__sparc__) && defined(__linux__) @@ -221,3 +230,115 @@ printf ("%s%c", element, toprint ? ' ' : '\n'); } } + + /* 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", + "cpu model", + "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; + } +