wok annotate coreutils/stuff/uname.u @ rev 23365

updated perl-mail-sendmail (0.79 -> 0.80)
author Hans-G?nter Theisgen
date Tue Mar 31 11:21:06 2020 +0100 (2020-03-31)
parents
children
rev   line source
slaxemulator@7775 1 On linux platforms, grok /proc/cpuinfo for the CPU/vendor info.
slaxemulator@7775 2
slaxemulator@7775 3 Prob not suitable for upstream seeing as how it's 100% linux-specific
slaxemulator@7775 4 http://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
slaxemulator@7775 5
slaxemulator@7775 6 Patch originally by Carlos E. Gorges <carlos@techlinux.com.br>, but
slaxemulator@7775 7 heavily reworked to suck less.
slaxemulator@7775 8
slaxemulator@7775 9 To add support for additional platforms, check out the show_cpuinfo()
slaxemulator@7775 10 func in the linux/arch/<ARCH>/ source tree of the kernel.
slaxemulator@7775 11
slaxemulator@7775 12 --- coreutils/src/uname.c
slaxemulator@7775 13 +++ coreutils/src/uname.c
slaxemulator@7775 14 @@ -50,6 +50,11 @@
slaxemulator@7775 15 # include <mach-o/arch.h>
slaxemulator@7775 16 #endif
slaxemulator@7775 17
slaxemulator@7775 18 +#if defined(__linux__)
slaxemulator@7775 19 +# define USE_PROCINFO
slaxemulator@7775 20 +# define UNAME_HARDWARE_PLATFORM
slaxemulator@7775 21 +#endif
slaxemulator@7775 22 +
slaxemulator@7775 23 #include "system.h"
slaxemulator@7775 24 #include "error.h"
slaxemulator@7775 25 #include "quote.h"
slaxemulator@7775 26 @@ -138,6 +143,117 @@
slaxemulator@7775 27 exit (status);
slaxemulator@7775 28 }
slaxemulator@7775 29
slaxemulator@7775 30 +#if defined(USE_PROCINFO)
slaxemulator@7775 31 +
slaxemulator@7775 32 +# if defined(__s390__) || defined(__s390x__)
slaxemulator@7775 33 +# define CPUINFO_FILE "/proc/sysinfo"
slaxemulator@7775 34 +# define CPUINFO_FORMAT "%64[^\t :]%*[ :]%256[^\n]%c"
slaxemulator@7775 35 +# else
slaxemulator@7775 36 +# define CPUINFO_FILE "/proc/cpuinfo"
slaxemulator@7775 37 +# define CPUINFO_FORMAT "%64[^\t:]\t:%256[^\n]%c"
slaxemulator@7775 38 +# endif
slaxemulator@7775 39 +
slaxemulator@7775 40 +# define PROCINFO_PROCESSOR 0
slaxemulator@7775 41 +# define PROCINFO_HARDWARE_PLATFORM 1
slaxemulator@7775 42 +
slaxemulator@7775 43 +static void __eat_cpuinfo_space(char *buf)
slaxemulator@7775 44 +{
slaxemulator@7775 45 + /* first eat trailing space */
slaxemulator@7775 46 + char *tmp = buf + strlen(buf) - 1;
slaxemulator@7775 47 + while (tmp > buf && isspace(*tmp))
slaxemulator@7775 48 + *tmp-- = '\0';
slaxemulator@7775 49 + /* then eat leading space */
slaxemulator@7775 50 + tmp = buf;
slaxemulator@7775 51 + while (*tmp && isspace(*tmp))
slaxemulator@7775 52 + tmp++;
slaxemulator@7775 53 + if (tmp != buf)
slaxemulator@7775 54 + memmove(buf, tmp, strlen(tmp)+1);
slaxemulator@7775 55 + /* finally collapse whitespace */
slaxemulator@7775 56 + tmp = buf;
slaxemulator@7775 57 + while (tmp[0] && tmp[1]) {
slaxemulator@7775 58 + if (isspace(tmp[0]) && isspace(tmp[1])) {
slaxemulator@7775 59 + memmove(tmp, tmp+1, strlen(tmp));
slaxemulator@7775 60 + continue;
slaxemulator@7775 61 + }
slaxemulator@7775 62 + ++tmp;
slaxemulator@7775 63 + }
slaxemulator@7775 64 +}
slaxemulator@7775 65 +
slaxemulator@7775 66 +static int __linux_procinfo(int x, char *fstr, size_t s)
slaxemulator@7775 67 +{
slaxemulator@7775 68 + FILE *fp;
slaxemulator@7775 69 +
slaxemulator@7775 70 + char *procinfo_keys[] = {
slaxemulator@7775 71 + /* --processor --hardware-platform */
slaxemulator@7775 72 + #if defined(__alpha__)
slaxemulator@7775 73 + "cpu model", "system type"
slaxemulator@7775 74 + #elif defined(__arm__)
slaxemulator@7775 75 + "Processor", "Hardware"
slaxemulator@7775 76 + #elif defined(__avr32__)
slaxemulator@7775 77 + "processor", "cpu family"
slaxemulator@7775 78 + #elif defined(__bfin__)
slaxemulator@7775 79 + "CPU", "BOARD Name"
slaxemulator@7775 80 + #elif defined(__cris__)
slaxemulator@7775 81 + "cpu", "cpu model"
slaxemulator@7775 82 + #elif defined(__frv__)
slaxemulator@7775 83 + "CPU-Core", "System"
slaxemulator@7775 84 + #elif defined(__i386__) || defined(__x86_64__)
slaxemulator@7775 85 + "model name", "vendor_id"
slaxemulator@7775 86 + #elif defined(__ia64__)
slaxemulator@7775 87 + "family", "vendor"
slaxemulator@7775 88 + #elif defined(__hppa__)
slaxemulator@7775 89 + "cpu", "model"
slaxemulator@7775 90 + #elif defined(__m68k__)
slaxemulator@7775 91 + "CPU", "MMU"
slaxemulator@7775 92 + #elif defined(__mips__)
slaxemulator@7775 93 + "cpu model", "system type"
slaxemulator@7775 94 + #elif defined(__powerpc__) || defined(__powerpc64__)
slaxemulator@7775 95 + "cpu", "machine"
slaxemulator@7775 96 + #elif defined(__s390__) || defined(__s390x__)
slaxemulator@7775 97 + "Type", "Manufacturer"
slaxemulator@7775 98 + #elif defined(__sh__)
slaxemulator@7775 99 + "cpu type", "machine"
slaxemulator@7775 100 + #elif defined(sparc) || defined(__sparc__)
slaxemulator@7775 101 + "type", "cpu"
slaxemulator@7775 102 + #elif defined(__vax__)
slaxemulator@7775 103 + "cpu type", "cpu"
slaxemulator@7775 104 + #else
slaxemulator@7775 105 + "unknown", "unknown"
slaxemulator@7775 106 + #endif
slaxemulator@7775 107 + };
slaxemulator@7775 108 +
slaxemulator@7775 109 + if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
slaxemulator@7775 110 + char key[65], value[257], eol, *ret = NULL;
slaxemulator@7775 111 +
slaxemulator@7775 112 + while (fscanf(fp, CPUINFO_FORMAT, key, value, &eol) != EOF) {
slaxemulator@7775 113 + __eat_cpuinfo_space(key);
slaxemulator@7775 114 + if (!strcmp(key, procinfo_keys[x])) {
slaxemulator@7775 115 + __eat_cpuinfo_space(value);
slaxemulator@7775 116 + ret = value;
slaxemulator@7775 117 + break;
slaxemulator@7775 118 + }
slaxemulator@7775 119 + if (eol != '\n') {
slaxemulator@7775 120 + /* we need two fscanf's here in case the previous
slaxemulator@7775 121 + * length limit caused us to read right up to the
slaxemulator@7775 122 + * newline ... doing "%*[^\n]\n" wont eat the newline
slaxemulator@7775 123 + */
slaxemulator@7775 124 + fscanf(fp, "%*[^\n]");
slaxemulator@7775 125 + fscanf(fp, "\n");
slaxemulator@7775 126 + }
slaxemulator@7775 127 + }
slaxemulator@7775 128 + fclose(fp);
slaxemulator@7775 129 +
slaxemulator@7775 130 + if (ret) {
slaxemulator@7775 131 + strncpy(fstr, ret, s);
slaxemulator@7775 132 + return 0;
slaxemulator@7775 133 + }
slaxemulator@7775 134 + }
slaxemulator@7775 135 +
slaxemulator@7775 136 + return -1;
slaxemulator@7775 137 +}
slaxemulator@7775 138 +
slaxemulator@7775 139 +#endif
slaxemulator@7775 140 +
slaxemulator@7775 141 /* Print ELEMENT, preceded by a space if something has already been
slaxemulator@7775 142 printed. */
slaxemulator@7775 143
slaxemulator@7775 144 @@ -250,10 +344,14 @@ main (int argc, char **argv)
slaxemulator@7775 145 if (toprint & PRINT_PROCESSOR)
slaxemulator@7775 146 {
slaxemulator@7775 147 char const *element = unknown;
slaxemulator@7775 148 -#if HAVE_SYSINFO && defined SI_ARCHITECTURE
slaxemulator@7775 149 +#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
slaxemulator@7775 150 {
slaxemulator@7775 151 static char processor[257];
slaxemulator@7775 152 +#if defined(USE_PROCINFO)
slaxemulator@7775 153 + if (0 <= __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof processor))
slaxemulator@7775 154 +#else
slaxemulator@7775 155 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
slaxemulator@7775 156 +#endif
slaxemulator@7775 157 element = processor;
slaxemulator@7775 158 }
slaxemulator@7775 159 #endif
slaxemulator@7775 160 @@ -306,9 +404,13 @@ main (int argc, char **argv)
slaxemulator@7775 161 if (element == unknown)
slaxemulator@7775 162 {
slaxemulator@7775 163 static char hardware_platform[257];
slaxemulator@7775 164 +#if defined(USE_PROCINFO)
slaxemulator@7775 165 + if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform, sizeof hardware_platform))
slaxemulator@7775 166 +#else
slaxemulator@7775 167 size_t s = sizeof hardware_platform;
slaxemulator@7775 168 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
slaxemulator@7775 169 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
slaxemulator@7775 170 +#endif
slaxemulator@7775 171 element = hardware_platform;
slaxemulator@7775 172 }
slaxemulator@7775 173 #endif