[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /* uname -- print system information
|
---|
[1765] | 3 | * Copyright (C) 1989-1999 Free Software Foundation, Inc.
|
---|
| 4 | *
|
---|
[2725] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[1765] | 6 | */
|
---|
[821] | 7 |
|
---|
| 8 | /* BB_AUDIT SUSv3 compliant */
|
---|
| 9 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
|
---|
| 10 |
|
---|
| 11 | /* Option Example
|
---|
[2725] | 12 | * -s, --sysname SunOS
|
---|
| 13 | * -n, --nodename rocky8
|
---|
| 14 | * -r, --release 4.0
|
---|
| 15 | * -v, --version
|
---|
| 16 | * -m, --machine sun
|
---|
| 17 | * -a, --all SunOS rocky8 4.0 sun
|
---|
| 18 | *
|
---|
| 19 | * The default behavior is equivalent to '-s'.
|
---|
| 20 | *
|
---|
| 21 | * David MacKenzie <djm@gnu.ai.mit.edu>
|
---|
| 22 | *
|
---|
| 23 | * GNU coreutils 6.10:
|
---|
| 24 | * Option: struct Example(s):
|
---|
| 25 | * utsname
|
---|
| 26 | * field:
|
---|
| 27 | * -s, --kernel-name sysname Linux
|
---|
| 28 | * -n, --nodename nodename localhost.localdomain
|
---|
| 29 | * -r, --kernel-release release 2.6.29
|
---|
| 30 | * -v, --kernel-version version #1 SMP Sun Jan 11 20:52:37 EST 2009
|
---|
| 31 | * -m, --machine machine x86_64 i686
|
---|
| 32 | * -p, --processor (none) x86_64 i686
|
---|
| 33 | * -i, --hardware-platform (none) x86_64 i386
|
---|
| 34 | * NB: vanilla coreutils reports "unknown" -p and -i,
|
---|
| 35 | * x86_64 and i686/i386 shown above are Fedora's inventions.
|
---|
| 36 | * -o, --operating-system (none) GNU/Linux
|
---|
| 37 | * -a, --all: all of the above, in the order shown.
|
---|
| 38 | * If -p or -i is not known, don't show them
|
---|
| 39 | */
|
---|
[821] | 40 |
|
---|
[2725] | 41 | /* Busyboxed by Erik Andersen
|
---|
[821] | 42 | *
|
---|
[2725] | 43 | * Before 2003: Glenn McGrath and Manuel Novoa III
|
---|
| 44 | * Further size reductions.
|
---|
| 45 | * Mar 16, 2003: Manuel Novoa III (mjn3@codepoet.org)
|
---|
| 46 | * Now does proper error checking on i/o. Plus some further space savings.
|
---|
| 47 | * Jan 2009:
|
---|
| 48 | * Fix handling of -a to not print "unknown", add -o and -i support.
|
---|
[821] | 49 | */
|
---|
| 50 |
|
---|
[3232] | 51 | //usage:#define uname_trivial_usage
|
---|
[3621] | 52 | //usage: "[-amnrspvio]"
|
---|
[3232] | 53 | //usage:#define uname_full_usage "\n\n"
|
---|
| 54 | //usage: "Print system information\n"
|
---|
| 55 | //usage: "\n -a Print all"
|
---|
| 56 | //usage: "\n -m The machine (hardware) type"
|
---|
| 57 | //usage: "\n -n Hostname"
|
---|
[3621] | 58 | //usage: "\n -r Kernel release"
|
---|
| 59 | //usage: "\n -s Kernel name (default)"
|
---|
[3232] | 60 | //usage: "\n -p Processor type"
|
---|
[3621] | 61 | //usage: "\n -v Kernel version"
|
---|
| 62 | //usage: "\n -i The hardware platform"
|
---|
| 63 | //usage: "\n -o OS name"
|
---|
[3232] | 64 | //usage:
|
---|
| 65 | //usage:#define uname_example_usage
|
---|
| 66 | //usage: "$ uname -a\n"
|
---|
| 67 | //usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n"
|
---|
| 68 |
|
---|
[2725] | 69 | #include "libbb.h"
|
---|
| 70 | /* After libbb.h, since it needs sys/types.h on some systems */
|
---|
[821] | 71 | #include <sys/utsname.h>
|
---|
| 72 |
|
---|
| 73 | typedef struct {
|
---|
| 74 | struct utsname name;
|
---|
[2725] | 75 | char processor[sizeof(((struct utsname*)NULL)->machine)];
|
---|
| 76 | char platform[sizeof(((struct utsname*)NULL)->machine)];
|
---|
[3621] | 77 | char os[sizeof(CONFIG_UNAME_OSNAME)];
|
---|
[821] | 78 | } uname_info_t;
|
---|
| 79 |
|
---|
[2725] | 80 | static const char options[] ALIGN1 = "snrvmpioa";
|
---|
| 81 | static const unsigned short utsname_offset[] = {
|
---|
| 82 | offsetof(uname_info_t, name.sysname), /* -s */
|
---|
| 83 | offsetof(uname_info_t, name.nodename), /* -n */
|
---|
| 84 | offsetof(uname_info_t, name.release), /* -r */
|
---|
| 85 | offsetof(uname_info_t, name.version), /* -v */
|
---|
| 86 | offsetof(uname_info_t, name.machine), /* -m */
|
---|
| 87 | offsetof(uname_info_t, processor), /* -p */
|
---|
| 88 | offsetof(uname_info_t, platform), /* -i */
|
---|
| 89 | offsetof(uname_info_t, os), /* -o */
|
---|
[821] | 90 | };
|
---|
| 91 |
|
---|
[2725] | 92 | int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 93 | int uname_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 94 | {
|
---|
[2725] | 95 | #if ENABLE_LONG_OPTS
|
---|
| 96 | static const char uname_longopts[] ALIGN1 =
|
---|
| 97 | /* name, has_arg, val */
|
---|
| 98 | "all\0" No_argument "a"
|
---|
| 99 | "kernel-name\0" No_argument "s"
|
---|
| 100 | "nodename\0" No_argument "n"
|
---|
| 101 | "kernel-release\0" No_argument "r"
|
---|
| 102 | "release\0" No_argument "r"
|
---|
| 103 | "kernel-version\0" No_argument "v"
|
---|
| 104 | "machine\0" No_argument "m"
|
---|
| 105 | "processor\0" No_argument "p"
|
---|
| 106 | "hardware-platform\0" No_argument "i"
|
---|
| 107 | "operating-system\0" No_argument "o"
|
---|
| 108 | ;
|
---|
| 109 | #endif
|
---|
[821] | 110 | uname_info_t uname_info;
|
---|
| 111 | #if defined(__sparc__) && defined(__linux__)
|
---|
| 112 | char *fake_sparc = getenv("FAKE_SPARC");
|
---|
| 113 | #endif
|
---|
[2725] | 114 | const char *unknown_str = "unknown";
|
---|
| 115 | const char *fmt;
|
---|
| 116 | const unsigned short *delta;
|
---|
| 117 | unsigned toprint;
|
---|
[821] | 118 |
|
---|
[2725] | 119 | IF_LONG_OPTS(applet_long_options = uname_longopts);
|
---|
[1765] | 120 | toprint = getopt32(argv, options);
|
---|
[821] | 121 |
|
---|
[2725] | 122 | if (argv[optind]) { /* coreutils-6.9 compat */
|
---|
[821] | 123 | bb_show_usage();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[2725] | 126 | if (toprint & (1 << 8)) { /* -a => all opts on */
|
---|
| 127 | toprint = (1 << 8) - 1;
|
---|
| 128 | unknown_str = ""; /* -a does not print unknown fields */
|
---|
[821] | 129 | }
|
---|
| 130 |
|
---|
[2725] | 131 | if (toprint == 0) { /* no opts => -s (sysname) */
|
---|
| 132 | toprint = 1;
|
---|
[821] | 133 | }
|
---|
| 134 |
|
---|
[2725] | 135 | uname(&uname_info.name); /* never fails */
|
---|
[821] | 136 |
|
---|
| 137 | #if defined(__sparc__) && defined(__linux__)
|
---|
[2725] | 138 | if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
|
---|
[821] | 139 | strcpy(uname_info.name.machine, "sparc");
|
---|
| 140 | }
|
---|
| 141 | #endif
|
---|
[2725] | 142 | strcpy(uname_info.processor, unknown_str);
|
---|
| 143 | strcpy(uname_info.platform, unknown_str);
|
---|
[3621] | 144 | strcpy(uname_info.os, CONFIG_UNAME_OSNAME);
|
---|
[2725] | 145 | #if 0
|
---|
| 146 | /* Fedora does something like this */
|
---|
| 147 | strcpy(uname_info.processor, uname_info.name.machine);
|
---|
| 148 | strcpy(uname_info.platform, uname_info.name.machine);
|
---|
| 149 | if (uname_info.platform[0] == 'i'
|
---|
| 150 | && uname_info.platform[1]
|
---|
| 151 | && uname_info.platform[2] == '8'
|
---|
| 152 | && uname_info.platform[3] == '6'
|
---|
| 153 | ) {
|
---|
| 154 | uname_info.platform[1] = '3';
|
---|
| 155 | }
|
---|
| 156 | #endif
|
---|
[821] | 157 |
|
---|
[1765] | 158 | delta = utsname_offset;
|
---|
[2725] | 159 | fmt = " %s" + 1;
|
---|
[821] | 160 | do {
|
---|
| 161 | if (toprint & 1) {
|
---|
[2725] | 162 | const char *p = (char *)(&uname_info) + *delta;
|
---|
| 163 | if (p[0]) {
|
---|
| 164 | printf(fmt, p);
|
---|
| 165 | fmt = " %s";
|
---|
[821] | 166 | }
|
---|
| 167 | }
|
---|
| 168 | ++delta;
|
---|
| 169 | } while (toprint >>= 1);
|
---|
[2725] | 170 | bb_putchar('\n');
|
---|
[821] | 171 |
|
---|
[2725] | 172 | fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
|
---|
[821] | 173 | }
|
---|