1 On linux platforms, grok /proc/cpuinfo for the CPU/vendor info.
3 Prob not suitable for upstream seeing as how it's 100% linux-specific
4 http://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
6 Patch originally by Carlos E. Gorges <carlos@techlinux.com.br>, but
7 heavily reworked to suck less.
9 To add support for additional platforms, check out the show_cpuinfo()
10 func in the linux/arch/<ARCH>/ source tree of the kernel.
12 --- coreutils/src/uname.c
13 +++ coreutils/src/uname.c
15 # include <mach-o/arch.h>
18 +#if defined(__linux__)
19 +# define USE_PROCINFO
20 +# define UNAME_HARDWARE_PLATFORM
30 +#if defined(USE_PROCINFO)
32 +# if defined(__s390__) || defined(__s390x__)
33 +# define CPUINFO_FILE "/proc/sysinfo"
34 +# define CPUINFO_FORMAT "%64[^\t :]%*[ :]%256[^\n]%c"
36 +# define CPUINFO_FILE "/proc/cpuinfo"
37 +# define CPUINFO_FORMAT "%64[^\t:]\t:%256[^\n]%c"
40 +# define PROCINFO_PROCESSOR 0
41 +# define PROCINFO_HARDWARE_PLATFORM 1
43 +static void __eat_cpuinfo_space(char *buf)
45 + /* first eat trailing space */
46 + char *tmp = buf + strlen(buf) - 1;
47 + while (tmp > buf && isspace(*tmp))
49 + /* then eat leading space */
51 + while (*tmp && isspace(*tmp))
54 + memmove(buf, tmp, strlen(tmp)+1);
55 + /* finally collapse whitespace */
57 + while (tmp[0] && tmp[1]) {
58 + if (isspace(tmp[0]) && isspace(tmp[1])) {
59 + memmove(tmp, tmp+1, strlen(tmp));
66 +static int __linux_procinfo(int x, char *fstr, size_t s)
70 + char *procinfo_keys[] = {
71 + /* --processor --hardware-platform */
72 + #if defined(__alpha__)
73 + "cpu model", "system type"
74 + #elif defined(__arm__)
75 + "Processor", "Hardware"
76 + #elif defined(__avr32__)
77 + "processor", "cpu family"
78 + #elif defined(__bfin__)
80 + #elif defined(__cris__)
82 + #elif defined(__frv__)
83 + "CPU-Core", "System"
84 + #elif defined(__i386__) || defined(__x86_64__)
85 + "model name", "vendor_id"
86 + #elif defined(__ia64__)
88 + #elif defined(__hppa__)
90 + #elif defined(__m68k__)
92 + #elif defined(__mips__)
93 + "cpu model", "system type"
94 + #elif defined(__powerpc__) || defined(__powerpc64__)
96 + #elif defined(__s390__) || defined(__s390x__)
97 + "Type", "Manufacturer"
98 + #elif defined(__sh__)
99 + "cpu type", "machine"
100 + #elif defined(sparc) || defined(__sparc__)
102 + #elif defined(__vax__)
105 + "unknown", "unknown"
109 + if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
110 + char key[65], value[257], eol, *ret = NULL;
112 + while (fscanf(fp, CPUINFO_FORMAT, key, value, &eol) != EOF) {
113 + __eat_cpuinfo_space(key);
114 + if (!strcmp(key, procinfo_keys[x])) {
115 + __eat_cpuinfo_space(value);
120 + /* we need two fscanf's here in case the previous
121 + * length limit caused us to read right up to the
122 + * newline ... doing "%*[^\n]\n" wont eat the newline
124 + fscanf(fp, "%*[^\n]");
131 + strncpy(fstr, ret, s);
141 /* Print ELEMENT, preceded by a space if something has already been
144 @@ -250,10 +344,14 @@ main (int argc, char **argv)
145 if (toprint & PRINT_PROCESSOR)
147 char const *element = unknown;
148 -#if HAVE_SYSINFO && defined SI_ARCHITECTURE
149 +#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
151 static char processor[257];
152 +#if defined(USE_PROCINFO)
153 + if (0 <= __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof processor))
155 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
160 @@ -306,9 +404,13 @@ main (int argc, char **argv)
161 if (element == unknown)
163 static char hardware_platform[257];
164 +#if defined(USE_PROCINFO)
165 + if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform, sizeof hardware_platform))
167 size_t s = sizeof hardware_platform;
168 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
169 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
171 element = hardware_platform;