Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/utils/uname.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 815 - (show annotations) (download)
Fri Apr 24 18:32:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 3203 byte(s)
-updated to klibc-1.5.15
1 /*
2 * by tlh
3 *
4 * The uname program for system information: kernel name, kernel
5 * release, kernel release, machine, processor, platform, os and
6 * hostname.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <sys/utsname.h>
14
15 enum uname_fields {
16 UN_SYSNAME,
17 UN_NODENAME,
18 UN_RELEASE,
19 UN_VERSION,
20 UN_MACHINE,
21 #if NOT_IMPLEMENTED_PROCESSOR
22 UN_PROCESSOR,
23 #endif
24 UN_HARDWARE,
25 #if NOT_IMPLEMENTED_OS
26 UN_OS,
27 #endif
28 UN_NR_FIELDS
29 };
30
31 static void usage(FILE *stream, const char *progname)
32 {
33 fprintf(stream,
34 "Usage: %s [OPTION] . . .\n"
35 "Print system information, No options defaults to -s.\n"
36 "\n"
37 " -a print all the information in the same order as follows below\n"
38 " -s kernel name\n"
39 " -n network node name (hostname)\n"
40 " -r kernel release\n"
41 " -v kernel version\n" " -m machine hardware name\n"
42 #if NOT_IMPLEMENTED_PROCESSOR
43 " -p processor type\n"
44 #endif
45 " -i hardware platform\n"
46 #if NOT_IMPLEMENTED_OS
47 " -o operating system\n"
48 #endif
49 "\n" " -h help/usage\n" "\n", progname);
50 }
51
52 static char *make_hardware(const char *machine)
53 {
54 char *hardware;
55
56 hardware = strdup(machine);
57 if (!hardware) {
58 fprintf(stderr, "strdup() failed: %s\n", strerror(errno));
59 goto end;
60 }
61 if (strlen(hardware) == 4
62 && hardware[0] == 'i' && hardware[2] == '8' && hardware[3] == '6') {
63 hardware[1] = '3';
64 }
65 end:
66 return hardware;
67 }
68
69 int main(int argc, char *argv[])
70 {
71 int ec = 1;
72 int opt;
73 int i;
74 int nr_pr;
75 struct utsname buf;
76 char *uname_fields[UN_NR_FIELDS] = { NULL };
77
78 if (-1 == uname(&buf)) {
79 fprintf(stderr, "uname() failure: %s\n", strerror(errno));
80 goto end;
81 }
82
83 if (1 == argc)
84 /* no options given - default to -s */
85 uname_fields[UN_SYSNAME] = buf.sysname;
86
87 while ((opt = getopt(argc, argv, "asnrvmpioh")) != -1) {
88 switch (opt) {
89 case 'a':
90 uname_fields[UN_SYSNAME] = buf.sysname;
91 uname_fields[UN_NODENAME] = buf.nodename;
92 uname_fields[UN_RELEASE] = buf.release;
93 uname_fields[UN_VERSION] = buf.version;
94 uname_fields[UN_MACHINE] = buf.machine;
95 uname_fields[UN_HARDWARE] = make_hardware(buf.machine);
96 if (!uname_fields[UN_HARDWARE])
97 goto end;
98 break;
99 case 's':
100 uname_fields[UN_SYSNAME] = buf.sysname;
101 break;
102 case 'n':
103 uname_fields[UN_NODENAME] = buf.nodename;
104 break;
105 case 'r':
106 uname_fields[UN_RELEASE] = buf.release;
107 break;
108 case 'v':
109 uname_fields[UN_VERSION] = buf.version;
110 break;
111 case 'm':
112 uname_fields[UN_MACHINE] = buf.machine;
113 break;
114 #if NOT_IMPLEMENTED_PROCESSOR
115 case 'p':
116 break;
117 #endif
118 case 'i':
119 uname_fields[UN_HARDWARE] = make_hardware(buf.machine);
120 if (!uname_fields[UN_HARDWARE])
121 goto end;
122 break;
123 #if NOT_IMPLEMENTED_OS
124 case 'o':
125 break;
126 #endif
127 case 'h':
128 usage(stdout, argv[0]);
129 ec = 0;
130 goto end;
131 break;
132 default:
133 usage(stderr, argv[0]);
134 goto end;
135 break;
136 }
137 }
138
139 for (nr_pr = 0, i = UN_SYSNAME; i < UN_NR_FIELDS; i++) {
140 if (!uname_fields[i])
141 continue;
142 if (nr_pr)
143 fputc(' ', stdout);
144 fputs(uname_fields[i], stdout);
145 nr_pr++;
146 }
147 fputc('\n', stdout);
148
149 ec = 0;
150
151 end:
152 if (uname_fields[UN_HARDWARE])
153 free(uname_fields[UN_HARDWARE]);
154 return ec;
155 }