File: linuxinfo.c

package info (click to toggle)
linuxinfo 1.1.8-34
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 504 kB
  • ctags: 197
  • sloc: ansic: 1,777; sh: 334; makefile: 29
file content (109 lines) | stat: -rw-r--r-- 2,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*

	linuxinfo.c

	Copyright (C) 1998-2000
	All Rights Reserved.

	Alex Buell <alex.buell@munted.org.uk>

	Version	Author	Date		Comments
 	----------------------------------------------------------------------
	1.0.0	AIB	199803??	Initial development
	1.0.2	AIB	199803??	Renamed from sysinfo to linuxinfo
					to avoid infringement with a
					commercial product
	1.0.5	AIB	199803??	Added new MHz field for 2.2.x kernels
	1.0.6	AIB	19980306	Added option for testing
	1.1.3	AIB	1999????	If MHz field not detected, does not
					print it
	1.1.4	AIB	1999????	Added -v for versioning
	1.1.6	AIB	20000405	Updates & changes to linuxinfo
	1.1.7	AIB	20000406	Changed to file descriptors

	1.1.8   KRE     20051118        Include <string.h>
        1.1.9   KRE     20060730        Support more than 9 CPUs and bail out
                                        if /proc/cpuinfo is unreadable

	Modelled on Vince Weaver's Linux_logo 2.10
	
	Prints out a line of information about your system.
	Supports Linux on Intel, Sparc, Alpha, m68k and others.	

*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#include "linuxinfo.h"

int main(int argc, char *argv[])
{
	char ordinals[13][10] = { "Unknown", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve" };

	struct os_stat os;
	struct hw_stat hw;
	struct lib_stat lib;

	int cpuinfo_fd;

	if (argc > 1)
	{
		if (strcmp(argv[1], ("-v")) == 0)
		{
			printf("%s %s\n", argv[0], VERSION);
			return 0;
		}
		if (strcmp(argv[1], ("-h")) == 0)
		{
			printf("%s %s\n", argv[0], VERSION);
			printf(" -h   print this help\n");
	    printf(" -v   print linuxinfo version\n");
			return 0;
		}
		
		cpuinfo_fd = open(argv[1], O_RDONLY);
                if (cpuinfo_fd < 0)
		{
                    fprintf(stderr,"Unsupported option or file %s not found.\n",argv[1]);
                    return -1;
		}
	}
	else
	{
		cpuinfo_fd = open(CPUINFO_FILE, O_RDONLY);
                if (cpuinfo_fd < 0)
		{
                    fprintf(stderr,"Could not open %s.\n",CPUINFO_FILE);
                    return -2;
		}
	}

	GetOperatingSystemInfo(&os);
	GetHardwareInfo(cpuinfo_fd, &hw);
	GetSystemLibcInfo(&lib);

	printf("%s %s %s %s\n", os.os_name, os.os_hostname, os.os_version, os.os_revision);
	if (hw.hw_processors > 12)
	{
	    printf("%i %s ", hw.hw_processors, hw.hw_cpuinfo);
	}
	else
	{
	    printf("%s %s ", ordinals[hw.hw_processors], hw.hw_cpuinfo);
	}

	if (strncmp(hw.hw_megahertz, "?", strlen("?")) == 0)
		printf("processor%s, %s total bogomips, %sM RAM\n", (hw.hw_processors > 1) ? "s" : "", hw.hw_bogomips, hw.hw_memory);
	else
	{
		printf("%sMHz processor%s, %s total bogomips, %sM RAM\n", hw.hw_megahertz, (hw.hw_processors > 1) ? "s" : "", hw.hw_bogomips, hw.hw_memory);
	}
	printf("System library %s\n", lib.lib_version);

	close(cpuinfo_fd);
	return 0;
}