summaryrefslogtreecommitdiff
path: root/coreutils/coreutils-uname.patch
blob: 37f4729619a7acda97634dc77b51f320720fee1a (plain)
    1 # http://sources.gentoo.org/viewcvs.py/gentoo/src/patchsets/coreutils/7.5/
    2 
    3 On linux platforms, grok /proc/cpuinfo for the CPU/vendor info.
    4 
    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
    7 
    8 Patch originally by Carlos E. Gorges <carlos@techlinux.com.br>, but 
    9 heavily reworked to suck less.
   10 
   11 To add support for additional platforms, check out the show_cpuinfo()
   12 func in the linux/arch/<ARCH>/ source tree of the kernel.
   13 
   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
   17 @@ -50,6 +50,11 @@
   18  # include <mach-o/arch.h>
   19  #endif
   20  
   21 +#if defined(__linux__)
   22 +# define USE_PROCINFO
   23 +# define UNAME_HARDWARE_PLATFORM
   24 +#endif
   25 +
   26  #include "system.h"
   27  #include "error.h"
   28  #include "quote.h"
   29 @@ -155,6 +160,117 @@
   30    exit (status);
   31  }
   32  
   33 +#if defined(USE_PROCINFO)
   34 +
   35 +# if defined(__s390__) || defined(__s390x__)
   36 +#  define CPUINFO_FILE    "/proc/sysinfo"
   37 +#  define CPUINFO_FORMAT  "%64[^\t :]%*[ :]%256[^\n]%c"
   38 +# else
   39 +#  define CPUINFO_FILE    "/proc/cpuinfo"
   40 +#  define CPUINFO_FORMAT  "%64[^\t:]\t:%256[^\n]%c"
   41 +# endif
   42 +
   43 +# define PROCINFO_PROCESSOR      0
   44 +# define PROCINFO_HARDWARE_PLATFORM 1
   45 +
   46 +static void __eat_cpuinfo_space(char *buf)
   47 +{
   48 +	/* first eat trailing space */
   49 +	char *tmp = buf + strlen(buf) - 1;
   50 +	while (tmp > buf && isspace(*tmp))
   51 +		*tmp-- = '\0';
   52 +	/* then eat leading space */
   53 +	tmp = buf;
   54 +	while (*tmp && isspace(*tmp))
   55 +		tmp++;
   56 +	if (tmp != buf)
   57 +		memmove(buf, tmp, strlen(tmp)+1);
   58 +	/* finally collapse whitespace */
   59 +	tmp = buf;
   60 +	while (tmp[0] && tmp[1]) {
   61 +		if (isspace(tmp[0]) && isspace(tmp[1])) {
   62 +			memmove(tmp, tmp+1, strlen(tmp));
   63 +			continue;
   64 +		}
   65 +		++tmp;
   66 +	}
   67 +}
   68 +
   69 +static int __linux_procinfo(int x, char *fstr, size_t s)
   70 +{
   71 +	FILE *fp;
   72 +
   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__)
   82 +			"CPU", "BOARD Name"
   83 +		#elif defined(__cris__)
   84 +			"cpu", "cpu model"
   85 +		#elif defined(__frv__)
   86 +			"CPU-Core", "System"
   87 +		#elif defined(__i386__) || defined(__x86_64__)
   88 +			"model name", "vendor_id"
   89 +		#elif defined(__ia64__)
   90 +			"family", "vendor"
   91 +		#elif defined(__hppa__)
   92 +			"cpu", "model"
   93 +		#elif defined(__m68k__)
   94 +			"CPU", "MMU"
   95 +		#elif defined(__mips__)
   96 +			"cpu model", "system type"
   97 +		#elif defined(__powerpc__) || defined(__powerpc64__)
   98 +			"cpu", "machine"
   99 +		#elif defined(__s390__) || defined(__s390x__)
  100 +			"Type", "Manufacturer"
  101 +		#elif defined(__sh__)
  102 +			"cpu type", "machine"
  103 +		#elif defined(sparc) || defined(__sparc__)
  104 +			"type", "cpu"
  105 +		#elif defined(__vax__)
  106 +			"cpu type", "cpu"
  107 +		#else
  108 +			"unknown", "unknown"
  109 +		#endif
  110 +	};
  111 +
  112 +	if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
  113 +		char key[65], value[257], eol, *ret = NULL;
  114 +
  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);
  119 +				ret = value;
  120 +				break;
  121 +			}
  122 +			if (eol != '\n') {
  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
  126 +				 */
  127 +				fscanf(fp, "%*[^\n]");
  128 +				fscanf(fp, "\n");
  129 +			}
  130 +		}
  131 +		fclose(fp);
  132 +
  133 +		if (ret) {
  134 +			strncpy(fstr, ret, s);
  135 +			return 0;
  136 +		}
  137 +	}
  138 +
  139 +	return -1;
  140 +}
  141 +
  142 +#endif
  143 +
  144  /* Print ELEMENT, preceded by a space if something has already been
  145     printed.  */
  146  
  147 @@ -302,10 +418,14 @@
  148    if (toprint & PRINT_PROCESSOR)
  149      {
  150        char const *element = unknown;
  151 -#if HAVE_SYSINFO && defined SI_ARCHITECTURE
  152 +#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
  153        {
  154          static char processor[257];
  155 +#if defined(USE_PROCINFO)
  156 +	if (0 <= __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof processor))
  157 +#else
  158          if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
  159 +#endif
  160            element = processor;
  161        }
  162  #endif
  163 @@ -358,9 +478,13 @@
  164        if (element == unknown)
  165          {
  166            static char hardware_platform[257];
  167 +#if defined(USE_PROCINFO)
  168 +	  if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, hardware_platform, sizeof hardware_platform))
  169 +#else
  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)
  173 +#endif
  174              element = hardware_platform;
  175          }
  176  #endif

Generated by cgit