Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 2 | /* uname -- print system information |
| 3 | Copyright (C) 1989-1999 Free Software Foundation, Inc. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2, or (at your option) |
| 8 | any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software Foundation, |
| 17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 18 | |
| 19 | /* Option Example |
| 20 | |
| 21 | -s, --sysname SunOS |
| 22 | -n, --nodename rocky8 |
| 23 | -r, --release 4.0 |
| 24 | -v, --version |
| 25 | -m, --machine sun |
| 26 | -a, --all SunOS rocky8 4.0 sun |
| 27 | |
| 28 | The default behavior is equivalent to `-s'. |
| 29 | |
| 30 | David MacKenzie <djm@gnu.ai.mit.edu> */ |
| 31 | |
| 32 | /* Busyboxed by Erik Andersen */ |
| 33 | |
| 34 | #include "internal.h" |
| 35 | #include <stdio.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/utsname.h> |
| 38 | |
| 39 | #if defined (HAVE_SYSINFO) && defined (HAVE_SYS_SYSTEMINFO_H) |
| 40 | # include <sys/systeminfo.h> |
| 41 | #endif |
| 42 | |
| 43 | |
| 44 | static const char uname_usage[] = |
Eric Andersen | 5a0a2aa | 2000-06-02 23:26:44 +0000 | [diff] [blame^] | 45 | "uname [OPTION]...\n" |
John Beppu | 91e581f | 2000-04-17 17:49:44 +0000 | [diff] [blame] | 46 | #ifndef BB_FEATURE_TRIVIAL_HELP |
Eric Andersen | 5a0a2aa | 2000-06-02 23:26:44 +0000 | [diff] [blame^] | 47 | "\nPrint certain system information. With no OPTION, same as -s.\n\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | "Options:\n" |
| 49 | "\t-a\tprint all information\n" |
| 50 | "\t-m\tthe machine (hardware) type\n" |
| 51 | "\t-n\tprint the machine's network node hostname\n" |
| 52 | "\t-r\tprint the operating system release\n" |
| 53 | "\t-s\tprint the operating system name\n" |
| 54 | |
| 55 | "\t-p\tprint the host processor type\n" |
John Beppu | 91e581f | 2000-04-17 17:49:44 +0000 | [diff] [blame] | 56 | "\t-v\tprint the operating system version\n" |
| 57 | #endif |
| 58 | ; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | static void print_element(unsigned int mask, char *element); |
| 62 | |
| 63 | /* Values that are bitwise or'd into `toprint'. */ |
| 64 | /* Operating system name. */ |
| 65 | #define PRINT_SYSNAME 1 |
| 66 | |
| 67 | /* Node name on a communications network. */ |
| 68 | #define PRINT_NODENAME 2 |
| 69 | |
| 70 | /* Operating system release. */ |
| 71 | #define PRINT_RELEASE 4 |
| 72 | |
| 73 | /* Operating system version. */ |
| 74 | #define PRINT_VERSION 8 |
| 75 | |
| 76 | /* Machine hardware name. */ |
| 77 | #define PRINT_MACHINE 16 |
| 78 | |
| 79 | /* Host processor type. */ |
| 80 | #define PRINT_PROCESSOR 32 |
| 81 | |
| 82 | /* Mask indicating which elements of the name to print. */ |
| 83 | static unsigned char toprint; |
| 84 | |
| 85 | |
| 86 | int uname_main(int argc, char **argv) |
| 87 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | struct utsname name; |
| 89 | char processor[256]; |
| 90 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 91 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | char *fake_sparc = getenv("FAKE_SPARC"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 93 | #endif |
| 94 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 95 | toprint = 0; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 96 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | /* Parse any options */ |
| 98 | //fprintf(stderr, "argc=%d, argv=%s\n", argc, *argv); |
| 99 | while (--argc > 0 && **(++argv) == '-') { |
| 100 | while (*(++(*argv))) { |
| 101 | switch (**argv) { |
| 102 | case 's': |
| 103 | toprint |= PRINT_SYSNAME; |
| 104 | break; |
| 105 | case 'n': |
| 106 | toprint |= PRINT_NODENAME; |
| 107 | break; |
| 108 | case 'r': |
| 109 | toprint |= PRINT_RELEASE; |
| 110 | break; |
| 111 | case 'v': |
| 112 | toprint |= PRINT_VERSION; |
| 113 | break; |
| 114 | case 'm': |
| 115 | toprint |= PRINT_MACHINE; |
| 116 | break; |
| 117 | case 'p': |
| 118 | toprint |= PRINT_PROCESSOR; |
| 119 | break; |
| 120 | case 'a': |
| 121 | toprint = (PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE | |
| 122 | PRINT_PROCESSOR | PRINT_VERSION | |
| 123 | PRINT_MACHINE); |
| 124 | break; |
| 125 | default: |
| 126 | usage(uname_usage); |
| 127 | } |
| 128 | } |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 129 | } |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 130 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | if (toprint == 0) |
| 132 | toprint = PRINT_SYSNAME; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 133 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | if (uname(&name) == -1) |
| 135 | perror("cannot get system name"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 136 | |
| 137 | #if defined (HAVE_SYSINFO) && defined (SI_ARCHITECTURE) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | if (sysinfo(SI_ARCHITECTURE, processor, sizeof(processor)) == -1) |
| 139 | perror("cannot get processor type"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 143 | strcpy(processor, "unknown"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 144 | #endif |
| 145 | |
| 146 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | if (fake_sparc != NULL |
| 148 | && (fake_sparc[0] == 'y' |
| 149 | || fake_sparc[0] == 'Y')) strcpy(name.machine, "sparc"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | print_element(PRINT_SYSNAME, name.sysname); |
| 153 | print_element(PRINT_NODENAME, name.nodename); |
| 154 | print_element(PRINT_RELEASE, name.release); |
| 155 | print_element(PRINT_VERSION, name.version); |
| 156 | print_element(PRINT_MACHINE, name.machine); |
| 157 | print_element(PRINT_PROCESSOR, processor); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 158 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | exit(TRUE); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* If the name element set in MASK is selected for printing in `toprint', |
| 163 | print ELEMENT; then print a space unless it is the last element to |
| 164 | be printed, in which case print a newline. */ |
| 165 | |
| 166 | static void print_element(unsigned int mask, char *element) |
| 167 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 168 | if (toprint & mask) { |
| 169 | toprint &= ~mask; |
| 170 | printf("%s%c", element, toprint ? ' ' : '\n'); |
| 171 | } |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 172 | } |