wok-current view coreutils/stuff/uname.u @ rev 15918

Up: slitaz-boot-scripts (5.4.1) Bug fix
author Christophe Lincoln <pankso@slitaz.org>
date Sun Feb 16 19:43:02 2014 +0100 (2014-02-16)
parents
children
line source
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
14 @@ -50,6 +50,11 @@
15 # include <mach-o/arch.h>
16 #endif
18 +#if defined(__linux__)
19 +# define USE_PROCINFO
20 +# define UNAME_HARDWARE_PLATFORM
21 +#endif
22 +
23 #include "system.h"
24 #include "error.h"
25 #include "quote.h"
26 @@ -138,6 +143,117 @@
27 exit (status);
28 }
30 +#if defined(USE_PROCINFO)
31 +
32 +# if defined(__s390__) || defined(__s390x__)
33 +# define CPUINFO_FILE "/proc/sysinfo"
34 +# define CPUINFO_FORMAT "%64[^\t :]%*[ :]%256[^\n]%c"
35 +# else
36 +# define CPUINFO_FILE "/proc/cpuinfo"
37 +# define CPUINFO_FORMAT "%64[^\t:]\t:%256[^\n]%c"
38 +# endif
39 +
40 +# define PROCINFO_PROCESSOR 0
41 +# define PROCINFO_HARDWARE_PLATFORM 1
42 +
43 +static void __eat_cpuinfo_space(char *buf)
44 +{
45 + /* first eat trailing space */
46 + char *tmp = buf + strlen(buf) - 1;
47 + while (tmp > buf && isspace(*tmp))
48 + *tmp-- = '\0';
49 + /* then eat leading space */
50 + tmp = buf;
51 + while (*tmp && isspace(*tmp))
52 + tmp++;
53 + if (tmp != buf)
54 + memmove(buf, tmp, strlen(tmp)+1);
55 + /* finally collapse whitespace */
56 + tmp = buf;
57 + while (tmp[0] && tmp[1]) {
58 + if (isspace(tmp[0]) && isspace(tmp[1])) {
59 + memmove(tmp, tmp+1, strlen(tmp));
60 + continue;
61 + }
62 + ++tmp;
63 + }
64 +}
65 +
66 +static int __linux_procinfo(int x, char *fstr, size_t s)
67 +{
68 + FILE *fp;
69 +
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__)
79 + "CPU", "BOARD Name"
80 + #elif defined(__cris__)
81 + "cpu", "cpu model"
82 + #elif defined(__frv__)
83 + "CPU-Core", "System"
84 + #elif defined(__i386__) || defined(__x86_64__)
85 + "model name", "vendor_id"
86 + #elif defined(__ia64__)
87 + "family", "vendor"
88 + #elif defined(__hppa__)
89 + "cpu", "model"
90 + #elif defined(__m68k__)
91 + "CPU", "MMU"
92 + #elif defined(__mips__)
93 + "cpu model", "system type"
94 + #elif defined(__powerpc__) || defined(__powerpc64__)
95 + "cpu", "machine"
96 + #elif defined(__s390__) || defined(__s390x__)
97 + "Type", "Manufacturer"
98 + #elif defined(__sh__)
99 + "cpu type", "machine"
100 + #elif defined(sparc) || defined(__sparc__)
101 + "type", "cpu"
102 + #elif defined(__vax__)
103 + "cpu type", "cpu"
104 + #else
105 + "unknown", "unknown"
106 + #endif
107 + };
108 +
109 + if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
110 + char key[65], value[257], eol, *ret = NULL;
111 +
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);
116 + ret = value;
117 + break;
118 + }
119 + if (eol != '\n') {
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
123 + */
124 + fscanf(fp, "%*[^\n]");
125 + fscanf(fp, "\n");
126 + }
127 + }
128 + fclose(fp);
129 +
130 + if (ret) {
131 + strncpy(fstr, ret, s);
132 + return 0;
133 + }
134 + }
135 +
136 + return -1;
137 +}
138 +
139 +#endif
140 +
141 /* Print ELEMENT, preceded by a space if something has already been
142 printed. */
144 @@ -250,10 +344,14 @@ main (int argc, char **argv)
145 if (toprint & PRINT_PROCESSOR)
146 {
147 char const *element = unknown;
148 -#if HAVE_SYSINFO && defined SI_ARCHITECTURE
149 +#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
150 {
151 static char processor[257];
152 +#if defined(USE_PROCINFO)
153 + if (0 <= __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof processor))
154 +#else
155 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
156 +#endif
157 element = processor;
158 }
159 #endif
160 @@ -306,9 +404,13 @@ main (int argc, char **argv)
161 if (element == unknown)
162 {
163 static char hardware_platform[257];
164 +#if defined(USE_PROCINFO)
165 + if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform, sizeof hardware_platform))
166 +#else
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)
170 +#endif
171 element = hardware_platform;
172 }
173 #endif