Though I've noticed it before, I finally decided to "fix" uname to display my
processor type when doing uname -p or uname -a. Currently, uname -p simply returns
"unknown" for all processor types under Linux (tested on kernel 2.2.16), since the
header file sys/systeminfo.h does not exist.
I've attached a patch that gets around this problem. On my x86 machine, I get the
following output:
ostermer@saidin:~ > src/uname -p
Pentium MMX
and the same code built on my ppc machine returns
ostermer@saidar:~$ ./uname -p
601
The patch essentially pulls either the "model name" or "cpu\t\t:" from Linux's
/proc/cpuinfo. The reason for needing both of those is that my ppc machine (running
kernel 2.2.16) does not have a "model name" field in /proc/cpuinfo, so I pulled the
next best field instead. If neither field exists, it just returns the old "unknown".
I've only tested this on x86 and ppc machines, so I'm not sure how it works on Sparcs,
MIPS, Alpha, and others. Assuming that /proc/cpuinfo contains one of the two fields
the patch looks for on those machines, then it'll work.
Thanks
--
Todd Ostermeier
[EMAIL PROTECTED] - school
[EMAIL PROTECTED] - personal
--
"I used to get high on life but lately I've built up a resistance."
--- uname.c Fri Oct 13 02:04:59 2000
+++ uname.c Fri Oct 13 02:06:05 2000
@@ -112,13 +112,66 @@
exit (status);
}
+/* Hack to get processor model name from /proc/cpuinfo on linux machines */
+int
+linux_get_processor(char* processor)
+{
+ FILE* fd;
+ char buff[256];
+ char* buffwalk;
+
+ fd = fopen("/proc/cpuinfo", "r");
+
+ if (!fd)
+ return 0;
+
+ while (!feof(fd))
+ {
+ if (!fgets(buff, 256, fd))
+ return 0;
+
+ /* check for the line beginning with "model name" or "cpu\t\t:"
+ * "model name" is sufficient for x86 machines, and "cpu\t\t:"
+ * works for ppc machines. Other architectures haven't been
+ * tested
+ */
+ if (!strncmp(buff, "model name", strlen("model name"))
+ || !strncmp(buff, "cpu\t\t:", strlen("cpu\t\t:")))
+ {
+ buffwalk = buff;
+ while (*buffwalk != ':' && *buffwalk != '\0')
+ buffwalk++;
+
+ if (*buffwalk == '\0')
+ return 0;
+
+ /* get past the ':' and the following space */
+ buffwalk += 2;
+
+ /* now copy the resulting string into *processor */
+ strncpy(processor, buffwalk, strlen(buffwalk) + 1);
+
+ buffwalk = processor;
+ while (*buffwalk != '\n' && *buffwalk != '\0')
+ buffwalk++;
+
+ *buffwalk = '\0';
+
+ return 1;
+ } /* if (!strncmp ...) */
+ } /* while (!feof(fd)) */
+
+ return 0;
+}
+
int
main (int argc, char **argv)
{
struct utsname name;
int c;
char processor[256];
-
+
+
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
@@ -184,7 +237,16 @@
if (sysinfo (SI_ARCHITECTURE, processor, sizeof (processor)) == -1)
error (1, errno, _("cannot get processor type"));
#else
- strcpy (processor, "unknown");
+ /* If this is a linux machine and does not have SI_ARCHITECTURE, then try
+ * to get processor from /proc/cpuinfo
+ */
+ if (!strncmp(name.sysname, "Linux", strlen("Linux")))
+ {
+ if (!linux_get_processor(processor))
+ strcpy(processor, "unknown");
+ }
+ else
+ strcpy (processor, "unknown");
#endif
print_element (PRINT_SYSNAME, name.sysname);