1 See http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/coreutils/6.10/
3 On linux platforms, grok /proc/cpuinfo for the CPU/vendor info.
5 Prob not suitable for upstream seeing as how it's 100% linux-specific
6 http://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
8 Patch originally by Carlos E. Gorges <carlos@techlinux.com.br>, but
9 heavily reworked to suck less.
11 To add support for additional platforms, check out the show_cpuinfo()
12 func in the linux/arch/<ARCH>/ source tree of the kernel.
14 --- coreutils/src/uname.c
15 +++ coreutils/src/uname.c
17 # include <mach-o/arch.h>
20 +#if defined (__linux__)
21 +# define USE_PROCINFO
22 +# define UNAME_HARDWARE_PLATFORM
32 +#if defined(USE_PROCINFO)
34 +# if defined(__s390__) || defined(__s390x__)
35 +# define CPUINFO_FILE "/proc/sysinfo"
36 +# define CPUINFO_FORMAT "%64[^\t :]%*[ :]%256[^\n]%c"
38 +# define CPUINFO_FILE "/proc/cpuinfo"
39 +# define CPUINFO_FORMAT "%64[^\t:]\t:%256[^\n]%c"
42 +# define PROCINFO_PROCESSOR 0
43 +# define PROCINFO_HARDWARE_PLATFORM 1
45 +static void __eat_cpuinfo_space(char *buf)
47 + /* first eat trailing space */
48 + char *tmp = buf + strlen(buf) - 1;
49 + while (tmp > buf && isspace(*tmp))
51 + /* then eat leading space */
53 + while (*tmp && isspace(*tmp))
56 + memmove(buf, tmp, strlen(tmp)+1);
57 + /* finally collapse whitespace */
59 + while (tmp[0] && tmp[1]) {
60 + if (isspace(tmp[0]) && isspace(tmp[1])) {
61 + memmove(tmp, tmp+1, strlen(tmp));
68 +static int __linux_procinfo (int x, char *fstr, size_t s)
72 + char *procinfo_keys[] = {
73 + /* --processor --hardware-platform */
74 + #if defined(__alpha__)
75 + "cpu model", "system type"
76 + #elif defined(__arm__)
77 + "Processor", "Hardware"
78 + #elif defined(__avr32__)
79 + "processor", "cpu family"
80 + #elif defined(__bfin__)
82 + #elif defined(__cris__)
84 + #elif defined(__frv__)
85 + "CPU-Core", "System"
86 + #elif defined(__i386__) || defined(__x86_64__)
87 + "model name", "vendor_id"
88 + #elif defined(__ia64__)
90 + #elif defined(__hppa__)
92 + #elif defined(__m68k__)
94 + #elif defined(__mips__)
95 + "cpu model", "system type"
96 + #elif defined(__powerpc__) || defined(__powerpc64__)
98 + #elif defined(__s390__) || defined(__s390x__)
99 + "Type", "Manufacturer"
100 + #elif defined(__sh__)
101 + "cpu type", "machine"
102 + #elif defined(sparc) || defined(__sparc__)
104 + #elif defined(__vax__)
107 + "unknown", "unknown"
111 + if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
112 + char key[65], value[257], eol, *ret = NULL;
114 + while (fscanf(fp, CPUINFO_FORMAT, key, value, &eol) != EOF) {
115 + __eat_cpuinfo_space(key);
116 + if (!strcmp(key, procinfo_keys[x])) {
117 + __eat_cpuinfo_space(value);
122 + /* we need two fscanf's here in case the previous
123 + * length limit caused us to read right up to the
124 + * newline ... doing "%*[^\n]\n" wont eat the newline
126 + fscanf(fp, "%*[^\n]");
133 + strncpy(fstr, ret, s);
143 /* Print ELEMENT, preceded by a space if something has already been
146 @@ -250,10 +344,14 @@ main (int argc, char **argv)
147 if (toprint & PRINT_PROCESSOR)
149 char const *element = unknown;
150 -#if HAVE_SYSINFO && defined SI_ARCHITECTURE
151 +#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
153 static char processor[257];
154 +#if defined(USE_PROCINFO)
155 + if (0 <= __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof processor))
157 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
162 @@ -306,9 +404,13 @@ main (int argc, char **argv)
163 if (element == unknown)
165 static char hardware_platform[257];
166 +#if defined(USE_PROCINFO)
167 + if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform, sizeof hardware_platform))
169 size_t s = sizeof hardware_platform;
170 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
171 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
173 element = hardware_platform;