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 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 19 | /* BB_AUDIT SUSv3 compliant */ |
| 20 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */ |
| 21 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 22 | /* Option Example |
| 23 | |
| 24 | -s, --sysname SunOS |
| 25 | -n, --nodename rocky8 |
| 26 | -r, --release 4.0 |
| 27 | -v, --version |
| 28 | -m, --machine sun |
| 29 | -a, --all SunOS rocky8 4.0 sun |
| 30 | |
| 31 | The default behavior is equivalent to `-s'. |
| 32 | |
| 33 | David MacKenzie <djm@gnu.ai.mit.edu> */ |
| 34 | |
| 35 | /* Busyboxed by Erik Andersen */ |
| 36 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 37 | /* Further size reductions by Glenn McGrath and Manuel Novoa III. */ |
| 38 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 39 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 40 | * |
| 41 | * Now does proper error checking on i/o. Plus some further space savings. |
| 42 | */ |
| 43 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 45 | #include <stdlib.h> |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 46 | #include <stddef.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 47 | #include <string.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 48 | #include <unistd.h> |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/utsname.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 51 | #include "busybox.h" |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 52 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 53 | typedef struct { |
| 54 | struct utsname name; |
| 55 | char processor[8]; /* for "unknown" */ |
| 56 | } uname_info_t; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 57 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 58 | static const char options[] = "snrvmpa"; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 59 | static const unsigned short int utsname_offset[] = { |
| 60 | offsetof(uname_info_t,name.sysname), |
| 61 | offsetof(uname_info_t,name.nodename), |
| 62 | offsetof(uname_info_t,name.release), |
| 63 | offsetof(uname_info_t,name.version), |
| 64 | offsetof(uname_info_t,name.machine), |
| 65 | offsetof(uname_info_t,processor) |
| 66 | }; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 67 | |
| 68 | int uname_main(int argc, char **argv) |
| 69 | { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 70 | uname_info_t uname_info; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 71 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | char *fake_sparc = getenv("FAKE_SPARC"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 73 | #endif |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 74 | const unsigned short int *delta; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 75 | char toprint; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 76 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 77 | toprint = bb_getopt_ulflags(argc, argv, options); |
| 78 | |
| 79 | if (argc != optind) { |
| 80 | bb_show_usage(); |
| 81 | } |
| 82 | |
| 83 | if (toprint & (1 << 6)) { |
| 84 | toprint = 0x3f; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (toprint == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 88 | toprint = 1; /* sysname */ |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (uname(&uname_info.name) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 92 | bb_error_msg_and_die("cannot get system name"); |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | #if defined(__sparc__) && defined(__linux__) |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 96 | if ((fake_sparc != NULL) |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 97 | && ((fake_sparc[0] == 'y') |
| 98 | || (fake_sparc[0] == 'Y'))) { |
| 99 | strcpy(uname_info.name.machine, "sparc"); |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | strcpy(uname_info.processor, "unknown"); |
| 104 | |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 105 | delta=utsname_offset; |
| 106 | do { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 107 | if (toprint & 1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 108 | bb_printf(((char *)(&uname_info)) + *delta); |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 109 | if (toprint > 1) { |
| 110 | putchar(' '); |
| 111 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | } |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 113 | ++delta; |
| 114 | } while (toprint >>= 1); |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 115 | putchar('\n'); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 116 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame^] | 117 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 118 | } |