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 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 34 | #include "busybox.h" |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 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 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 43 | static void print_element(unsigned int mask, char *element); |
| 44 | |
| 45 | /* Values that are bitwise or'd into `toprint'. */ |
| 46 | /* Operating system name. */ |
| 47 | #define PRINT_SYSNAME 1 |
| 48 | |
| 49 | /* Node name on a communications network. */ |
| 50 | #define PRINT_NODENAME 2 |
| 51 | |
| 52 | /* Operating system release. */ |
| 53 | #define PRINT_RELEASE 4 |
| 54 | |
| 55 | /* Operating system version. */ |
| 56 | #define PRINT_VERSION 8 |
| 57 | |
| 58 | /* Machine hardware name. */ |
| 59 | #define PRINT_MACHINE 16 |
| 60 | |
| 61 | /* Host processor type. */ |
| 62 | #define PRINT_PROCESSOR 32 |
| 63 | |
| 64 | /* Mask indicating which elements of the name to print. */ |
| 65 | static unsigned char toprint; |
| 66 | |
| 67 | |
| 68 | int uname_main(int argc, char **argv) |
| 69 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | struct utsname name; |
| 71 | char processor[256]; |
| 72 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 73 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | char *fake_sparc = getenv("FAKE_SPARC"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | toprint = 0; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 78 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | /* Parse any options */ |
| 80 | //fprintf(stderr, "argc=%d, argv=%s\n", argc, *argv); |
| 81 | while (--argc > 0 && **(++argv) == '-') { |
| 82 | while (*(++(*argv))) { |
| 83 | switch (**argv) { |
| 84 | case 's': |
| 85 | toprint |= PRINT_SYSNAME; |
| 86 | break; |
| 87 | case 'n': |
| 88 | toprint |= PRINT_NODENAME; |
| 89 | break; |
| 90 | case 'r': |
| 91 | toprint |= PRINT_RELEASE; |
| 92 | break; |
| 93 | case 'v': |
| 94 | toprint |= PRINT_VERSION; |
| 95 | break; |
| 96 | case 'm': |
| 97 | toprint |= PRINT_MACHINE; |
| 98 | break; |
| 99 | case 'p': |
| 100 | toprint |= PRINT_PROCESSOR; |
| 101 | break; |
| 102 | case 'a': |
| 103 | toprint = (PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE | |
| 104 | PRINT_PROCESSOR | PRINT_VERSION | |
| 105 | PRINT_MACHINE); |
| 106 | break; |
| 107 | default: |
| 108 | usage(uname_usage); |
| 109 | } |
| 110 | } |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 111 | } |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 112 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | if (toprint == 0) |
| 114 | toprint = PRINT_SYSNAME; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 115 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 116 | if (uname(&name) == -1) |
| 117 | perror("cannot get system name"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 118 | |
| 119 | #if defined (HAVE_SYSINFO) && defined (SI_ARCHITECTURE) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | if (sysinfo(SI_ARCHITECTURE, processor, sizeof(processor)) == -1) |
| 121 | perror("cannot get processor type"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 125 | strcpy(processor, "unknown"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 126 | #endif |
| 127 | |
| 128 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 129 | if (fake_sparc != NULL |
| 130 | && (fake_sparc[0] == 'y' |
| 131 | || fake_sparc[0] == 'Y')) strcpy(name.machine, "sparc"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | print_element(PRINT_SYSNAME, name.sysname); |
| 135 | print_element(PRINT_NODENAME, name.nodename); |
| 136 | print_element(PRINT_RELEASE, name.release); |
| 137 | print_element(PRINT_VERSION, name.version); |
| 138 | print_element(PRINT_MACHINE, name.machine); |
| 139 | print_element(PRINT_PROCESSOR, processor); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 140 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 141 | return(TRUE); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* If the name element set in MASK is selected for printing in `toprint', |
| 145 | print ELEMENT; then print a space unless it is the last element to |
| 146 | be printed, in which case print a newline. */ |
| 147 | |
| 148 | static void print_element(unsigned int mask, char *element) |
| 149 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | if (toprint & mask) { |
| 151 | toprint &= ~mask; |
| 152 | printf("%s%c", element, toprint ? ' ' : '\n'); |
| 153 | } |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 154 | } |