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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* by tlh
*
* The uname program for system information: kernel name, kernel
* release, kernel release, machine, processor, platform, os and
* hostname.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/utsname.h>
enum uname_fields {
UN_SYSNAME,
UN_NODENAME,
UN_RELEASE,
UN_VERSION,
UN_MACHINE,
#if NOT_IMPLEMENTED_PROCESSOR
UN_PROCESSOR,
#endif
UN_HARDWARE,
#if NOT_IMPLEMENTED_OS
UN_OS,
#endif
UN_NR_FIELDS
};
static void usage(FILE *stream, const char *progname)
{
fprintf(stream,
"Usage: %s [OPTION] . . .\n"
"Print system information, No options defaults to -s.\n"
"\n"
" -a print all the information in the same order as follows below\n"
" -s kernel name\n"
" -n network node name (hostname)\n"
" -r kernel release\n"
" -v kernel version\n" " -m machine hardware name\n"
#if NOT_IMPLEMENTED_PROCESSOR
" -p processor type\n"
#endif
" -i hardware platform\n"
#if NOT_IMPLEMENTED_OS
" -o operating system\n"
#endif
"\n" " -h help/usage\n" "\n", progname);
}
static char *make_hardware(const char *machine)
{
char *hardware;
hardware = strdup(machine);
if (!hardware) {
fprintf(stderr, "strdup() failed: %s\n", strerror(errno));
goto end;
}
if (strlen(hardware) == 4
&& hardware[0] == 'i' && hardware[2] == '8' && hardware[3] == '6') {
hardware[1] = '3';
}
end:
return hardware;
}
int main(int argc, char *argv[])
{
int ec = 1;
int opt;
int i;
int nr_pr;
struct utsname buf;
char *uname_fields[UN_NR_FIELDS] = { NULL };
if (-1 == uname(&buf)) {
fprintf(stderr, "uname() failure: %s\n", strerror(errno));
goto end;
}
if (1 == argc)
/* no options given - default to -s */
uname_fields[UN_SYSNAME] = buf.sysname;
while ((opt = getopt(argc, argv, "asnrvmpioh")) != -1) {
switch (opt) {
case 'a':
uname_fields[UN_SYSNAME] = buf.sysname;
uname_fields[UN_NODENAME] = buf.nodename;
uname_fields[UN_RELEASE] = buf.release;
uname_fields[UN_VERSION] = buf.version;
uname_fields[UN_MACHINE] = buf.machine;
uname_fields[UN_HARDWARE] = make_hardware(buf.machine);
if (!uname_fields[UN_HARDWARE])
goto end;
break;
case 's':
uname_fields[UN_SYSNAME] = buf.sysname;
break;
case 'n':
uname_fields[UN_NODENAME] = buf.nodename;
break;
case 'r':
uname_fields[UN_RELEASE] = buf.release;
break;
case 'v':
uname_fields[UN_VERSION] = buf.version;
break;
case 'm':
uname_fields[UN_MACHINE] = buf.machine;
break;
#if NOT_IMPLEMENTED_PROCESSOR
case 'p':
break;
#endif
case 'i':
uname_fields[UN_HARDWARE] = make_hardware(buf.machine);
if (!uname_fields[UN_HARDWARE])
goto end;
break;
#if NOT_IMPLEMENTED_OS
case 'o':
break;
#endif
case 'h':
usage(stdout, argv[0]);
ec = 0;
goto end;
break;
default:
usage(stderr, argv[0]);
goto end;
break;
}
}
for (nr_pr = 0, i = UN_SYSNAME; i < UN_NR_FIELDS; i++) {
if (!uname_fields[i])
continue;
if (nr_pr)
fputc(' ', stdout);
fputs(uname_fields[i], stdout);
nr_pr++;
}
fputc('\n', stdout);
ec = 0;
end:
if (uname_fields[UN_HARDWARE])
free(uname_fields[UN_HARDWARE]);
return ec;
}
|