--- src/uname.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 126 insertions(+), 5 deletions(-) --- coreutils-6.12.orig/src/uname.c +++ coreutils-6.12/src/uname.c @@ -260,10 +260,124 @@ decode_switches (int argc, char **argv) return toprint; } +/* 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 * sizeof(char *)); + + 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) { static char const unknown[] = "unknown"; + struct utsname name; /* Mask indicating which elements to print. */ unsigned int toprint = 0; @@ -283,9 +397,8 @@ main (int argc, char **argv) if (toprint & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE - | PRINT_KERNEL_VERSION | PRINT_MACHINE)) + | PRINT_KERNEL_VERSION | PRINT_MACHINE | PRINT_PROCESSOR)) { - struct utsname name; if (uname (&name) == -1) error (EXIT_FAILURE, errno, _("cannot get system name")); @@ -305,12 +418,20 @@ main (int argc, char **argv) if (toprint & PRINT_PROCESSOR) { char const *element = unknown; + static char processor[257]; #if HAVE_SYSINFO && defined SI_ARCHITECTURE - { - static char processor[257]; if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor)) element = processor; - } + else +#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"); + element = processor; + } #endif #ifdef UNAME_PROCESSOR if (element == unknown)