mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.2 KiB

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