Contents of /trunk/mkinitrd-magellan/klibc/usr/utils/uname.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 3193 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 3193 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
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 | 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 | char *make_hardware(const char *machine) |
53 | { |
54 | char *hardware; |
55 | |
56 | if (!(hardware = strdup(machine))) { |
57 | fprintf(stderr, "strdup() failed: %s\n", strerror(errno)); |
58 | goto end; |
59 | } |
60 | if (strlen(hardware) == 4 |
61 | && hardware[0] == 'i' && hardware[2] == '8' && hardware[3] == '6') { |
62 | hardware[1] = '3'; |
63 | } |
64 | end: |
65 | return hardware; |
66 | } |
67 | |
68 | int main(int argc, char *argv[]) |
69 | { |
70 | int ec = 1; |
71 | int opt; |
72 | int i; |
73 | int nr_pr; |
74 | struct utsname buf; |
75 | char *uname_fields[UN_NR_FIELDS] = { NULL }; |
76 | |
77 | if (-1 == uname(&buf)) { |
78 | fprintf(stderr, "uname() failure: %s\n", strerror(errno)); |
79 | goto end; |
80 | } |
81 | |
82 | if (1 == argc) |
83 | /* no options given - default to -s */ |
84 | uname_fields[UN_SYSNAME] = buf.sysname; |
85 | |
86 | while ((opt = getopt(argc, argv, "asnrvmpioh")) != -1) { |
87 | switch (opt) { |
88 | case 'a': |
89 | uname_fields[UN_SYSNAME] = buf.sysname; |
90 | uname_fields[UN_NODENAME] = buf.nodename; |
91 | uname_fields[UN_RELEASE] = buf.release; |
92 | uname_fields[UN_VERSION] = buf.version; |
93 | uname_fields[UN_MACHINE] = buf.machine; |
94 | uname_fields[UN_HARDWARE] = make_hardware(buf.machine); |
95 | if (!uname_fields[UN_HARDWARE]) |
96 | goto end; |
97 | break; |
98 | case 's': |
99 | uname_fields[UN_SYSNAME] = buf.sysname; |
100 | break; |
101 | case 'n': |
102 | uname_fields[UN_NODENAME] = buf.nodename; |
103 | break; |
104 | case 'r': |
105 | uname_fields[UN_RELEASE] = buf.release; |
106 | break; |
107 | case 'v': |
108 | uname_fields[UN_VERSION] = buf.version; |
109 | break; |
110 | case 'm': |
111 | uname_fields[UN_MACHINE] = buf.machine; |
112 | break; |
113 | #if NOT_IMPLEMENTED_PROCESSOR |
114 | case 'p': |
115 | break; |
116 | #endif |
117 | case 'i': |
118 | uname_fields[UN_HARDWARE] = make_hardware(buf.machine); |
119 | if (!uname_fields[UN_HARDWARE]) |
120 | goto end; |
121 | break; |
122 | #if NOT_IMPLEMENTED_OS |
123 | case 'o': |
124 | break; |
125 | #endif |
126 | case 'h': |
127 | usage(stdout, argv[0]); |
128 | ec = 0; |
129 | goto end; |
130 | break; |
131 | default: |
132 | usage(stderr, argv[0]); |
133 | goto end; |
134 | break; |
135 | } |
136 | } |
137 | |
138 | for (nr_pr = 0, i = UN_SYSNAME; i < UN_NR_FIELDS; i++) { |
139 | if (!uname_fields[i]) |
140 | continue; |
141 | if (nr_pr) |
142 | fputc(' ', stdout); |
143 | fputs(uname_fields[i], stdout); |
144 | nr_pr++; |
145 | } |
146 | fputc('\n', stdout); |
147 | |
148 | ec = 0; |
149 | |
150 | end: |
151 | if (uname_fields[UN_HARDWARE]) |
152 | free(uname_fields[UN_HARDWARE]); |
153 | return ec; |
154 | } |