[67300de] | 1 | Submitted By: Jim Gifford <jim at linuxfromscratch dot org>
|
---|
| 2 | Date: 2006-08-24
|
---|
| 3 | Initial Package Version: 5.97
|
---|
| 4 | Upstream Status: Not Accepted
|
---|
| 5 | Origin: Gentoo - http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/coreutils
|
---|
| 6 | Description: Display CPU Information from /proc/cpuinfo or /proc/sysinfo
|
---|
| 7 |
|
---|
| 8 | Original Patch by - Matthew Burgess and Scot McPherson
|
---|
| 9 |
|
---|
| 10 | diff -Naur coreutils-5.97.orig/src/uname.c coreutils-5.97/src/uname.c
|
---|
| 11 | --- coreutils-5.97.orig/src/uname.c 2005-09-15 12:57:04.000000000 -0700
|
---|
| 12 | +++ coreutils-5.97/src/uname.c 2006-08-24 15:52:53.000000000 -0700
|
---|
| 13 | @@ -51,6 +51,11 @@
|
---|
| 14 | # include <mach-o/arch.h>
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 | +#if defined (__linux__)
|
---|
| 18 | +# define USE_PROCINFO
|
---|
| 19 | +# define UNAME_HARDWARE_PLATFORM
|
---|
| 20 | +#endif
|
---|
| 21 | +
|
---|
| 22 | #include "system.h"
|
---|
| 23 | #include "error.h"
|
---|
| 24 | #include "quote.h"
|
---|
| 25 | @@ -138,6 +143,106 @@
|
---|
| 26 | exit (status);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | +#if defined(USE_PROCINFO)
|
---|
| 30 | +
|
---|
| 31 | +# if defined(__s390__) || defined(__s390x__)
|
---|
| 32 | +# define CPUINFO_FILE "/proc/sysinfo"
|
---|
| 33 | +# define CPUINFO_FORMAT "%64[^\t :]%*[ :]%256[^\n]%c"
|
---|
| 34 | +# else
|
---|
| 35 | +# define CPUINFO_FILE "/proc/cpuinfo"
|
---|
| 36 | +# define CPUINFO_FORMAT "%64[^\t:]\t:%256[^\n]%c"
|
---|
| 37 | +# endif
|
---|
| 38 | +
|
---|
| 39 | +# define PROCINFO_PROCESSOR 0
|
---|
| 40 | +# define PROCINFO_HARDWARE_PLATFORM 1
|
---|
| 41 | +
|
---|
| 42 | +static void __eat_cpuinfo_space(char *buf)
|
---|
| 43 | +{
|
---|
| 44 | + /* first eat trailing space */
|
---|
| 45 | + char *tmp = buf + strlen(buf) - 1;
|
---|
| 46 | + while (tmp > buf && isspace(*tmp))
|
---|
| 47 | + *tmp-- = '\0';
|
---|
| 48 | + /* then eat leading space */
|
---|
| 49 | + tmp = buf;
|
---|
| 50 | + while (*tmp && isspace(*tmp))
|
---|
| 51 | + tmp++;
|
---|
| 52 | + if (tmp != buf)
|
---|
| 53 | + memmove(buf, tmp, strlen(tmp)+1);
|
---|
| 54 | +}
|
---|
| 55 | +
|
---|
| 56 | +static int __linux_procinfo (int x, char *fstr, size_t s)
|
---|
| 57 | +{
|
---|
| 58 | + FILE *fp;
|
---|
| 59 | +
|
---|
| 60 | + char *procinfo_keys[] = {
|
---|
| 61 | + /* --processor --hardware-platform */
|
---|
| 62 | + #if defined(__alpha__)
|
---|
| 63 | + "cpu model", "system type"
|
---|
| 64 | + #elif defined(__arm__)
|
---|
| 65 | + "Processor", "Hardware"
|
---|
| 66 | + #elif defined(bfin)
|
---|
| 67 | + "CPU", "BOARD Name"
|
---|
| 68 | + #elif defined(__cris__)
|
---|
| 69 | + "cpu", "cpu model"
|
---|
| 70 | + #elif defined(__frv__)
|
---|
| 71 | + "CPU-Core", "System"
|
---|
| 72 | + #elif defined(__i386__) || defined(__x86_64__)
|
---|
| 73 | + "model name", "vendor_id"
|
---|
| 74 | + #elif defined(__ia64__)
|
---|
| 75 | + "family", "vendor"
|
---|
| 76 | + #elif defined(__hppa__)
|
---|
| 77 | + "cpu", "model"
|
---|
| 78 | + #elif defined(__m68k__)
|
---|
| 79 | + "CPU", "MMU"
|
---|
| 80 | + #elif defined(__mips__)
|
---|
| 81 | + "cpu model", "system type"
|
---|
| 82 | + #elif defined(__powerpc__) || defined(__powerpc64__)
|
---|
| 83 | + "cpu", "machine"
|
---|
| 84 | + #elif defined(__s390__) || defined(__s390x__)
|
---|
| 85 | + "Type", "Manufacturer"
|
---|
| 86 | + #elif defined(__sh__)
|
---|
| 87 | + "cpu type", "machine"
|
---|
| 88 | + #elif defined(sparc) || defined(__sparc__)
|
---|
| 89 | + "type", "cpu"
|
---|
| 90 | + #elif defined(__vax__)
|
---|
| 91 | + "cpu type", "cpu"
|
---|
| 92 | + #else
|
---|
| 93 | + "unknown", "unknown"
|
---|
| 94 | + #endif
|
---|
| 95 | + };
|
---|
| 96 | +
|
---|
| 97 | + if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
|
---|
| 98 | + char key[65], value[257], eol, *ret = NULL;
|
---|
| 99 | +
|
---|
| 100 | + while (fscanf(fp, CPUINFO_FORMAT, key, value, &eol) != EOF) {
|
---|
| 101 | + __eat_cpuinfo_space(key);
|
---|
| 102 | + if (!strcmp(key, procinfo_keys[x])) {
|
---|
| 103 | + __eat_cpuinfo_space(value);
|
---|
| 104 | + ret = value;
|
---|
| 105 | + break;
|
---|
| 106 | + }
|
---|
| 107 | + if (eol != '\n') {
|
---|
| 108 | + /* we need two fscanf's here in case the previous
|
---|
| 109 | + * length limit caused us to read right up to the
|
---|
| 110 | + * newline ... doing "%*[^\n]\n" wont eat the newline
|
---|
| 111 | + */
|
---|
| 112 | + fscanf(fp, "%*[^\n]");
|
---|
| 113 | + fscanf(fp, "\n");
|
---|
| 114 | + }
|
---|
| 115 | + }
|
---|
| 116 | + fclose(fp);
|
---|
| 117 | +
|
---|
| 118 | + if (ret) {
|
---|
| 119 | + strncpy(fstr, ret, s);
|
---|
| 120 | + return 0;
|
---|
| 121 | + }
|
---|
| 122 | + }
|
---|
| 123 | +
|
---|
| 124 | + return -1;
|
---|
| 125 | +}
|
---|
| 126 | +
|
---|
| 127 | +#endif
|
---|
| 128 | +
|
---|
| 129 | /* Print ELEMENT, preceded by a space if something has already been
|
---|
| 130 | printed. */
|
---|
| 131 |
|
---|
| 132 | @@ -250,10 +355,14 @@
|
---|
| 133 | if (toprint & PRINT_PROCESSOR)
|
---|
| 134 | {
|
---|
| 135 | char const *element = unknown;
|
---|
| 136 | -#if HAVE_SYSINFO && defined SI_ARCHITECTURE
|
---|
| 137 | +#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
|
---|
| 138 | {
|
---|
| 139 | static char processor[257];
|
---|
| 140 | +#if defined(USE_PROCINFO)
|
---|
| 141 | + if (0 <= __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof processor))
|
---|
| 142 | +#else
|
---|
| 143 | if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
|
---|
| 144 | +#endif
|
---|
| 145 | element = processor;
|
---|
| 146 | }
|
---|
| 147 | #endif
|
---|
| 148 | @@ -306,9 +415,13 @@
|
---|
| 149 | if (element == unknown)
|
---|
| 150 | {
|
---|
| 151 | static char hardware_platform[257];
|
---|
| 152 | +#if defined(USE_PROCINFO)
|
---|
| 153 | + if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform, sizeof hardware_platform))
|
---|
| 154 | +#else
|
---|
| 155 | size_t s = sizeof hardware_platform;
|
---|
| 156 | static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
|
---|
| 157 | if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
|
---|
| 158 | +#endif
|
---|
| 159 | element = hardware_platform;
|
---|
| 160 | }
|
---|
| 161 | #endif
|
---|