wok diff coreutils/stuff/uname.u @ rev 14411

new: python-ecore-dev
author Dominique Corbex <domcox@slitaz.org>
date Tue Apr 23 18:30:12 2013 +0200 (2013-04-23)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/coreutils/stuff/uname.u	Tue Apr 23 18:30:12 2013 +0200
     1.3 @@ -0,0 +1,173 @@
     1.4 +On linux platforms, grok /proc/cpuinfo for the CPU/vendor info.
     1.5 +
     1.6 +Prob not suitable for upstream seeing as how it's 100% linux-specific
     1.7 +http://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
     1.8 +
     1.9 +Patch originally by Carlos E. Gorges <carlos@techlinux.com.br>, but 
    1.10 +heavily reworked to suck less.
    1.11 +
    1.12 +To add support for additional platforms, check out the show_cpuinfo()
    1.13 +func in the linux/arch/<ARCH>/ source tree of the kernel.
    1.14 +
    1.15 +--- coreutils/src/uname.c
    1.16 ++++ coreutils/src/uname.c
    1.17 +@@ -50,6 +50,11 @@
    1.18 + # include <mach-o/arch.h>
    1.19 + #endif
    1.20 + 
    1.21 ++#if defined(__linux__)
    1.22 ++# define USE_PROCINFO
    1.23 ++# define UNAME_HARDWARE_PLATFORM
    1.24 ++#endif
    1.25 ++
    1.26 + #include "system.h"
    1.27 + #include "error.h"
    1.28 + #include "quote.h"
    1.29 +@@ -138,6 +143,117 @@
    1.30 +   exit (status);
    1.31 + }
    1.32 + 
    1.33 ++#if defined(USE_PROCINFO)
    1.34 ++
    1.35 ++# if defined(__s390__) || defined(__s390x__)
    1.36 ++#  define CPUINFO_FILE    "/proc/sysinfo"
    1.37 ++#  define CPUINFO_FORMAT  "%64[^\t :]%*[ :]%256[^\n]%c"
    1.38 ++# else
    1.39 ++#  define CPUINFO_FILE    "/proc/cpuinfo"
    1.40 ++#  define CPUINFO_FORMAT  "%64[^\t:]\t:%256[^\n]%c"
    1.41 ++# endif
    1.42 ++
    1.43 ++# define PROCINFO_PROCESSOR      0
    1.44 ++# define PROCINFO_HARDWARE_PLATFORM 1
    1.45 ++
    1.46 ++static void __eat_cpuinfo_space(char *buf)
    1.47 ++{
    1.48 ++	/* first eat trailing space */
    1.49 ++	char *tmp = buf + strlen(buf) - 1;
    1.50 ++	while (tmp > buf && isspace(*tmp))
    1.51 ++		*tmp-- = '\0';
    1.52 ++	/* then eat leading space */
    1.53 ++	tmp = buf;
    1.54 ++	while (*tmp && isspace(*tmp))
    1.55 ++		tmp++;
    1.56 ++	if (tmp != buf)
    1.57 ++		memmove(buf, tmp, strlen(tmp)+1);
    1.58 ++	/* finally collapse whitespace */
    1.59 ++	tmp = buf;
    1.60 ++	while (tmp[0] && tmp[1]) {
    1.61 ++		if (isspace(tmp[0]) && isspace(tmp[1])) {
    1.62 ++			memmove(tmp, tmp+1, strlen(tmp));
    1.63 ++			continue;
    1.64 ++		}
    1.65 ++		++tmp;
    1.66 ++	}
    1.67 ++}
    1.68 ++
    1.69 ++static int __linux_procinfo(int x, char *fstr, size_t s)
    1.70 ++{
    1.71 ++	FILE *fp;
    1.72 ++
    1.73 ++	char *procinfo_keys[] = {
    1.74 ++		/* --processor --hardware-platform */
    1.75 ++		#if defined(__alpha__)
    1.76 ++			"cpu model", "system type"
    1.77 ++		#elif defined(__arm__)
    1.78 ++			"Processor", "Hardware"
    1.79 ++		#elif defined(__avr32__)
    1.80 ++			"processor", "cpu family"
    1.81 ++		#elif defined(__bfin__)
    1.82 ++			"CPU", "BOARD Name"
    1.83 ++		#elif defined(__cris__)
    1.84 ++			"cpu", "cpu model"
    1.85 ++		#elif defined(__frv__)
    1.86 ++			"CPU-Core", "System"
    1.87 ++		#elif defined(__i386__) || defined(__x86_64__)
    1.88 ++			"model name", "vendor_id"
    1.89 ++		#elif defined(__ia64__)
    1.90 ++			"family", "vendor"
    1.91 ++		#elif defined(__hppa__)
    1.92 ++			"cpu", "model"
    1.93 ++		#elif defined(__m68k__)
    1.94 ++			"CPU", "MMU"
    1.95 ++		#elif defined(__mips__)
    1.96 ++			"cpu model", "system type"
    1.97 ++		#elif defined(__powerpc__) || defined(__powerpc64__)
    1.98 ++			"cpu", "machine"
    1.99 ++		#elif defined(__s390__) || defined(__s390x__)
   1.100 ++			"Type", "Manufacturer"
   1.101 ++		#elif defined(__sh__)
   1.102 ++			"cpu type", "machine"
   1.103 ++		#elif defined(sparc) || defined(__sparc__)
   1.104 ++			"type", "cpu"
   1.105 ++		#elif defined(__vax__)
   1.106 ++			"cpu type", "cpu"
   1.107 ++		#else
   1.108 ++			"unknown", "unknown"
   1.109 ++		#endif
   1.110 ++	};
   1.111 ++
   1.112 ++	if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
   1.113 ++		char key[65], value[257], eol, *ret = NULL;
   1.114 ++
   1.115 ++		while (fscanf(fp, CPUINFO_FORMAT, key, value, &eol) != EOF) {
   1.116 ++			__eat_cpuinfo_space(key);
   1.117 ++			if (!strcmp(key, procinfo_keys[x])) {
   1.118 ++				__eat_cpuinfo_space(value);
   1.119 ++				ret = value;
   1.120 ++				break;
   1.121 ++			}
   1.122 ++			if (eol != '\n') {
   1.123 ++				/* we need two fscanf's here in case the previous
   1.124 ++				 * length limit caused us to read right up to the
   1.125 ++				 * newline ... doing "%*[^\n]\n" wont eat the newline
   1.126 ++				 */
   1.127 ++				fscanf(fp, "%*[^\n]");
   1.128 ++				fscanf(fp, "\n");
   1.129 ++			}
   1.130 ++		}
   1.131 ++		fclose(fp);
   1.132 ++
   1.133 ++		if (ret) {
   1.134 ++			strncpy(fstr, ret, s);
   1.135 ++			return 0;
   1.136 ++		}
   1.137 ++	}
   1.138 ++
   1.139 ++	return -1;
   1.140 ++}
   1.141 ++
   1.142 ++#endif
   1.143 ++
   1.144 + /* Print ELEMENT, preceded by a space if something has already been
   1.145 +    printed.  */
   1.146 + 
   1.147 +@@ -250,10 +344,14 @@ main (int argc, char **argv)
   1.148 +   if (toprint & PRINT_PROCESSOR)
   1.149 +     {
   1.150 +       char const *element = unknown;
   1.151 +-#if HAVE_SYSINFO && defined SI_ARCHITECTURE
   1.152 ++#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
   1.153 +       {
   1.154 +         static char processor[257];
   1.155 ++#if defined(USE_PROCINFO)
   1.156 ++        if (0 <= __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof processor))
   1.157 ++#else
   1.158 +         if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
   1.159 ++#endif
   1.160 +           element = processor;
   1.161 +       }
   1.162 + #endif
   1.163 +@@ -306,9 +404,13 @@ main (int argc, char **argv)
   1.164 +       if (element == unknown)
   1.165 +         {
   1.166 +           static char hardware_platform[257];
   1.167 ++#if defined(USE_PROCINFO)
   1.168 ++          if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform, sizeof hardware_platform))
   1.169 ++#else
   1.170 +           size_t s = sizeof hardware_platform;
   1.171 +           static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
   1.172 +           if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
   1.173 ++#endif
   1.174 +             element = hardware_platform;
   1.175 +         }
   1.176 + #endif