1 # http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/coreutils/7.5/
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 diff -Nru coreutils-7.6.orig/src/uname.c coreutils-7.6/src/uname.c
15 --- coreutils-7.6.orig/src/uname.c 2009-09-11 16:34:48.000000000 +0200
16 +++ coreutils-7.6/src/uname.c 2009-09-11 16:40:29.000000000 +0200
18 # include <mach-o/arch.h>
21 +#if defined(__linux__)
22 +# define USE_PROCINFO
23 +# define UNAME_HARDWARE_PLATFORM
33 +#if defined(USE_PROCINFO)
35 +# if defined(__s390__) || defined(__s390x__)
36 +# define CPUINFO_FILE "/proc/sysinfo"
37 +# define CPUINFO_FORMAT "%64[^\t :]%*[ :]%256[^\n]%c"
39 +# define CPUINFO_FILE "/proc/cpuinfo"
40 +# define CPUINFO_FORMAT "%64[^\t:]\t:%256[^\n]%c"
43 +# define PROCINFO_PROCESSOR 0
44 +# define PROCINFO_HARDWARE_PLATFORM 1
46 +static void __eat_cpuinfo_space(char *buf)
48 + /* first eat trailing space */
49 + char *tmp = buf + strlen(buf) - 1;
50 + while (tmp > buf && isspace(*tmp))
52 + /* then eat leading space */
54 + while (*tmp && isspace(*tmp))
57 + memmove(buf, tmp, strlen(tmp)+1);
58 + /* finally collapse whitespace */
60 + while (tmp[0] && tmp[1]) {
61 + if (isspace(tmp[0]) && isspace(tmp[1])) {
62 + memmove(tmp, tmp+1, strlen(tmp));
69 +static int __linux_procinfo(int x, char *fstr, size_t s)
73 + char *procinfo_keys[] = {
74 + /* --processor --hardware-platform */
75 + #if defined(__alpha__)
76 + "cpu model", "system type"
77 + #elif defined(__arm__)
78 + "Processor", "Hardware"
79 + #elif defined(__avr32__)
80 + "processor", "cpu family"
81 + #elif defined(__bfin__)
83 + #elif defined(__cris__)
85 + #elif defined(__frv__)
86 + "CPU-Core", "System"
87 + #elif defined(__i386__) || defined(__x86_64__)
88 + "model name", "vendor_id"
89 + #elif defined(__ia64__)
91 + #elif defined(__hppa__)
93 + #elif defined(__m68k__)
95 + #elif defined(__mips__)
96 + "cpu model", "system type"
97 + #elif defined(__powerpc__) || defined(__powerpc64__)
99 + #elif defined(__s390__) || defined(__s390x__)
100 + "Type", "Manufacturer"
101 + #elif defined(__sh__)
102 + "cpu type", "machine"
103 + #elif defined(sparc) || defined(__sparc__)
105 + #elif defined(__vax__)
108 + "unknown", "unknown"
112 + if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
113 + char key[65], value[257], eol, *ret = NULL;
115 + while (fscanf(fp, CPUINFO_FORMAT, key, value, &eol) != EOF) {
116 + __eat_cpuinfo_space(key);
117 + if (!strcmp(key, procinfo_keys[x])) {
118 + __eat_cpuinfo_space(value);
123 + /* we need two fscanf's here in case the previous
124 + * length limit caused us to read right up to the
125 + * newline ... doing "%*[^\n]\n" wont eat the newline
127 + fscanf(fp, "%*[^\n]");
134 + strncpy(fstr, ret, s);
144 /* Print ELEMENT, preceded by a space if something has already been
147 @@ -302,10 +418,14 @@
148 if (toprint & PRINT_PROCESSOR)
150 char const *element = unknown;
151 -#if HAVE_SYSINFO && defined SI_ARCHITECTURE
152 +#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
154 static char processor[257];
155 +#if defined(USE_PROCINFO)
156 + if (0 <= __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof processor))
158 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
164 if (element == unknown)
166 static char hardware_platform[257];
167 +#if defined(USE_PROCINFO)
168 + if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform, sizeof hardware_platform))
170 size_t s = sizeof hardware_platform;
171 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
172 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
174 element = hardware_platform;