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