On linux platforms, grok /proc/cpuinfo for the CPU/vendor info. Prob not suitable for upstream seeing as how it's 100% linux-specific Patch originally by Carlos E. Gorges , but heavily reworked to suck less. --- coreutils/src/uname.c +++ coreutils/src/uname.c @@ -44,6 +44,11 @@ # include #endif +#if defined (__linux__) +# define USE_PROCINFO +# define UNAME_HARDWARE_PLATFORM +#endif + #include "system.h" #include "error.h" #include "quote.h" @@ -129,6 +134,84 @@ exit (status); } +#if defined(USE_PROCINFO) + +# if defined(__s390__) || defined(__s390x__) +# define CPUINFO_FILE "/proc/sysinfo" +# define CPUINFO_FORMAT "%[^\t :]%*[ :]%[^\n]\n" +# else +# define CPUINFO_FILE "/proc/cpuinfo" +# define CPUINFO_FORMAT "%[^\t:]\t:%[^\n]\n" +# endif + +# define PROCINFO_ARCHITECTURE 0 +# define PROCINFO_HARDWARE_PLATFORM 1 + +static void __eat_trailing_space(char *buf) +{ + char *tmp = buf + strlen(buf) - 1; + while (tmp > buf && isspace(*tmp)) + *tmp-- = '\0'; +} + +static int __linux_procinfo (int x, char *fstr) +{ + FILE *fp; + + char *procinfo_keys[] = { + #if defined(__i386__) || defined(__x86_64__) + "model name", "vendor_id" + #elif defined(__ia64__) + "model", "vendor" + #elif defined(__alpha__) + "cpu model", "system type" + #elif defined(sparc) || defined(__sparc__) + "type", "cpu" + #elif defined(__hppa__) + "cpu", "model" + #elif defined(__mips__) + "cpu model", "system type" + #elif defined(__powerpc__) || defined(__powerpc64__) + "cpu", "machine" + #elif defined(__arm__) + "Processor", "Hardware" + #elif defined(__s390__) || defined(__s390x__) + "Type", "Manufacturer" + #elif defined(__sh__) + "cpu family", "Machine" + #elif defined(__m68k__) + "CPU", "MMU" + #elif defined(__cris__) + "cpu", "cpu model" + #else + "???", "???" + #endif + }; + + if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) { + char key[64], value[257], *ret = NULL; + + while (fscanf(fp, CPUINFO_FORMAT, key, value) != EOF) { + __eat_trailing_space(key); + if (!strcmp(key, procinfo_keys[x])) { + __eat_trailing_space(value); + ret = value + (isblank(*value) ? 1 : 0); + break; + } + } + fclose(fp); + + if (ret) { + strncpy(fstr, ret, 257); + return 0; + } + } + + return -1; +} + +#endif + /* Print ELEMENT, preceded by a space if something has already been printed. */ @@ -243,10 +328,14 @@ if (toprint & PRINT_PROCESSOR) { char const *element = unknown; -#if HAVE_SYSINFO && defined SI_ARCHITECTURE +#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO) { static char processor[257]; +#if defined(USE_PROCINFO) + if (0 <= __linux_procinfo (PROCINFO_ARCHITECTURE, processor)) +#else if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor)) +#endif element = processor; } #endif @@ -278,9 +369,13 @@ if (element == unknown) { static char hardware_platform[257]; +#if defined(USE_PROCINFO) + if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform)) +#else size_t s = sizeof hardware_platform; static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM }; if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0) +#endif element = hardware_platform; } #endif